Skip to content

perf(arrays): O(1) shift() on fast arrays for feat/py - #10

Merged
can1357 merged 3 commits into
stencil-hq:masterfrom
H4vC:fix/py-array-shift-offset
Nov 8, 2026
Merged

can1357 merged 3 commits into
stencil-hq:masterfrom
H4vC:fix/py-array-shift-offset

Conversation

@H4vC

@H4vC H4vC commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

Brings #7 (O(1) shift() on fast arrays) to feat/py, plus the fix it needs there.

On this branch a Python list is a JS fast array, and python/listobject.c takes over the elements buffer directly. After a JS shift() the buffer starts u.array.offset slots before values, which list.clear() and list.sort() didn't know about. Merging #7 alone segfaults as soon as Python clears or sorts a shifted JS array.

  • qp_lst_clear: frees fast_array_base(p) and resets offset.
  • qp_list_sort: saves offset with the detached buffer, keeps it 0 while the list is detached (a key function that appends no longer reallocs NULL - offset), and restores it afterwards. The error path and the replaced buffer free from their allocation start.
  • Slow → fast conversions in listobject.c set offset = 0 explicitly.
  • test_shifted_js_array() in tests/py/interop/test_interop.py covers clear(), JS shift()/push after sort(), and a sort whose key mutates the list from both sides.

The first commit is #7's own commit (2cc2532), so when #7 lands on master and master is merged into feat/py, git sees it as already present.

feat/py + #7 + #7 + fix
test_interop.py PASS (68 checks, before the new test) segfault PASS (72 checks)
test_interop.js PASS PASS PASS
JS tests (api, closure, language, builtin, loop, bjson, std) pass pass pass
Python L0 203/211 203/211 203/211
JS: drain 200k with shift() 4525 ms 8 ms 8 ms
Same drain on a JS array held by Python, then extend/sort/clear 4575 ms segfault 7 ms

The interop test and the drain also run clean under an AddressSanitizer build (leak detection off). The 8 L0 failures are the same without these changes (4 expected, 4 glibc/platform differences).

Tested on Linux (WSL, gcc 13.3), using two workarounds for problems already on feat/py: python/*.c needed -include limits.h (INT_MAX undeclared), and the Python tests ran on .obj/qpy-nobase because building qpy-base.bin fails with unregistered native code pointer.


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

H4vC added 3 commits November 3, 2026 17:09
…elements

Array.prototype.shift() moved all the elements of a fast array, so a queue built with push() and shift() was O(n^2). shift() now advances the element pointer and counts the free slots before it (u.array.offset, which fits in the padding of JSObject). expand_fast_array() moves the elements back to the start of the allocation when the free slots are at least half as many as the elements (at most 2 copied elements per shift() call), otherwise it reallocates the whole block and keeps them.
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