Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GH-90081: Run python tracers at full speed #95328

Merged
merged 5 commits into from
Jul 28, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions Include/internal/pycore_pystate.h
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,9 @@ PyAPI_FUNC(void) _PyThreadState_DeleteExcept(
static inline void
_PyThreadState_UpdateTracingState(PyThreadState *tstate)
{
int use_tracing = (tstate->c_tracefunc != NULL
|| tstate->c_profilefunc != NULL);
bool use_tracing =
(tstate->tracing == 0) &&
(tstate->c_tracefunc != NULL || tstate->c_profilefunc != NULL);
tstate->cframe->use_tracing = (use_tracing ? 255 : 0);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Run Python code in tracer/profiler function at full speed. Fixes slowdown in
earlier versions of 3.11.
12 changes: 7 additions & 5 deletions Python/ceval.c
Original file line number Diff line number Diff line change
Expand Up @@ -5631,7 +5631,7 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, _PyInterpreterFrame *frame, int
assert(oparg);
oparg <<= 8;
oparg |= _Py_OPARG(*next_instr);
// We might be tracing. To avoid breaking tracing guarantees in
// We might be tracing. To avoid breaking tracing guarantees in
// quickened instructions, always deoptimize the next opcode:
opcode = _PyOpcode_Deopt[_Py_OPCODE(*next_instr)];
PRE_DISPATCH_GOTO();
Expand Down Expand Up @@ -5661,9 +5661,9 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, _PyInterpreterFrame *frame, int
case DO_TRACING:
#endif
{
if (tstate->tracing == 0 &&
INSTR_OFFSET() >= frame->f_code->_co_firsttraceable
) {
assert(cframe.use_tracing);
assert(tstate->tracing == 0);
if (INSTR_OFFSET() >= frame->f_code->_co_firsttraceable) {
int instr_prev = _PyInterpreterFrame_LASTI(frame);
frame->prev_instr = next_instr;
TRACING_NEXTOPARG();
Expand Down Expand Up @@ -6875,12 +6875,15 @@ void
PyThreadState_EnterTracing(PyThreadState *tstate)
{
tstate->tracing++;
tstate->cframe->use_tracing = 0;
}

void
PyThreadState_LeaveTracing(PyThreadState *tstate)
{
assert(tstate->tracing > 0 && tstate->cframe->use_tracing == 0);
tstate->tracing--;
_PyThreadState_UpdateTracingState(tstate);
}

static int
Expand Down Expand Up @@ -6919,7 +6922,6 @@ _PyEval_CallTracing(PyObject *func, PyObject *args)
int save_tracing = tstate->tracing;
int save_use_tracing = tstate->cframe->use_tracing;
tstate->tracing = 0;

// Call the tracing function
PyObject *result = PyObject_Call(func, args, NULL);

Expand Down