https://github.com/python/cpython/commit/7895a61168aad4565a1d953104c9ec620e7c588f
commit: 7895a61168aad4565a1d953104c9ec620e7c588f
branch: main
author: Tian Gao <[email protected]>
committer: encukou <[email protected]>
date: 2024-03-01T07:46:33+01:00
summary:

gh-116098: Revert "gh-107674: Improve performance of `sys.settrace` 
(GH-114986)" (GH-116178)

Revert "gh-107674: Improve performance of `sys.settrace` (GH-114986)"

This reverts commit 0a61e237009bf6b833e13ac635299ee063377699.

files:
D Misc/NEWS.d/next/Core and 
Builtins/2024-02-04-07-45-29.gh-issue-107674.q8mCmi.rst
M Python/bytecodes.c
M Python/ceval.c
M Python/ceval_macros.h
M Python/executor_cases.c.h
M Python/generated_cases.c.h
M Python/instrumentation.c

diff --git a/Misc/NEWS.d/next/Core and 
Builtins/2024-02-04-07-45-29.gh-issue-107674.q8mCmi.rst b/Misc/NEWS.d/next/Core 
and Builtins/2024-02-04-07-45-29.gh-issue-107674.q8mCmi.rst
deleted file mode 100644
index f9b96788bfad94..00000000000000
--- a/Misc/NEWS.d/next/Core and 
Builtins/2024-02-04-07-45-29.gh-issue-107674.q8mCmi.rst 
+++ /dev/null
@@ -1 +0,0 @@
-Improved the performance of :func:`sys.settrace` significantly
diff --git a/Python/bytecodes.c b/Python/bytecodes.c
index 095982d64f34d0..396a8f09f3feca 100644
--- a/Python/bytecodes.c
+++ b/Python/bytecodes.c
@@ -143,23 +143,22 @@ dummy_func(
 
         tier1 inst(RESUME, (--)) {
             assert(frame == tstate->current_frame);
-            if (tstate->tracing == 0) {
-                uintptr_t global_version =
-                    _Py_atomic_load_uintptr_relaxed(&tstate->eval_breaker) &
-                    ~_PY_EVAL_EVENTS_MASK;
-                uintptr_t code_version = 
_PyFrame_GetCode(frame)->_co_instrumentation_version;
-                assert((code_version & 255) == 0);
-                if (code_version != global_version) {
-                    int err = _Py_Instrument(_PyFrame_GetCode(frame), 
tstate->interp);
-                    ERROR_IF(err, error);
-                    next_instr = this_instr;
-                    DISPATCH();
-                }
+            uintptr_t global_version =
+                _Py_atomic_load_uintptr_relaxed(&tstate->eval_breaker) &
+                ~_PY_EVAL_EVENTS_MASK;
+            uintptr_t code_version = 
_PyFrame_GetCode(frame)->_co_instrumentation_version;
+            assert((code_version & 255) == 0);
+            if (code_version != global_version) {
+                int err = _Py_Instrument(_PyFrame_GetCode(frame), 
tstate->interp);
+                ERROR_IF(err, error);
+                next_instr = this_instr;
             }
-            if ((oparg & RESUME_OPARG_LOCATION_MASK) < 
RESUME_AFTER_YIELD_FROM) {
-                CHECK_EVAL_BREAKER();
+            else {
+                if ((oparg & RESUME_OPARG_LOCATION_MASK) < 
RESUME_AFTER_YIELD_FROM) {
+                    CHECK_EVAL_BREAKER();
+                }
+                this_instr->op.code = RESUME_CHECK;
             }
-            this_instr->op.code = RESUME_CHECK;
         }
 
         inst(RESUME_CHECK, (--)) {
@@ -170,13 +169,13 @@ dummy_func(
             uintptr_t eval_breaker = 
_Py_atomic_load_uintptr_relaxed(&tstate->eval_breaker);
             uintptr_t version = 
_PyFrame_GetCode(frame)->_co_instrumentation_version;
             assert((version & _PY_EVAL_EVENTS_MASK) == 0);
-            DEOPT_IF(eval_breaker != version && tstate->tracing == 0);
+            DEOPT_IF(eval_breaker != version);
         }
 
         inst(INSTRUMENTED_RESUME, (--)) {
             uintptr_t global_version = 
_Py_atomic_load_uintptr_relaxed(&tstate->eval_breaker) & ~_PY_EVAL_EVENTS_MASK;
             uintptr_t code_version = 
_PyFrame_GetCode(frame)->_co_instrumentation_version;
-            if (code_version != global_version && tstate->tracing == 0) {
+            if (code_version != global_version) {
                 if (_Py_Instrument(_PyFrame_GetCode(frame), tstate->interp)) {
                     GOTO_ERROR(error);
                 }
diff --git a/Python/ceval.c b/Python/ceval.c
index 34f286e4e17eb8..f817f288903694 100644
--- a/Python/ceval.c
+++ b/Python/ceval.c
@@ -806,23 +806,17 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, 
_PyInterpreterFrame *frame, int
     {
         _Py_CODEUNIT *prev = frame->instr_ptr;
         _Py_CODEUNIT *here = frame->instr_ptr = next_instr;
-        int original_opcode = 0;
-        if (tstate->tracing) {
-            PyCodeObject *code = _PyFrame_GetCode(frame);
-            original_opcode = code->_co_monitoring->lines[(int)(here - 
_PyCode_CODE(code))].original_opcode;
-        } else {
-            _PyFrame_SetStackPointer(frame, stack_pointer);
-            original_opcode = _Py_call_instrumentation_line(
-                    tstate, frame, here, prev);
-            stack_pointer = _PyFrame_GetStackPointer(frame);
-            if (original_opcode < 0) {
-                next_instr = here+1;
-                goto error;
-            }
-            next_instr = frame->instr_ptr;
-            if (next_instr != here) {
-                DISPATCH();
-            }
+        _PyFrame_SetStackPointer(frame, stack_pointer);
+        int original_opcode = _Py_call_instrumentation_line(
+                tstate, frame, here, prev);
+        stack_pointer = _PyFrame_GetStackPointer(frame);
+        if (original_opcode < 0) {
+            next_instr = here+1;
+            goto error;
+        }
+        next_instr = frame->instr_ptr;
+        if (next_instr != here) {
+            DISPATCH();
         }
         if (_PyOpcode_Caches[original_opcode]) {
             _PyBinaryOpCache *cache = (_PyBinaryOpCache *)(next_instr+1);
diff --git a/Python/ceval_macros.h b/Python/ceval_macros.h
index b024b510cfda1f..22992aa09e1f38 100644
--- a/Python/ceval_macros.h
+++ b/Python/ceval_macros.h
@@ -347,16 +347,12 @@ do { \
 // for an exception handler, displaying the traceback, and so on
 #define INSTRUMENTED_JUMP(src, dest, event) \
 do { \
-    if (tstate->tracing) {\
-        next_instr = dest; \
-    } else { \
-        _PyFrame_SetStackPointer(frame, stack_pointer); \
-        next_instr = _Py_call_instrumentation_jump(tstate, event, frame, src, 
dest); \
-        stack_pointer = _PyFrame_GetStackPointer(frame); \
-        if (next_instr == NULL) { \
-            next_instr = (dest)+1; \
-            goto error; \
-        } \
+    _PyFrame_SetStackPointer(frame, stack_pointer); \
+    next_instr = _Py_call_instrumentation_jump(tstate, event, frame, src, 
dest); \
+    stack_pointer = _PyFrame_GetStackPointer(frame); \
+    if (next_instr == NULL) { \
+        next_instr = (dest)+1; \
+        goto error; \
     } \
 } while (0);
 
diff --git a/Python/executor_cases.c.h b/Python/executor_cases.c.h
index e84c33281ee640..56ee93862743d5 100644
--- a/Python/executor_cases.c.h
+++ b/Python/executor_cases.c.h
@@ -20,7 +20,7 @@
             uintptr_t eval_breaker = 
_Py_atomic_load_uintptr_relaxed(&tstate->eval_breaker);
             uintptr_t version = 
_PyFrame_GetCode(frame)->_co_instrumentation_version;
             assert((version & _PY_EVAL_EVENTS_MASK) == 0);
-            if (eval_breaker != version && tstate->tracing == 0) goto 
deoptimize;
+            if (eval_breaker != version) goto deoptimize;
             break;
         }
 
diff --git a/Python/generated_cases.c.h b/Python/generated_cases.c.h
index 4369d8c9df796b..e612c9e4c37632 100644
--- a/Python/generated_cases.c.h
+++ b/Python/generated_cases.c.h
@@ -3153,7 +3153,7 @@
             INSTRUCTION_STATS(INSTRUMENTED_RESUME);
             uintptr_t global_version = 
_Py_atomic_load_uintptr_relaxed(&tstate->eval_breaker) & ~_PY_EVAL_EVENTS_MASK;
             uintptr_t code_version = 
_PyFrame_GetCode(frame)->_co_instrumentation_version;
-            if (code_version != global_version && tstate->tracing == 0) {
+            if (code_version != global_version) {
                 if (_Py_Instrument(_PyFrame_GetCode(frame), tstate->interp)) {
                     GOTO_ERROR(error);
                 }
@@ -4825,23 +4825,22 @@
             _Py_CODEUNIT *this_instr = next_instr - 1;
             (void)this_instr;
             assert(frame == tstate->current_frame);
-            if (tstate->tracing == 0) {
-                uintptr_t global_version =
-                _Py_atomic_load_uintptr_relaxed(&tstate->eval_breaker) &
-                ~_PY_EVAL_EVENTS_MASK;
-                uintptr_t code_version = 
_PyFrame_GetCode(frame)->_co_instrumentation_version;
-                assert((code_version & 255) == 0);
-                if (code_version != global_version) {
-                    int err = _Py_Instrument(_PyFrame_GetCode(frame), 
tstate->interp);
-                    if (err) goto error;
-                    next_instr = this_instr;
-                    DISPATCH();
-                }
+            uintptr_t global_version =
+            _Py_atomic_load_uintptr_relaxed(&tstate->eval_breaker) &
+            ~_PY_EVAL_EVENTS_MASK;
+            uintptr_t code_version = 
_PyFrame_GetCode(frame)->_co_instrumentation_version;
+            assert((code_version & 255) == 0);
+            if (code_version != global_version) {
+                int err = _Py_Instrument(_PyFrame_GetCode(frame), 
tstate->interp);
+                if (err) goto error;
+                next_instr = this_instr;
             }
-            if ((oparg & RESUME_OPARG_LOCATION_MASK) < 
RESUME_AFTER_YIELD_FROM) {
-                CHECK_EVAL_BREAKER();
+            else {
+                if ((oparg & RESUME_OPARG_LOCATION_MASK) < 
RESUME_AFTER_YIELD_FROM) {
+                    CHECK_EVAL_BREAKER();
+                }
+                this_instr->op.code = RESUME_CHECK;
             }
-            this_instr->op.code = RESUME_CHECK;
             DISPATCH();
         }
 
@@ -4857,7 +4856,7 @@
             uintptr_t eval_breaker = 
_Py_atomic_load_uintptr_relaxed(&tstate->eval_breaker);
             uintptr_t version = 
_PyFrame_GetCode(frame)->_co_instrumentation_version;
             assert((version & _PY_EVAL_EVENTS_MASK) == 0);
-            DEOPT_IF(eval_breaker != version && tstate->tracing == 0, RESUME);
+            DEOPT_IF(eval_breaker != version, RESUME);
             DISPATCH();
         }
 
diff --git a/Python/instrumentation.c b/Python/instrumentation.c
index 4b7d8b5a587504..6f1bc2e0a107df 100644
--- a/Python/instrumentation.c
+++ b/Python/instrumentation.c
@@ -1156,13 +1156,15 @@ int
 _Py_call_instrumentation_line(PyThreadState *tstate, _PyInterpreterFrame* 
frame, _Py_CODEUNIT *instr, _Py_CODEUNIT *prev)
 {
     PyCodeObject *code = _PyFrame_GetCode(frame);
-    assert(tstate->tracing == 0);
     assert(is_version_up_to_date(code, tstate->interp));
     assert(instrumentation_cross_checks(tstate->interp, code));
     int i = (int)(instr - _PyCode_CODE(code));
 
     _PyCoMonitoringData *monitoring = code->_co_monitoring;
     _PyCoLineInstrumentationData *line_data = &monitoring->lines[i];
+    if (tstate->tracing) {
+        goto done;
+    }
     PyInterpreterState *interp = tstate->interp;
     int8_t line_delta = line_data->line_delta;
     int line = compute_line(code, i, line_delta);

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-checkins.python.org/
Member address: [email protected]

Reply via email to