Skip to content

perf: format numbers in JS for the JDK's DecimalDigits, DoubleToDecimal and FloatToDecimal - #147

Merged
Project516 merged 2 commits into
masterfrom
p516/perf-number-strings
Dec 5, 2026
Merged

Project516 merged 2 commits into
masterfrom
p516/perf-number-strings

Conversation

@Project516

Copy link
Copy Markdown
Owner

What this changes

Formatting a number is 2 to 8 times faster: Double.toString takes about 0.8 µs instead of 6.2 µs, Long.toString 0.5 µs instead of 1.7 µs. The JDK's formatting helpers run in long arithmetic, which compiled code does with BigInt, so their bodies are now intrinsics in src/natives/numbers.ts that produce the same characters:

  • DecimalDigits.stringSize and uncheckedGetCharsLatin1 / uncheckedGetCharsUTF16 for int and long, which Integer.toString, Long.toString, StringBuilder.append and String.valueOf use. 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.
  • DoubleToDecimal and FloatToDecimal toString and putDecimal, which Double.toString, Float.toString, StringBuilder.append and 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_VALUE is 4.9E-324, not 5.0E-324), ties go to the even digit, and the layout is plain from 10^-3 to 10^7 and d.dddEn otherwise. 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

  • 508,166 doubles and floats (every power of two and its neighbors, powers of ten and neighbors, subnormals, edge values, 400,000 random bit patterns and 100,000 random magnitudes) formatted by the new code match Double.toString and Float.toString on HotSpot character for character.
  • New program test/java/NumberStrings.java: edge values, powers of two and ten, and 100,000 random doubles, floats and longs through toString, String.valueOf, StringBuilder.append into Latin-1 and UTF-16 builders, and concatenation, hot enough to compile. It prints the same as HotSpot.
  • Program tests compared with HotSpot: NumberStrings, Numbers, IndyConcat, StringConcat, StringBuilders, Strings, JitLoops.
  • snapshot, warm-snapshot, natives, library tests pass. pnpm typecheck.
  • Benchmark, 100,000 calls per round under Node, last round of 3 alternating runs each (median, range):
master this branch HotSpot 25
Double.toString(i * 0.37) 6171 ns (6152 to 6205) 809 ns (808 to 867) 93 ns
Float.toString(i * 0.37f) 4639 ns (4590 to 5900) 2841 ns (2799 to 6552)
Long.toString(i * 1234567L) 1697 ns (1671 to 1706) 542 ns (540 to 544) 39 ns
Integer.toString(i + 100000) 878 ns (877 to 996) 307 ns (307 to 308)
"id=" + i + " v=" + d + " ok=" + z + " c=" + ch 6185 ns (6078 to 6247) 1504 ns (1442 to 1867)

@coderabbitai

coderabbitai Bot commented Dec 5, 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 23 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: 9947f030-5d90-4008-b92a-f3188d63d9f5
Commits

Reviewing files that changed from the base of the PR and between 78ac5c7 and fd781be.

Files selected for processing: 4

  • src/jit.ts
  • src/natives/index.ts
  • src/natives/numbers.ts
  • test/java/NumberStrings.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 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

Comment thread src/natives/numbers.ts Resolved

@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 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

@Project516
Project516 force-pushed the p516/perf-number-strings branch from 667a358 to fd781be Compare December 5, 2026 01:46

@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.

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

@Project516
Project516 enabled auto-merge (squash) December 5, 2026 01:50
@Project516
Project516 merged commit 710ae77 into master Dec 5, 2026
16 checks passed
@Project516
Project516 deleted the p516/perf-number-strings branch December 5, 2026 01:54
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.

perf: Double.toString and Long.toString take 1 to 6 µs in compiled code, 40 to 70 times HotSpot

1 participant