]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
GH-90081: Run python tracers at full speed (GH-95328) (#95363)
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Fri, 29 Jul 2022 08:43:52 +0000 (01:43 -0700)
committerGitHub <noreply@github.com>
Fri, 29 Jul 2022 08:43:52 +0000 (09:43 +0100)
(cherry picked from commit b8b2990fb3218cffedfe7bc92e9e7ae2275b3c98)

Co-authored-by: Mark Shannon <mark@hotpy.org>
Co-authored-by: Mark Shannon <mark@hotpy.org>
Include/internal/pycore_pystate.h
Misc/NEWS.d/next/Core and Builtins/2022-07-27-14-21-57.gh-issue-90081.HVAS5x.rst [new file with mode: 0644]
Python/ceval.c

index e2d7c5bee28b924792320451fad5997ab3ca980f..cb8f115768a35205cc273d3618422e6a83368370 100644 (file)
@@ -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 (file)
index 0000000..a3be34c
--- /dev/null
@@ -0,0 +1,2 @@
+Run Python code in tracer/profiler function at full speed. Fixes slowdown in
+earlier versions of 3.11.
index 31cdb554279e37b9ce297ec522c5c1769a7c6687..23733b63f3c7640a5aea811924ac14695d3246e7 100644 (file)
@@ -5590,7 +5590,7 @@ handle_eval_breaker:
             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();
@@ -5620,9 +5620,9 @@ handle_eval_breaker:
         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();
@@ -6832,12 +6832,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