Skip to content

perf: join string concatenations in one native call that writes the text and arguments straight into the String - #145

Merged
Project516 merged 2 commits into
masterfrom
p516/perf-concat-join
Dec 5, 2026
Merged

Project516 merged 2 commits into
masterfrom
p516/perf-concat-join

Conversation

@Project516

Copy link
Copy Markdown
Owner

What this changes

String concatenation through StringConcatFactory, which javac emits by default, is 4.7 times faster for int arguments. java-web links each concatenation site to a generated class. That class used to build a StringBuilder chain, so each int went through DecimalDigits and each piece was copied twice. Now the generated concat method does three things:

  • It converts float, double and object arguments with String.valueOf in Java, in order, as the JDK's concatenation does, so toString calls, their exceptions and Java's number formatting are unchanged.
  • It calls a native join with the recipe text and the other arguments. join writes the text, the int, long, char and boolean values and the Strings straight into the new String's bytes, Latin-1 when every char fits and UTF-16 otherwise.
  • When every argument is a String, it keeps the StringBuilder chain, which the JIT already fuses into one copy.

Compiled code calls join directly, as #142 does for other natives. After a snapshot restore the VM binds join again by the hidden class's name. A recipe constant that contains the argument tag \u0001 falls back to the JDK's own bootstrap.

Closes #135. The cost left in the mixed case is Double.toString itself, filed as #144.

Tests

  • New program test/java/IndyConcat.java: every argument type with edge values, null, wide chars in text and arguments, float and double edge values, recipe constants, toString order and an exception from toString, run hot enough to compile. It prints the same as HotSpot.
  • Program tests compared with HotSpot: IndyConcat, StringConcat, StringBuilders, Strings, ImageStrings, Lambdas, Exceptions, JitLoops, Collections.
  • test/library.test.ts, bridge.test.ts, snapshot.test.ts, warm-snapshot.test.ts, warm-jit.test.ts: all pass.
  • pnpm typecheck.
  • Benchmark, 200,000 concatenations per round under Node, last round of 5 alternating runs each (median, range):
master this branch
"id=" + i + " name=" + s 2205 ns (2137 to 2326) 469 ns (447 to 496)
"id=" + i + " v=" + d + " ok=" + z + " c=" + ch 10114 ns (9380 to 15734) 6784 ns (6346 to 9833)
s + t, two Strings 403 ns (381 to 653) 399 ns (368 to 677)
a call to Integer.hashCode, for scale 90 ns (83 to 130) 104 ns (78 to 149)

@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 12 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: d10f5451-78dd-4b7e-9963-62ad742f9592
Commits

Reviewing files that changed from the base of the PR and between 82475d1 and 9c36a7f.

Files selected for processing: 6

  • src/classwriter.ts
  • src/indy.ts
  • src/jit.ts
  • src/jvm.ts
  • src/natives/strings.ts
  • test/java/IndyConcat.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 StringBuilder-based string concatenation in generated hidden classes with a native join that writes text and arguments directly into a new String's bytes. All-String concatenations keep the StringBuilder chain. A new join native is added to natives/strings.ts, bound in jvm.ts, inlined in jit.ts, and the class writer gains addNativeMethod. A new test test/java/IndyConcat.java covers argument types, edge values, nulls, wide chars, recipe constants, toString order and exceptions.

I found one defect in the diff itself: stringBuilderChain in src/indy.ts uses slot as both the parameter-array index and the local-variable index, incrementing it by one unconditionally. It only works because the call site guards it with an all-String check, so no parameter is wide; if that guard ever changes, long/double parameters would read the wrong locals. The joinCall function does this correctly with a separate descriptor iteration.

Other points are optional: chars << coder can overflow a signed 32-bit shift for very large results, and types.length counts bytes but is safe because the types string is always ASCII/Latin-1. The positions WeakMap cache keyed on the text String is safe because the text is a constant per concat site.


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

Comment thread src/indy.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 generated StringBuilder chain for most string concatenation sites with a hidden class containing a static concat method that converts float/double/object arguments in Java, then calls a native join writing text and primitive arguments directly into the new String's bytes. It adds addNativeMethod to ClassWriter, rewrites spinConcat in indy.ts, implements concatJoin in natives/strings.ts, wires it through jvm.ts bindNative and jit.ts trivialBody/intrinsic, and adds an IndyConcat test. I reviewed the diff for correctness issues.


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

@Project516
Project516 merged commit 7b4da41 into master Dec 5, 2026
16 checks passed
@Project516
Project516 deleted the p516/perf-concat-join branch December 5, 2026 01: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.

perf: string concatenation costs about 4.5 µs in compiled code, 150 times HotSpot

1 participant