Skip to content

Streamline spilling around calls in the code generator #150516

Closed

Description

@markshannon

So that the GC can see all objects on the stack, any call that escapes and could thus potentially trigger the GC, must be wrapped in code to spill values to the stack and save the stack pointer.

Prior to tailcalling, it made sense to save and reload the stack pointer as the stack pointer would have usually been spilled anyway. Now that we use the preserve none calling convention, the stack pointer will not be spilled so reloading is redundant.

We can drop the reload and not adjust the stack pointer, merely saving the adjusted value.
For example, instead of:

    stack_pointer[0] = foo;
    stack_pointer[1] = bar;
    stack_pointer += 2;
    _PyFrame_SetStackPointer(frame, stack_pointer);
    escaping_call();
    stack_pointer = _PyFrame_GetStackPointer(frame);
    ...
    stack_pointer += -2;

we can do this:

    stack_pointer[0] = foo;
    stack_pointer[1] = bar;
    _PyFrame_SetStackPointer(frame, stack_pointer+2);
    escaping_call();
    ...

Which saves a few instructions on the hot path and reduces correction code on exit and error branches.

Linked PRs

Activity

  1. added
    performancePerformance or resource usage
    interpreter-core(Objects, Python, Grammar, and Parser dirs)
    3.16new features, bugs and security fixes
    on May 27, 2026
  2. added 4 commits that reference this issue on Jun 12, 2026
  3. added a commit that references this issue on Jun 18, 2026
  4. added a commit that references this issue on Jul 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.16new features, bugs and security fixesinterpreter-core(Objects, Python, Grammar, and Parser dirs)performancePerformance or resource usagetype-featureA feature request or enhancement

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions