Skip to content

Commit 00566a8

Browse files
GH-90081: Run python tracers at full speed (GH-95328) (#95363)
(cherry picked from commit b8b2990) Co-authored-by: Mark Shannon <[email protected]> Co-authored-by: Mark Shannon <[email protected]>
1 parent f06f365 commit 00566a8

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
@@ -5590,7 +5590,7 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, _PyInterpreterFrame *frame, int
55905590
assert(oparg);
55915591
oparg <<= 8;
55925592
oparg |= _Py_OPARG(*next_instr);
5593-
// We might be tracing. To avoid breaking tracing guarantees in
5593+
// We might be tracing. To avoid breaking tracing guarantees in
55945594
// quickened instructions, always deoptimize the next opcode:
55955595
opcode = _PyOpcode_Deopt[_Py_OPCODE(*next_instr)];
55965596
PRE_DISPATCH_GOTO();
@@ -5620,9 +5620,9 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, _PyInterpreterFrame *frame, int
56205620
case DO_TRACING:
56215621
#endif
56225622
{
5623-
if (tstate->tracing == 0 &&
5624-
INSTR_OFFSET() >= frame->f_code->_co_firsttraceable
5625-
) {
5623+
assert(cframe.use_tracing);
5624+
assert(tstate->tracing == 0);
5625+
if (INSTR_OFFSET() >= frame->f_code->_co_firsttraceable) {
56265626
int instr_prev = _PyInterpreterFrame_LASTI(frame);
56275627
frame->prev_instr = next_instr;
56285628
TRACING_NEXTOPARG();
@@ -6832,12 +6832,15 @@ void
68326832
PyThreadState_EnterTracing(PyThreadState *tstate)
68336833
{
68346834
tstate->tracing++;
6835+
tstate->cframe->use_tracing = 0;
68356836
}
68366837

68376838
void
68386839
PyThreadState_LeaveTracing(PyThreadState *tstate)
68396840
{
6841+
assert(tstate->tracing > 0 && tstate->cframe->use_tracing == 0);
68406842
tstate->tracing--;
6843+
_PyThreadState_UpdateTracingState(tstate);
68416844
}
68426845

68436846
static int

0 commit comments

Comments
 (0)