Repository navigation
perf: format numbers in JS for the JDK's DecimalDigits, DoubleToDecimal and FloatToDecimal - #147
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 23 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 replaces JDK number-formatting helpers (DecimalDigits, DoubleToDecimal, FloatToDecimal) with JS intrinsics in src/natives/numbers.ts, adds PURE inlining in src/jit.ts, wires the intrinsics in src/natives/index.ts, and adds test/java/NumberStrings.java. Two defects are visible in the diff: the exactly function uses floating-point arithmetic for a bound check that can exceed 2^53, and javaDecimal computes the exponent before a trailing-zero trim that would invalidate it.
review-bot, model nvidia/nemotron-3-ultra-550b-a55b:free, verdict request_changes
There was a problem hiding this comment.
The PR replaces the JDK's number-formatting helpers with JS intrinsics: DecimalDigits.stringSize/uncheckedGetChars for int and long, and DoubleToDecimal/FloatToDecimal toString/putDecimal. It adds a putBefore helper, a shortest-decimal algorithm with Java's layout rules, and wires the intrinsics into jit.ts PURE and natives/index.ts. A new test program NumberStrings.java is added. The diff is self-contained and the reported HotSpot comparison testing is noted, but I could not verify the field/encoding assumptions or the full runtime wiring since the rest of the repo is not shown.
review-bot, model nvidia/nemotron-3.5-super-vl:free, verdict approve
…al and FloatToDecimal
…h.pow may differ by an ulp between platforms
667a358 to
fd781be
Compare
There was a problem hiding this comment.
Adds JS intrinsics for DecimalDigits, DoubleToDecimal and FloatToDecimal in src/natives/numbers.ts, wires them into src/natives/index.ts, and exposes DecimalDigits as PURE JS expressions in src/jit.ts, plus a new test/java/NumberStrings.java. The change replaces JDK long-arithmetic formatting with JS number/BigInt code producing the same characters.
One concrete defect: in exactly() the bound check d * 5 ** k <= 2 ** 53 is done in float64, so for d=9, k=22 the product 21457672119140625 cannot be represented exactly and the rounded comparison can misclassify the boundary. Use BigInt for that check, as already agreed with the author.
review-bot, model nvidia/nemotron-3.5-super-vl:free, verdict approve
What this changes
Formatting a number is 2 to 8 times faster:
Double.toStringtakes about 0.8 µs instead of 6.2 µs,Long.toString0.5 µs instead of 1.7 µs. The JDK's formatting helpers run inlongarithmetic, which compiled code does with BigInt, so their bodies are now intrinsics insrc/natives/numbers.tsthat produce the same characters:DecimalDigits.stringSizeanduncheckedGetCharsLatin1/uncheckedGetCharsUTF16forintandlong, whichInteger.toString,Long.toString,StringBuilder.appendandString.valueOfuse. Compiled code calls them as JS expressions, as 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 does for other natives.DoubleToDecimalandFloatToDecimaltoStringandputDecimal, whichDouble.toString,Float.toString,StringBuilder.appendand string concatenation use. They take the engine's shortest round-trip digits for a double (and search the length for a float), then apply what Java's specification adds: when the shortest decimal has one digit, the closest decimal of one or two digits wins (Double.MIN_VALUEis4.9E-324, not5.0E-324), ties go to the even digit, and the layout is plain from 10^-3 to 10^7 andd.dddEnotherwise. Exact BigInt arithmetic decides only those cases and a float whose parse lands halfway between two floats.Closes #144. What is left of each call is the String allocation and the call itself, which any java-web call pays.
Tests
Double.toStringandFloat.toStringon HotSpot character for character.test/java/NumberStrings.java: edge values, powers of two and ten, and 100,000 random doubles, floats and longs throughtoString,String.valueOf,StringBuilder.appendinto Latin-1 and UTF-16 builders, and concatenation, hot enough to compile. It prints the same as HotSpot.snapshot,warm-snapshot,natives,librarytests pass.pnpm typecheck.Double.toString(i * 0.37)Float.toString(i * 0.37f)Long.toString(i * 1234567L)Integer.toString(i + 100000)"id=" + i + " v=" + d + " ok=" + z + " c=" + ch