Skip to content

perf(hir): constant-fold adjacent ASCII string literals (8.11→1.25 ns) - #9145

Merged
proggeramlug merged 1 commit into
PerryTS:mainfrom
proggeramlug:perf/fold-literal-string-concat
Aug 30, 2026
Merged

perf(hir): constant-fold adjacent ASCII string literals (8.11→1.25 ns)#9145
proggeramlug merged 1 commit into
PerryTS:mainfrom
proggeramlug:perf/fold-literal-string-concat

Conversation

@proggeramlug

@proggeramlug proggeramlug commented Aug 30, 2026

Copy link
Copy Markdown
Contributor

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 + 4 costs
the empty loop while "a" + "b" ran a real concat on every evaluation, and
the cost scaled with the number of literals.

Mac mini, ns/op (loop reads .length so nothing is dead-code eliminated):

expression before after node
"id-" + "x" 8.11 1.25 0.49
"a" + "b" + "c" 19.71 1.56 0.52
"id-x" (single literal, reference point) 1.25 1.25 0.49
3 + 4 (numeric fold, reference point) 0.94 0.94 0.54
`id-${"x"}` (template — separate fold, untouched) 9.94 9.99 0.48

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 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
(#6728), and utf16_len / isWellFormed depend 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 the
compiler, 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/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, a switch discriminant,
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

  • Performance Improvements
    • Adjacent ASCII string literals joined with + are now combined at compile time.
    • Other string concatenation scenarios continue to work through the existing runtime behavior.

@coderabbitai

coderabbitai Bot commented Aug 30, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 1cc409df-e5a6-4d04-bc6a-f3b217e1c6b8

📥 Commits

Reviewing files that changed from the base of the PR and between 166d3ea and 9e7733f.

📒 Files selected for processing (1)
  • crates/perry-hir/src/lower/lower_expr/arm_bin.rs

Included review availability: Your plan provides up to 8 included reviews per hour; 2 remain after this review.


📝 Walkthrough

Walkthrough

Changes

ASCII string concatenation folding

Layer / File(s) Summary
Fold ASCII string literals during lowering
crates/perry-hir/src/lower/lower_expr/arm_bin.rs
The BinaryOp::Add arm folds two ASCII-only Expr::String operands into one compile-time string. Non-ASCII and other operand shapes use runtime concatenation.

Estimated code review effort: 2 (Simple) | ~10 minutes

Merge Risk: ⚪ Minimal · up to 9e773

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: thehypnoo

🚥 Pre-merge checks | ✅ 3 | ❌ 2

❌ Failed checks (2 warnings)

Check name Status Explanation Resolution
Description check ⚠️ Warning 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 … 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 wi…
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 1 functions across 1 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly identifies the main change: HIR constant folding for adjacent ASCII string literals. The performance result is relevant and concise.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Full details: Description check

Explanation

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.

  • Fix all pre-merge checks with AI
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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

Comment @coderabbitai help to get the list of available commands.

@proggeramlug

Copy link
Copy Markdown
Contributor Author

Full battery: -D warnings 0, codegen 1835/0, full runtime suite 2822/0, lints clean (census, addr-class audit, file-size, raw-handle debt none raised), integration issue_8655 2/2 / issue_8690 3/3 / issue_8897 3/3. Ready for review.

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
@proggeramlug
proggeramlug force-pushed the perf/fold-literal-string-concat branch from 9e7733f to d691c52 Compare August 30, 2026 08:43
@proggeramlug

Copy link
Copy Markdown
Contributor Author

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, run_lint_gates.sh all 60 gates passed, git diff origin/main --diff-filter=D empty.

The train initially also carried #9140 (tombstone reuse for small-object churn), which failed four delete/shape-transition tests on their own premise (test premise: the delete did not compact the slots). Dropping it made the rest green, so those failures are its alone — handed back separately. Mentioning it because if any of these three later look implicated in a delete-path regression, #9140 is the change to look at first.

@proggeramlug
proggeramlug merged commit d98c12e into PerryTS:main Aug 30, 2026
20 checks passed
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.

1 participant