Repository navigation
perf: join string concatenations in one native call that writes the text and arguments straight into the String - #145
Conversation
…ext and arguments straight into the String
|
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 12 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 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
…only concat chain
There was a problem hiding this comment.
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
What this changes
String concatenation through
StringConcatFactory, which javac emits by default, is 4.7 times faster forintarguments. java-web links each concatenation site to a generated class. That class used to build aStringBuilderchain, so eachintwent throughDecimalDigitsand each piece was copied twice. Now the generatedconcatmethod does three things:float,doubleand object arguments withString.valueOfin Java, in order, as the JDK's concatenation does, sotoStringcalls, their exceptions and Java's number formatting are unchanged.joinwith the recipe text and the other arguments.joinwrites the text, theint,long,charandbooleanvalues and the Strings straight into the new String's bytes, Latin-1 when every char fits and UTF-16 otherwise.StringBuilderchain, which the JIT already fuses into one copy.Compiled code calls
joindirectly, as #142 does for other natives. After a snapshot restore the VM bindsjoinagain by the hidden class's name. A recipe constant that contains the argument tag\u0001falls back to the JDK's own bootstrap.Closes #135. The cost left in the mixed case is
Double.toStringitself, filed as #144.Tests
test/java/IndyConcat.java: every argument type with edge values, null, wide chars in text and arguments,floatanddoubleedge values, recipe constants,toStringorder and an exception fromtoString, run hot enough to compile. It prints the same as HotSpot.test/library.test.ts,bridge.test.ts,snapshot.test.ts,warm-snapshot.test.ts,warm-jit.test.ts: all pass.pnpm typecheck."id=" + i + " name=" + s"id=" + i + " v=" + d + " ok=" + z + " c=" + chs + t, two StringsInteger.hashCode, for scale