perf(hir): constant-fold adjacent ASCII string literals (8.11→1.25 ns) - #9145
Conversation
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (1)
Included review availability: Your plan provides up to 8 included reviews per hour; 2 remain after this review. 📝 WalkthroughWalkthroughChangesASCII string concatenation folding
Estimated code review effort: 2 (Simple) | ~10 minutes Merge Risk: ⚪ Minimal · up to This change folds eligible adjacent ASCII string literals during compilation while preserving existing behavior for dynamic and non-ASCII values; no actionable merge-blocking risk remains beyond normal checks and review. Suggested reviewers: 🚥 Pre-merge checks | ✅ 3 | ❌ 2❌ Failed checks (2 warnings)
✅ Passed checks (3 passed)
Full details: Description checkExplanation The description explains the optimization, scope, benchmarks, and correctness coverage, but it does not use the required template sections. It omits the required Summary, Changes, Related issue, Test plan, and Checklist sections, and it leaves the test gates as a placeholder. Resolution Rewrite the description using the repository template. Add Summary, Changes, Related issue with an issue reference or "n/a", Test plan with completed verification commands and checkboxes, Screenshots / output if applicable, and Checklist with the applicable items checked. Replace "Gates: (placeholder)" with the actual test results.
✨ Finishing Touches🧪 Generate unit tests (beta)
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. Comment |
|
Full battery: |
Two string literals next to each other are a compile-time constant, but
nothing downstream could fold them: the pair lowers to a runtime concat
call, and LLVM — where numeric literal folding happens — cannot see
through it. Measured on the mini: "id-" + "x" cost 8.11 ns per
evaluation and "a" + "b" + "c" cost 19.71 (the cost scales with the
number of literals), against 0.49 and 0.52 in node; a single string
literal is 1.25 and 3 + 4 folds to the empty-loop baseline.
Fold in the HIR Add arm when both operands are string literals:
"id-" + "x" 8.11 -> 1.25 ns (= single-literal baseline)
"a" + "b" + "c" 19.71 -> 1.56 ns
Left-associativity folds whole chains for free: "a" + "b" + "c" parses
as ("a" + "b") + "c", so the inner pair is already String("ab") when the
outer one runs.
ASCII-only, deliberately. A non-ASCII pair needs the runtime's WTF-8
rules — canonicalize_surrogate_pairs merges a high/low surrogate pair
ACROSS the join into one astral code point (PerryTS#6728), and utf16_len /
isWellFormed depend on that — so those keep the runtime path rather than
have the compiler restate the rules.
Differential vs node (folded and unfolded pairs, empty strings, mixed
literal/variable, number/boolean/null/undefined coercions, accented and
CJK pairs, an astral surrogate pair, a lone surrogate, escapes and quotes
inside literals, charCodeAt/slice/indexOf/repeat on the result, computed
property keys, the in operator, object indexing, switch discriminant,
template literals, array join): identical to a build without the fold,
and identical to node except one pre-existing lone-surrogate print that
main produces too.
Claude-Session: https://claude.ai/code/session_01F1dt1jfzK2cheMZyus6y6p
9e7733f to
d691c52
Compare
|
Merged via a merge train — cherry-picked with two other PRs onto one branch and validated together in a single build. Final validation: hir 365 passed, codegen 1356, runtime 2831 passed (exit 0, 0 abort markers), perry --bins 1066, The train initially also carried #9140 (tombstone reuse for small-object churn), which failed four delete/shape-transition tests on their own premise ( |
What
Two adjacent string literals are a compile-time constant, but nothing
downstream could fold them: the pair lowers to a runtime concat call, and LLVM —
where numeric literal folding happens — cannot see through it. So
3 + 4coststhe empty loop while
"a" + "b"ran a real concat on every evaluation, andthe cost scaled with the number of literals.
Mac mini, ns/op (loop reads
.lengthso nothing is dead-code eliminated):"id-" + "x""a" + "b" + "c""id-x"(single literal, reference point)3 + 4(numeric fold, reference point)`id-${"x"}`(template — separate fold, untouched)The folded rows land on the single-literal baseline, which is the point: the
concatenation disappears entirely rather than getting cheaper.
Left-associativity folds whole chains for free —
"a" + "b" + "c"parses as("a" + "b") + "c", so the inner pair is alreadyString("ab")when the outerone runs.
ASCII-only, deliberately
A non-ASCII pair needs the runtime's WTF-8 rules:
canonicalize_surrogate_pairsmerges a high/low surrogate pair across the join into one astral code point
(#6728), and
utf16_len/isWellFormeddepend on that. Node agrees —"\uD83D" + "\uDE00"is one emoji of length 2 that is well-formed, while"\uD800" + "x"is length 2 and is not. Rather than restate those rules in thecompiler, non-ASCII literals keep the runtime path they have today.
Correctness
Differential covering folded and unfolded pairs, empty strings, mixed
literal/variable operands, number/boolean/
null/undefinedcoercions, accentedand CJK pairs, an astral surrogate pair, a lone surrogate, escapes and quotes
inside literals,
charCodeAt/slice/indexOf/repeaton the result, computedproperty keys, the
inoperator, object indexing, aswitchdiscriminant,template literals and
Array#join: identical to a build without the fold,and identical to node except one pre-existing lone-surrogate print that main
produces too (verified against a non-fold build of the same tree).
Gates: (placeholder)
https://claude.ai/code/session_01F1dt1jfzK2cheMZyus6y6p
Summary by CodeRabbit
+are now combined at compile time.