Skip to content

perf(arrays): unshifted fast arrays in amortized O(1) - #16

Merged
can1357 merged 1 commit into
stencil-hq:masterfrom
H4vC:perf/array-unshift
Nov 11, 2026
Merged

can1357 merged 1 commit into
stencil-hq:masterfrom
H4vC:perf/array-unshift

Conversation

@H4vC

@H4vC H4vC commented Nov 8, 2026 •

Copy link
Copy Markdown
Contributor

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), which master contains: it applies to master now and to feat/py once #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.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.

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.
@can1357
can1357 merged commit 6475f80 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