Repository navigation
perf(arrays): O(1) shift() on fast arrays for feat/py - #10
Merged
Merged
Conversation
…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.
…py-array-shift-offset
can1357
added a commit
that referenced
this pull request
Nov 8, 2026
This was referenced Nov 8, 2026
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.
Brings #7 (O(1)
shift()on fast arrays) tofeat/py, plus the fix it needs there.On this branch a Python list is a JS fast array, and
python/listobject.ctakes over the elements buffer directly. After a JSshift()the buffer startsu.array.offsetslots beforevalues, whichlist.clear()andlist.sort()didn't know about. Merging #7 alone segfaults as soon as Python clears or sorts a shifted JS array.qp_lst_clear: freesfast_array_base(p)and resetsoffset.qp_list_sort: savesoffsetwith the detached buffer, keeps it 0 while the list is detached (a key function that appends no longer reallocsNULL - offset), and restores it afterwards. The error path and the replaced buffer free from their allocation start.listobject.csetoffset = 0explicitly.test_shifted_js_array()intests/py/interop/test_interop.pycoversclear(), JSshift()/pushaftersort(), 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 intofeat/py, git sees it as already present.feat/pytest_interop.pytest_interop.jsshift()extend/sort/clearThe 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/*.cneeded-include limits.h(INT_MAXundeclared), and the Python tests ran on.obj/qpy-nobasebecause buildingqpy-base.binfails withunregistered native code pointer.Open in [code]smith · Need help on this PR? Tag
@codesmith-botwith what you need.