Skip to content

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

Merged
markshannon merged 5 commits into
python:mainfrom
markshannon:streamline-stack-spilling
Jun 18, 2026
Merged

GH-150516: Reduce the work done to spill and reload the stack around calls#151587
markshannon merged 5 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).

@markshannon

Copy link
Copy Markdown
Member Author

Our numbers for the whole pyperformance suite show a speedup, but less than 1%.

@markshannon

Copy link
Copy Markdown
Member Author

The fuzzers are producing compile errors that make no sense

@markshannon markshannon reopened this Jun 18, 2026

@diegorusso diegorusso left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has been reviewed internally and LGTM.

@markshannon markshannon merged commit ad1513a into python:main Jun 18, 2026
87 checks passed
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.

3 participants