Skip to content

perf(jit): compile Unsafe array element access inline and copy short strings with a loop - #141

Merged
Project516 merged 1 commit into
masterfrom
p516/perf-string-concat
Dec 5, 2026
Merged

Project516 merged 1 commit into
masterfrom
p516/perf-string-concat

Conversation

@Project516

Copy link
Copy Markdown
Owner

What this changes

Number formatting and string concatenation run about 30% faster in compiled code. Two changes:

  • The JIT compiles a call to Unsafe.get<Kind> or put<Kind> (plain or volatile, for byte, short, char, int, long, float and double) to a plain element access when the base is an array of that kind, as HotSpot's Unsafe intrinsics do. Any other base (another array kind, native memory, null) still calls the native. DecimalDigits, which Integer.toString, Long.toString, StringBuilder.append(int) and invokedynamic concatenation use, writes each digit through Unsafe.putByte, so each digit was a native call.
  • The string natives copy short byte arrays (64 bytes or fewer) with a loop. Making a subarray view to copy a few bytes took 11% of an int+String concatenation.

Part of #135. The largest cost left is the BigInt arithmetic for the long offset of each Unsafe access, and the frame each small call pushes.

Tests

  • New program test/java/UnsafeArrays.java: Unsafe reads and writes in loops hot enough to compile, on arrays of the accessed kind, arrays of another kind and native memory, then Integer.toString, Long.toString and concatenation of edge values. It prints the same as HotSpot.
  • Program tests compared with HotSpot, all passing: UnsafeArrays, StringConcat, StringBuilders, Strings, Numbers, Unaligned, ByteArrayViews, UnsafeAtomics (both), VarHandles, JitLoops, ForeignMemory, MiscUnsafe, MappedFile.
  • pnpm typecheck.
  • Benchmark, 200,000 calls per round under Node, last round of 5 alternating runs each (median, range):
master this branch
Integer.toString(i) 1841 ns (1540 to 1863) 1318 ns (1305 to 2159)
"id=" + i + " name=" + s (indy) 3558 ns (3412 to 4121) 2485 ns (2478 to 2664)
x + y, two Strings 422 ns (416 to 438) 428 ns (410 to 442)

Noticed, not fixed

  • env option replaces the host environment in test/programs.test.ts fails on master as well as on this branch, on the machine these ran on.

@coderabbitai

coderabbitai Bot commented Dec 4, 2026 •

Copy link
Copy Markdown

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 39 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available.

Learn how review limits work.

Review configuration:

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: ASSERTIVE
  • Plan: Advanced
  • Run ID: 3e5f9ab5-983a-4f33-b03a-e4ae4ad809ec
Commits

Reviewing files that changed from the base of the PR and between df33234 and 360bbd1.

Files selected for processing: 4

  • src/jit.ts
  • src/natives/misc.ts
  • src/natives/strings.ts
  • test/java/UnsafeArrays.java
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@project516-review-bot project516-review-bot Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The PR inlines Unsafe.get/put (plain and volatile) for array bases of the matching element type in the JIT, falling back to the native for other bases, and copies short byte arrays with a loop instead of a subarray view in strings.ts. It also exports WIDTH from misc.ts and adds an UnsafeArrays test. The JIT change is the risky part: the get fast path writes the loaded value to args[0], which is the Unsafe receiver argument, instead of the invoke result slot, and the put value is read from args[4], which only lines up if longs occupy two frame slots.


review-bot, model nvidia/nemotron-3.5-super-vl:free, verdict request_changes

Comment thread src/jit.ts Resolved
Comment thread src/natives/strings.ts Resolved
@Project516
Project516 force-pushed the p516/perf-string-concat branch from e4c7b2f to 1624195 Compare December 4, 2026 23:26
@Project516
Project516 force-pushed the p516/perf-string-concat branch from 1624195 to 360bbd1 Compare December 4, 2026 23:28

@project516-review-bot project516-review-bot Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The JIT now inlines Unsafe.get/put (plain and volatile) for byte, short, char, int, long, float and double when the base is an array of the matching kind, falling back to the native call otherwise. WIDTH is exported from natives/misc.ts, and natives/strings.ts copies byte arrays of 64 bytes or fewer with an element loop instead of subarray. A new test program, test/java/UnsafeArrays.java, exercises the access paths and number formatting.


review-bot, model nvidia/nemotron-3.5-super-vl:free, verdict approve

@Project516
Project516 enabled auto-merge (squash) December 4, 2026 23:32
@Project516
Project516 merged commit 5ab42fd into master Dec 5, 2026
16 checks passed
@Project516
Project516 deleted the p516/perf-string-concat branch December 5, 2026 00:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant