[Python-checkins] GH-90081: Run python tracers at full speed (GH-95328)

markshannon webhook-mailer at python.org
Thu Jul 28 05:17:59 EDT 2022


https://github.com/python/cpython/commit/b8b2990fb3218cffedfe7bc92e9e7ae2275b3c98
commit: b8b2990fb3218cffedfe7bc92e9e7ae2275b3c98
branch: main
author: Mark Shannon <mark at hotpy.org>
committer: markshannon <mark at hotpy.org>
date: 2022-07-28T10:17:22+01:00
summary:

GH-90081: Run python tracers at full speed (GH-95328)

files:
A Misc/NEWS.d/next/Core and Builtins/2022-07-27-14-21-57.gh-issue-90081.HVAS5x.rst
M Include/internal/pycore_pystate.h
M Python/ceval.c

diff --git a/Include/internal/pycore_pystate.h b/Include/internal/pycore_pystate.h
index b4a39b62b76a1..51d119c23e731 100644
--- a/Include/internal/pycore_pystate.h
+++ b/Include/internal/pycore_pystate.h
@@ -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);
 }
 
diff --git a/Misc/NEWS.d/next/Core and Builtins/2022-07-27-14-21-57.gh-issue-90081.HVAS5x.rst b/Misc/NEWS.d/next/Core and Builtins/2022-07-27-14-21-57.gh-issue-90081.HVAS5x.rst
new file mode 100644
index 0000000000000..a3be34c175af6
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and Builtins/2022-07-27-14-21-57.gh-issue-90081.HVAS5x.rst	
@@ -0,0 +1,2 @@
+Run Python code in tracer/profiler function at full speed. Fixes slowdown in
+earlier versions of 3.11.
diff --git a/Python/ceval.c b/Python/ceval.c
index 3a9b0e569f02a..7ad26a70dd18c 100644
--- a/Python/ceval.c
+++ b/Python/ceval.c
@@ -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();
@@ -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();
@@ -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



More information about the Python-checkins mailing list