Skip to content

Commit c74970a

Browse files
asvetlovclaude
andcommitted
Keep the lazy coroutine path out of clear_thread_frame()
clear_thread_frame() runs on every return from a Python function. With the lazy coroutine handling inline it was no longer inlined into _PyEval_FrameClearAndPop() and kept a value across the frame pop, adding about 9 instructions per return. Move that handling into a separate non-inlined function. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent d3251e7 commit c74970a

1 file changed

Lines changed: 32 additions & 27 deletions

File tree

‎Python/ceval.c‎

Lines changed: 32 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2005,47 +2005,52 @@ initialize_locals(PyThreadState *tstate, PyFunctionObject *func,
20052005
return -1;
20062006
}
20072007

2008-
/* Pop the exception state that RETURN_GENERATOR gave a lazily started
2009-
* coroutine body, restoring the caller's. Returns the body's exception,
2010-
* if any (a new reference). */
2011-
static PyObject *
2012-
pop_lazy_coro_exc_state(PyThreadState *tstate, _PyInterpreterFrame *frame)
2008+
static inline void
2009+
clear_and_pop_thread_frame(PyThreadState *tstate, _PyInterpreterFrame *frame)
20132010
{
2014-
assert(frame->lazy_coro);
2011+
// Make sure that this is, indeed, the top frame. We can't check this in
2012+
// _PyThreadState_PopFrame, since f_code is already cleared at that point:
2013+
assert((PyObject **)frame + _PyFrame_GetCode(frame)->co_framesize ==
2014+
tstate->datastack_top);
2015+
assert(frame->frame_obj == NULL || frame->frame_obj->f_frame == frame);
2016+
_PyFrame_ClearExceptCode(frame);
2017+
PyStackRef_CLEAR(frame->f_executable);
2018+
_PyThreadState_PopFrame(tstate, frame);
2019+
}
2020+
2021+
/* A lazily started coroutine body finished without creating its coroutine
2022+
* object (see RETURN_GENERATOR). Kept out of clear_thread_frame(), which
2023+
* every return from a Python function goes through. */
2024+
static Py_NO_INLINE void
2025+
clear_lazy_coro_frame(PyThreadState *tstate, _PyInterpreterFrame *frame)
2026+
{
2027+
// Pop the body's exception state, restoring the caller's.
20152028
_PyErr_StackItem *exc_info = tstate->exc_info;
20162029
tstate->exc_info = exc_info->previous_item;
20172030
PyObject *exc_value = exc_info->exc_value;
20182031
_PyThreadState_PopFrame(tstate, (_PyInterpreterFrame *)exc_info);
20192032
frame->lazy_coro = 0;
2020-
return exc_value;
2033+
/* Tell the caller's END_AWAITED_CALL through the slot that the compiler
2034+
* pushed below the awaited call. */
2035+
_PyInterpreterFrame *caller = frame->previous;
2036+
assert(PyStackRef_IsNull(caller->stackpointer[-1]));
2037+
caller->stackpointer[-1] = PyStackRef_TagInt(0);
2038+
// Like clear_gen_frame(): a frame object that outlives the
2039+
// coroutine must not keep the caller's frame alive via f_back.
2040+
frame->previous = NULL;
2041+
clear_and_pop_thread_frame(tstate, frame);
2042+
Py_XDECREF(exc_value);
20212043
}
20222044

20232045
static void
20242046
clear_thread_frame(PyThreadState *tstate, _PyInterpreterFrame * frame)
20252047
{
20262048
assert(frame->owner == FRAME_OWNED_BY_THREAD);
2027-
PyObject *lazy_exc = NULL;
20282049
if (frame->lazy_coro) {
2029-
lazy_exc = pop_lazy_coro_exc_state(tstate, frame);
2030-
/* The body finished without creating its coroutine object: tell
2031-
* the caller's END_AWAITED_CALL through the slot that the compiler
2032-
* pushed below the awaited call. */
2033-
_PyInterpreterFrame *caller = frame->previous;
2034-
assert(PyStackRef_IsNull(caller->stackpointer[-1]));
2035-
caller->stackpointer[-1] = PyStackRef_TagInt(0);
2036-
// Like clear_gen_frame(): a frame object that outlives the
2037-
// coroutine must not keep the caller's frame alive via f_back.
2038-
frame->previous = NULL;
2050+
clear_lazy_coro_frame(tstate, frame);
2051+
return;
20392052
}
2040-
// Make sure that this is, indeed, the top frame. We can't check this in
2041-
// _PyThreadState_PopFrame, since f_code is already cleared at that point:
2042-
assert((PyObject **)frame + _PyFrame_GetCode(frame)->co_framesize ==
2043-
tstate->datastack_top);
2044-
assert(frame->frame_obj == NULL || frame->frame_obj->f_frame == frame);
2045-
_PyFrame_ClearExceptCode(frame);
2046-
PyStackRef_CLEAR(frame->f_executable);
2047-
_PyThreadState_PopFrame(tstate, frame);
2048-
Py_XDECREF(lazy_exc);
2053+
clear_and_pop_thread_frame(tstate, frame);
20492054
}
20502055

20512056
static void

0 commit comments

Comments
 (0)