Skip to content

Calls across stack chunks perform badly #142183

Open

Description

@markshannon

The Python stack is composed of a series of chunks of memory. These chunks are large, so we cross the boundaries infrequently and assume that it will never be on the fast path.

Unfortunately, it is possible to cross the boundary repeatedly if looping deep in the stack.

We should adjust the stack when crossing the boundary, to avoid doing so repeatedly.

Possible options include:

  1. Move the caller to the new chunk, so repeated calls do not cross the boundary.
  2. Instead of using a chunked stack, double the size of the stack and copy the old stack.

Both of these assume that we can move the frame. This is only possible if there are no pointers into the frame.
If a frames calls a native function taking an array of arguments, then there will be pointers into the frame.
So if we do any moving, we need to check for these pointers.

While we cannot move the frame, we can copy it, as long as the original remains. This is a bit wasteful, but shouldn't happen too often or waste too much space.

Linked PRs

Activity

  1. added 2 commits that reference this issue on Mar 18, 2026
  2. added a commit that references this issue on Mar 24, 2026
  3. added a commit that references this issue on Apr 17, 2026
  4. added a commit that references this issue on Apr 25, 2026
  5. added a commit that references this issue on Apr 27, 2026
  6. added 3 commits that reference this issue on May 8, 2026
  7. added a commit that references this issue on Jun 2, 2026
  8. maurycy commented on Aug 3, 2026

    @maurycy
    Contributor

    @markshannon Maybe just specialize call sites that hit the boundary? Naively: _CHECK_STACK_SPACE miss? CALL_PY_EXACT_ARGS -> CALL_PY_EXACT_ARGS_CACHED_STACK, CALL_BOUND_METHOD_EXACT_ARGS -> CALL_BOUND_METHOD_EXACT_ARGS_CACHED_STACK.

    cc @pablogsal @dpdani

  9. markshannon commented on Aug 4, 2026

    @markshannon
    MemberAuthor

    Maybe just specialize call sites that hit the boundary? Naively: _CHECK_STACK_SPACE miss? CALL_PY_EXACT_ARGS -> CALL_PY_EXACT_ARGS_CACHED_STACK, CALL_BOUND_METHOD_EXACT_ARGS -> CALL_BOUND_METHOD_EXACT_ARGS_CACHED_STACK.

    Can you explain. I have no idea what you mean by "just specialize call sites that hit the boundary?"

  10. maurycy commented on Aug 4, 2026

    @maurycy
    Contributor

    @markshannon Yes. I don't have a patch ready, and this is a rough idea. I never worked in this code, so likely I'm missing something very obvious here.

    That's what I mean by _CHECK_STACK_SPACE miss:

    DEOPT_IF(!_PyThreadState_HasStackSpace(tstate, code->co_framesize));

    Instead, do something like (naive, just to sketch the idea):

    if (!_PyThreadState_HasStackSpace(tstate, code->co_framesize)) {
      /* TODO: Check if remaining recursion */
    
      uint8_t op = frame->instr_ptr->op.code;
    
      if (op == CALL_PY_EXACT_ARGS) {
        set_opcode(instr, CALL_PY_EXACT_ARGS_CACHED_STACK); /* an example; more likely: specialize_...() ? */
      } else if (op == CALL_BOUND_METHOD_EXACT_ARGS) {
        set_opcode(instr, CALL_BOUND_METHOD_EXACT_ARGS_CACHED_STACK); /* an example; more likely: specialize_...() ? */
      }
    
      DEOPT();
    }

    Then, both CALL_PY_EXACT_ARGS_CACHED_STACK and CALL_BOUND_METHOD_EXACT_ARGS_CACHED_STACK could either continue if there's stack space or try to use tstate->datastack_cached_chunk. (Probably by introducing _CHECK_CACHED_STACK_SPACE or similar?)

    This doesn't remove the edge but should reduce the deopts, and avoid changing this for everyone.

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.15bugs and security fixesinterpreter-core(Objects, Python, Grammar, and Parser dirs)performancePerformance or resource usage

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions