Skip to content

Commit b8b2990

Browse files
authored
pythonGH-90081: Run python tracers at full speed (pythonGH-95328)
1 parent ea269b9 commit b8b2990

File tree

3 files changed

+12
-6
lines changed

3 files changed

+12
-6
lines changed

Include/internal/pycore_pystate.h

+3-2
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,9 @@ PyAPI_FUNC(void) _PyThreadState_DeleteExcept(
131131
static inline void
132132
_PyThreadState_UpdateTracingState(PyThreadState *tstate)
133133
{
134-
int use_tracing = (tstate->c_tracefunc != NULL
135-
|| tstate->c_profilefunc != NULL);
134+
bool use_tracing =
135+
(tstate->tracing == 0) &&
136+
(tstate->c_tracefunc != NULL || tstate->c_profilefunc != NULL);
136137
tstate->cframe->use_tracing = (use_tracing ? 255 : 0);
137138
}
138139

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Run Python code in tracer/profiler function at full speed. Fixes slowdown in
2+
earlier versions of 3.11.

Python/ceval.c

+7-4
Original file line numberDiff line numberDiff line change
@@ -5631,7 +5631,7 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, _PyInterpreterFrame *frame, int
56315631
assert(oparg);
56325632
oparg <<= 8;
56335633
oparg |= _Py_OPARG(*next_instr);
5634-
// We might be tracing. To avoid breaking tracing guarantees in
5634+
// We might be tracing. To avoid breaking tracing guarantees in
56355635
// quickened instructions, always deoptimize the next opcode:
56365636
opcode = _PyOpcode_Deopt[_Py_OPCODE(*next_instr)];
56375637
PRE_DISPATCH_GOTO();
@@ -5661,9 +5661,9 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, _PyInterpreterFrame *frame, int
56615661
case DO_TRACING:
56625662
#endif
56635663
{
5664-
if (tstate->tracing == 0 &&
5665-
INSTR_OFFSET() >= frame->f_code->_co_firsttraceable
5666-
) {
5664+
assert(cframe.use_tracing);
5665+
assert(tstate->tracing == 0);
5666+
if (INSTR_OFFSET() >= frame->f_code->_co_firsttraceable) {
56675667
int instr_prev = _PyInterpreterFrame_LASTI(frame);
56685668
frame->prev_instr = next_instr;
56695669
TRACING_NEXTOPARG();
@@ -6875,12 +6875,15 @@ void
68756875
PyThreadState_EnterTracing(PyThreadState *tstate)
68766876
{
68776877
tstate->tracing++;
6878+
tstate->cframe->use_tracing = 0;
68786879
}
68796880

68806881
void
68816882
PyThreadState_LeaveTracing(PyThreadState *tstate)
68826883
{
6884+
assert(tstate->tracing > 0 && tstate->cframe->use_tracing == 0);
68836885
tstate->tracing--;
6886+
_PyThreadState_UpdateTracingState(tstate);
68846887
}
68856888

68866889
static int

0 commit comments

Comments
 (0)