Skip to content

gh-142183: Change data stack to use a resizable array#148681

Open
dpdani wants to merge 2 commits intopython:mainfrom
dpdani:gh-142183-stack-resizable-array
Open

gh-142183: Change data stack to use a resizable array#148681
dpdani wants to merge 2 commits intopython:mainfrom
dpdani:gh-142183-stack-resizable-array

Conversation

@dpdani
Copy link
Copy Markdown
Contributor

@dpdani dpdani commented Apr 17, 2026

This PR changes the implementation of the Python stack to use a resizable array. This avoids the problem of calls that frequently cause the datastack_top (now called stack_top) pointer to switch between allocations.

After resizing, previous array allocations are not immediately freed because that would cause issues for various bits around the VM still pointing into them, and are instead freed along with the tstate.

During resizing, the previous contents of the stack are not copied into the new allocations, and instead the memory of the previous allocation is still used. Subsequently, popping and pushing frames, the new frames will always be residing on the new stack chunk allocation.

Overall it results in a ±1% performance change (within the noise range), but it avoids degenerate cases for any number of frames. I am also told it would allow further optimizations in the JIT.

@python-cla-bot
Copy link
Copy Markdown

python-cla-bot bot commented Apr 17, 2026

All commit authors signed the Contributor License Agreement.

CLA signed

@pablogsal pablogsal self-assigned this Apr 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants