You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
Move the caller to the new chunk, so repeated calls do not cross the boundary.
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.
@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.
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?"
@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.
Instead, do something like (naive, just to sketch the idea):
if (!_PyThreadState_HasStackSpace(tstate, code->co_framesize)) {
/* TODO: Check if remaining recursion */uint8_top=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_...() ? */
} elseif (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.
Activity
[3.14] gh-142183: Cache one datachunk per tstate to prevent alloc/dea…
pythongh-142183: Cache one datachunk per tstate to prevent alloc/deal…
Cache one datachunk per tstate to prevent alloc/dealloc thrashing
Cache one datachunk per tstate to prevent alloc/dealloc thrashing
[3.13] gh-142183: Cache one datachunk per tstate to prevent alloc/dea…
pythongh-142183: Change data stack to use a resizable array
pythongh-142183: Cache one datachunk per tstate to prevent alloc/deal…
Merge branch 'main' into pythongh-142183-stack-resizable-array
Merge branch 'main' into pythongh-142183-stack-resizable-array
Merge branch 'main' into pythongh-142183-stack-resizable-array
Merge branch 'main' into pythongh-142183-stack-resizable-array
Merge branch 'main' into pythongh-142183-stack-resizable-array
maurycy commentedon Aug 3, 2026
@markshannon Maybe just specialize call sites that hit the boundary? Naively:
_CHECK_STACK_SPACEmiss?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
markshannon commentedon Aug 4, 2026
Can you explain. I have no idea what you mean by "just specialize call sites that hit the boundary?"
maurycy commentedon Aug 4, 2026
@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_SPACEmiss:cpython/Python/bytecodes.c
Line 4600 in 416c346
Instead, do something like (naive, just to sketch the idea):
Then, both
CALL_PY_EXACT_ARGS_CACHED_STACKandCALL_BOUND_METHOD_EXACT_ARGS_CACHED_STACKcould either continue if there's stack space or try to usetstate->datastack_cached_chunk. (Probably by introducing_CHECK_CACHED_STACK_SPACEor similar?)This doesn't remove the edge but should reduce the deopts, and avoid changing this for everyone.
[TPU/Compile] Fix warmup slowdown from CPython 3.12 frame-stack churn
[TPU/Compile] Fix warmup slowdown from CPython 3.12 frame-stack churn
[TPU/Compile] Fix warmup slowdown from CPython 3.12 frame-stack churn (…
build(bench): micro-benchmarks run on the newest Python, in a nox ses…
[None][perf] Run the executor loop under a CPython frame-stack reserve
[None][perf] Run the executor loop under a CPython frame-stack reserve
[None][perf] Run the executor loop under a CPython frame-stack reserve
[None][perf] Run the executor loop under a CPython frame-stack reserve
[None][perf] Run the executor loop under a CPython frame-stack reserve