Skip to content

perf(strings): grew a string appended by 's += x' in place - #13

Merged
can1357 merged 1 commit into
stencil-hq:masterfrom
H4vC:perf/string-append-grow
Nov 11, 2026
Merged

can1357 merged 1 commit into
stencil-hq:masterfrom
H4vC:perf/string-append-grow

Conversation

@H4vC

@H4vC H4vC commented Nov 8, 2026 •

Copy link
Copy Markdown
Contributor

OP_add_loc appended in place only within the slack of the allocation.
Past it, a string of 512 characters or more became a rope, and any read
of the string between two appends (charCodeAt(), indexOf(), a
comparison...) flattened the whole rope again: an append and a
charCodeAt() per iteration was O(n^2), 400k iterations took 2.4 s, and
with an indexOf() 7.8 s.

When the local variable holds the only reference to 's' (a string that
is not an atom, or a rope) and the result has at least
JS_STRING_GROW_MIN_LEN characters, js_concat_string_grow() makes it a
flat string allocated with 1/8 of headroom, the over-allocation of the
lists of CPython: appends are amortized O(1) and the unused part of a
finished string is bounded by 1/8 of its length. A flat string is
reallocated in place; a rope, or an 8 bit string receiving 16 bit
characters, is copied once into the new string, so that the following
appends take the fast path again instead of making ropes. No other
reference can see the change. string_buffer_end2() keeps the size of the
buffer for this case. A shared or short string, or a failed
reallocation, takes the previous path.

400k appends with a charCodeAt(): 2.4 s -> 15 ms; the test loops
(200k appends to a string, to a rope and after a widening, each with a
read) take 24 ms and took 6.4 s.

OP_add_loc is emitted for var and argument variables. 'let' variables
get it once their TDZ checks are elided after the declaration (separate
change).

Testing

Based on 68be18f (the tip of feat/py), so the same commit applies to master and to feat/py without conflicts, alone or with the other PRs of this series.

  • make test, tests/py/interop/run.sh and a short Python script (string indexing/iteration/concatenation, marshal, list pop/insert/clear/extend) pass with the Python build, on this PR alone and with the whole series, on master and on feat/py (plus perf(arrays): O(1) shift() on fast arrays for feat/py #10 for the array PRs).
  • test262 on master + the series: 44/52778 errors, as on master (the same 3 host time zone failures); the same with CONFIG_CHECK_TDZ=y, without abort.
  • AddressSanitizer build of master + the series: test_language, test_builtin, test_loop, test_closure, test_bigint, test_decorators, test_using and test262 language/ (0/23162 errors).
  • Linux (WSL, gcc 13.3). feat/py itself needs 665e816 (gcc 15 / glibc fixes from master) to build here; it was added for testing only.

Autofix with [code]smith
Open in [code]smith · Need help on this PR? Tag @codesmith-bot with what you need.

OP_add_loc appended in place only within the slack of the allocation.
Past it, a string of 512 characters or more became a rope, and any read
of the string between two appends (charCodeAt(), indexOf(), a
comparison...) flattened the whole rope again: an append and a
charCodeAt() per iteration was O(n^2), 400k iterations took 2.4 s, and
with an indexOf() 7.8 s.

When the local variable holds the only reference to 's' (a string that
is not an atom, or a rope) and the result has at least
JS_STRING_GROW_MIN_LEN characters, js_concat_string_grow() makes it a
flat string allocated with 1/8 of headroom, the over-allocation of the
lists of CPython: appends are amortized O(1) and the unused part of a
finished string is bounded by 1/8 of its length. A flat string is
reallocated in place; a rope, or an 8 bit string receiving 16 bit
characters, is copied once into the new string, so that the following
appends take the fast path again instead of making ropes. No other
reference can see the change. string_buffer_end2() keeps the size of the
buffer for this case. A shared or short string, or a failed
reallocation, takes the previous path.

400k appends with a charCodeAt(): 2.4 s -> 15 ms; the test loops
(200k appends to a string, to a rope and after a widening, each with a
read) take 24 ms and took 6.4 s.

OP_add_loc is emitted for var and argument variables. 'let' variables
get it once their TDZ checks are elided after the declaration (separate
change).
@can1357
can1357 merged commit 1e9d9c6 into stencil-hq:master Nov 11, 2026
17 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.

2 participants