Skip to content

perf(jit): call Unsafe, System.arraycopy and other common natives directly from compiled code, cutting slow concurrency and digest tests by 10 to 30% - #142

Merged
Project516 merged 3 commits into
masterfrom
p516/jit-unsafe
Dec 5, 2026

Conversation

@Project516

@Project516 Project516 commented Dec 5, 2026 •

Copy link
Copy Markdown
Owner

What this changes

The slowest OpenJDK tests set how long each CI shard takes. Their profiles showed compiled code calling a small set of natives over and over through the generic native-call path: an argument array, a try/catch, a generator check, and a thread-state check after each call. These are Unsafe's field, array and memory accessors and atomics, which ConcurrentHashMap, atomics, VarHandles, locks and fork/join use, plus System.arraycopy and a few others. HotSpot intrinsifies all of them.

Compiled code now calls these natives directly, as an inline expression or a call to a runtime helper:

  • Unsafe get/put, including the Volatile, Acquire, Release and Opaque forms, plus compareAndSet, weakCompareAndSet, compareAndExchange, getAndSet and getAndAdd, for every kind. perf(jit): compile Unsafe array element access inline and copy short strings with a loop #141's inline primitive-array path still runs first for get/put on a primitive array of the matching kind. Other bases now take the direct call instead of the native.
  • Class.isInstance, Object.getClass, Object.hashCode when the receiver's class doesn't override it, System.identityHashCode, Thread.currentThread, Thread.currentCarrierThread, System.nanoTime and System.currentTimeMillis.
  • System.arraycopy. Its exceptions keep the System.arraycopy(Native Method) frame at the top of the stack trace, as on HotSpot.
  • A method inlined into its caller can now inline these natives too. Before, any native callee stopped the inlining.

The arraycopy type-mismatch message named element types the way HotSpot does only for object arrays. Before: can not copy I[] into J[]. After: can not copy int[] into long[], and object array[] for object arrays.

Tests

Test master branch
Integrate 84, 65 48, 45
TestSameLength 91, 64 53, 50
Collisions 92, 50 44, 44
JSR166TestCase#3 119, 106 98, 98
RandomCanaryPi 161, 123 115, 112
MethodOrderException 48, 37 40, 37

The first master run was slower than the second, so the second is the fair baseline.

  • Master with perf(jit): compile Unsafe array element access inline and copy short strings with a loop #141 and this branch, rebased on it, alternated. Times are in ms; locks had 4 runs each, the rest 2:
    • uncontended ReentrantLock lock and unlock, 1M times: 198 to 232 on master, 123 to 158 on the branch
    • LockSupport.park/unpark between two threads: unchanged, 47 to 78 on both
    • atomics: 471, 487 on master; 204, 213 on the branch
    • LinkedBlockingQueue: 279, 279 on master; 174, 178 on the branch
    • ConcurrentHashMap: 394, 396 on master; 321, 324 on the branch
  • New programs compared with HotSpot:
  • Before the rebase, the full unit suite passed: 302 passed, 0 failed. The OpenJDK tests in passing.txt under java/util/concurrent, java/lang/invoke, java/nio/Buffer and java/lang/ref passed, 75 tests. pnpm typecheck passes.

@coderabbitai

coderabbitai Bot commented Dec 5, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Summary

Summary by CodeRabbit

  • New Features
    • Compiled code now supports additional unsafe field, array, and memory operations, along with array copying and more built-in operations.
    • Improved error details for invalid array copies.
  • Tests
    • Added compiled-code coverage for array copying, unsafe operations, and type checks.

The JIT now supports compiled Unsafe accessors, System.arraycopy, and additional pure native intrinsics. Native arguments are passed as arrays, and pending arraycopy exceptions receive method and PC metadata. Trivial callees can enter the inline path without bytecode. Java tests exercise compiled arraycopy and Unsafe operations.

Changes: Compiled native support

Layer / File(s) Summary
Native Unsafe and arraycopy helpers
src/natives/lang.ts, src/natives/misc.ts
arraycopy is exported and uses a shared helper to format array component names. Unsafe compare-and-set, get-and-set, and get-and-add handlers delegate to exported helpers.
Compiled JIT dispatch and tests
src/jit.ts, test/java/ArraycopyCompiled.java, test/java/UnsafeCompiled.java
The JIT adds compiled mappings for Unsafe operations, System.arraycopy, and other pure intrinsics. Native arguments use arrays, and the arraycopy wrapper attaches method and PC details to pending exceptions. The Java tests exercise compiled arraycopy and Unsafe operations.
Trivial callee inlining
src/jit.ts
The JIT checks for a trivial callee before rejecting callees without bytecode or marked abstract.

Estimated code review effort: 3 (Moderate) | ~25 minutes

Change: Refactor

Sequence Diagram(s)

sequenceDiagram
  participant CompiledMethod
  participant JIT
  participant arraycopyWrapper
  participant arraycopy
  CompiledMethod->>JIT: Invoke compiled System.arraycopy
  JIT->>arraycopyWrapper: Pass native argument array
  arraycopyWrapper->>arraycopy: Copy arrays
  arraycopy-->>arraycopyWrapper: Return or raise PendingThrow
  alt PendingThrow
    arraycopyWrapper->>arraycopyWrapper: Attach method and bytecode PC
    arraycopyWrapper-->>JIT: Rethrow PendingThrow
  else Copy succeeds
    arraycopyWrapper-->>JIT: Return
  end
