Skip to content

GH-150516: Reduce the work done to spill and reload the stack around calls#151587

Open
markshannon wants to merge 4 commits into
python:mainfrom
markshannon:streamline-stack-spilling
Open

GH-150516: Reduce the work done to spill and reload the stack around calls#151587
markshannon wants to merge 4 commits into
python:mainfrom
markshannon:streamline-stack-spilling

Conversation

@markshannon

@markshannon markshannon commented Jun 17, 2026

Copy link
Copy Markdown
Member

Avoids needless spilling of the stack around calls, as described in the issue.

Also adds the stackpointer_valid to frames in the debug build to verify that the frame's stack pointer is not used when it shouldn't be. frame->stackpointer_valid is equivalent to the old frame->stackpointer != NULL check.

The new scheme is more precise, so I needed to change a few stack saving/syncing/reloading operations to better reflect the true semantics.

@markshannon

Copy link
Copy Markdown
Member Author

Skipping news as this has no impact on any users

@eendebakpt

Copy link
Copy Markdown
Contributor

Here is a latent issue:

inst(TEST_LOOP_SPILL, (a -- a)) {
    int n = (int)oparg;
    for (int i = 0; i < n; i++) {
        escaping_inside_loop(a);
    }
    escaping_after_loop(a);
}

The generator ellides the _PyFrame_SetStackPointer for the escaping_after_loop, which can be a problem if n is zero.

@markshannon

Copy link
Copy Markdown
Member Author

That's a pre-existing issue where we don't merge stacks properly at the end of for loops.
It needs fixing though, so I've added a fix for that here.

@eendebakpt

Copy link
Copy Markdown
Contributor

For proper benchmarking of this PR a PGO build is needed. I did a subset of 19 loop-heavy benchmarks from pyperformance and got +1.9% geomean (biggest win is deltablue at 1.10x).

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