Repository navigation
perf(arrays): unshifted fast arrays in amortized O(1) - #16
Merged
Merged
Conversation
Array.prototype.unshift() took the generic path for every array: it moved all the elements with JS_CopySubArray(), so building an array with unshift() was O(n^2) (80k elements: 1.6 s, now 2 ms) and a deque using unshift() and pop() paid the whole length at each step (1M steps at 1000 elements: 553 ms, now 59 ms). unshift() of a fast array now has the fast path of push() (same conditions: extensible, writable length equal to the element count). It stores the new elements in the free slots before the elements that shift() leaves (u.array.offset). When there are not enough of them, expand_fast_array_front() reallocates the block with argc + count / 2 free slots before the elements (a large block usually grows in place) and moves the elements after them, keeping the free slots after them, so a sequence of unshift() calls is amortized O(1). As with the 1.5 growth of push(), up to about half of the element count can stay unused before the elements of an array built with unshift(). expand_fast_array() keeps the free slots before the elements when push() grows the block, unless they are at least half as many as the elements. The test building a 200k element array with unshift() takes 6 ms and took 10 s.
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.
Array.prototype.unshift() took the generic path for every array: it
moved all the elements with JS_CopySubArray(), so building an array
with unshift() was O(n^2) (80k elements: 1.6 s, now 2 ms) and a deque
using unshift() and pop() paid the whole length at each step (1M steps
at 1000 elements: 553 ms, now 59 ms).
unshift() of a fast array now has the fast path of push() (same
conditions: extensible, writable length equal to the element count). It
stores the new elements in the free slots before the elements that
shift() leaves (u.array.offset). When there are not enough of them,
expand_fast_array_front() reallocates the block with argc + count / 2
free slots before the elements (a large block usually grows in place)
and moves the elements after them, keeping the free slots after them,
so a sequence of unshift() calls is amortized O(1). As with the 1.5
growth of push(), up to about half of the element count can stay unused
before the elements of an array built with unshift().
expand_fast_array() keeps the free slots before the elements when
push() grows the block, unless they are at least half as many as the
elements.
The test building a 200k element array with unshift() takes 6 ms and
took 10 s.
Testing
Based on the head of #10 (
dac2b87), whichmastercontains: it applies tomasternow and tofeat/pyonce #10 is merged there, alone or with the other PRs of this series. It uses the free slots before the elements that #10 introduced.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.