Repository navigation
perf(jit): compile Unsafe array element access inline and copy short strings with a loop - #141
Conversation
|
Warning Review limit reachedYou'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. View limit details
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. |
There was a problem hiding this comment.
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
e4c7b2f to
1624195
Compare
…strings with a loop
1624195 to
360bbd1
Compare
There was a problem hiding this comment.
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
What this changes
Number formatting and string concatenation run about 30% faster in compiled code. Two changes:
Unsafe.get<Kind>orput<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, whichInteger.toString,Long.toString,StringBuilder.append(int)and invokedynamic concatenation use, writes each digit throughUnsafe.putByte, so each digit was a native call.subarrayview 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
longoffset of each Unsafe access, and the frame each small call pushes.Tests
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, thenInteger.toString,Long.toStringand concatenation of edge values. It prints the same as HotSpot.pnpm typecheck.Integer.toString(i)"id=" + i + " name=" + s(indy)x + y, two StringsNoticed, not fixed
env option replaces the host environmentintest/programs.test.tsfails on master as well as on this branch, on the machine these ran on.