Loading
Merge Risk: Minimal · up to 7757c

This change compiles Unsafe accessors, System.arraycopy and several common natives directly, which speeds up compiled code. The reviewed evidence shows no concrete defect in exception handling or argument passing, and the author reports passing test and typecheck results. Merge risk is minimal.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names the main change: compiled code calling Unsafe, System.arraycopy and other common natives directly. It also states the expected gain of 10 to 30%. The changeset matches this: src/jit.ts…
Description check Passed The description follows the three-section template: What this changes, Tests, and Noticed, not fixed. The What this changes section explains the motivation and lists the behavior changes, including th…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title names the main change: compiled code calling Unsafe, System.arraycopy and other common natives directly. It also states the expected gain of 10 to 30%. The changeset matches this: src/jit.ts adds direct paths for Unsafe accessors and arraycopy, and the test programs cover them. The title is long (149 characters) but it is specific and accurate.

Full details: Description check

Explanation

The description follows the three-section template: What this changes, Tests, and Noticed, not fixed. The What this changes section explains the motivation and lists the behavior changes, including the arraycopy message change. The Tests section gives benchmark tables, the new programs compared with HotSpot, and unit and OpenJDK results. The Noticed, not fixed section is absent, which the template allows because it is optional when there are no items. The description does not state the numbers as median and spread, which the template asks for on performance work. It gives per-run timings instead, so the spread is visible, but a median is not reported.

Finishing Touches

Simplify code

  • Commit to this branch
  • Create a new PR
  • 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

A rabbit checks the fields with care,
Then sends array copies through the air.
Small arguments hop in an array,
A call-site mark shows where they stray.
The compiled paths now run and play.

Recent review info

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: f9387153-10ee-485c-bc0c-32becadb7cb7
Commits

Reviewing files that changed from the base of the PR and between 5ab42fd and 7757c18.

Files selected for processing: 5

  • src/jit.ts
  • src/natives/lang.ts
  • src/natives/misc.ts
  • test/java/ArraycopyCompiled.java
  • test/java/UnsafeCompiled.java

Included review availability: This review used your included allowance. Your plan provides up to 1 included review per hour; 0 remain after this review.

Recent review details

Context from checks skipped due to timeout: 10

  • GitHub Check: CodeRabbit
  • GitHub Check: jtreg / run (3)
  • GitHub Check: jtreg / run (8)
  • GitHub Check: jtreg / run (1)
  • GitHub Check: jtreg / run (6)
  • GitHub Check: jtreg / run (2)
  • GitHub Check: jtreg / run (5)
  • GitHub Check: jtreg / run (4)
  • GitHub Check: jtreg / run (7)
  • GitHub Check: test
Additional comments: 1

src/jit.ts: 1

1364-1365: Stability & Availability

The inlined arraycopy exception keeps its inner PC on the wrapper frame.

The concern assumes that region restoration leaves only the caller frame. The inspected code contradicts that. The copy region sets ifs before its body, so an exception from the inlined arraycopy enters deoptCatch. That path rebuilds the copy frame with e.pc before it exits to handleError. The caller's frame is restored with its own call-site PC, so the caller's exception table is not searched with the inner PC.

…call, and name element types in its mismatch message as HotSpot does

@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 a set of HotSpot-intrinsic natives (Unsafe accessors and atomics, System.arraycopy, Class.isInstance, Object.getClass/hashCode, Thread.currentThread, System.nanoTime/currentTimeMillis, identityHashCode) directly into compiled code as JS expressions or calls to runtime helpers, and allows inlined methods to inline these natives too. It also changes the arraycopy type-mismatch message to use Java element names and object-array wording. The full unit suite and OpenJDK passing tests are reported green.

I reviewed the diff for bugs, wrong logic, unhandled errors, and data-loss risks. The inlining guard change correctly lets trivial natives bypass the no-code check, nativeArgs now returns an array and both callers are updated, the arraycopy wrapper sets the native method and pc on PendingThrow for stack traces, and the CAS/getAndSet/getAndAdd extractions preserve the old read-modify-write semantics. I found no defect I can demonstrate from the diff alone.

One item is worth verifying because I cannot see the definition it depends on: elemName returns c.javaName for primitive component classes. If javaName returns internal JVM descriptors (I, J) rather than Java source names (int, long), the arraycopy mismatch message would not change as the PR claims. The reported test results suggest it is correct, but it is not visible in the diff.


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

@Project516

Copy link
Copy Markdown
Owner Author

On elemName: javaName of a primitive class is its Java source name (int, long). ArraycopyCompiled checks this against HotSpot. Its output includes arraycopy: type mismatch: can not copy int[] into long[], object array[] into int[] and int[] into object array[], the same as HotSpot prints. Before this change, master printed I[] into J[].

@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 HotSpot-intrinsic natives (Unsafe accessors/atomics, System.arraycopy, Class.isInstance, Object.getClass/hashCode, identityHashCode, Thread.currentThread/carrierThread, nanoTime, currentTimeMillis) as compiled-code expressions, and allows trivial natives to be inlined into callers. It also changes the arraycopy type-mismatch message to use primitive type names and "object array" for object arrays, and adds two compiled-code tests. The full suite and OpenJDK passing tests are reported as passing.


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

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