Repository navigation
perf(strings): grew a string appended by 's += x' in place - #13
Merged
Merged
Conversation
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).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 offeat/py), so the same commit applies tomasterand tofeat/pywithout conflicts, alone or with the other PRs of this series.make test,tests/py/interop/run.shand 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, onmasterand onfeat/py(plus perf(arrays): O(1)shift()on fast arrays for feat/py #10 for the array PRs).master+ the series: 44/52778 errors, as onmaster(the same 3 host time zone failures); the same withCONFIG_CHECK_TDZ=y, without abort.master+ the series:test_language,test_builtin,test_loop,test_closure,test_bigint,test_decorators,test_usingand test262language/(0/23162 errors).feat/pyitself needs665e816(gcc 15 / glibc fixes frommaster) to build here; it was added for testing only.Open in [code]smith · Need help on this PR? Tag
@codesmith-botwith what you need.