Skip to content

Commit 7895a61

Browse files
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 0a61e23.
1 parent 339c8e1 commit 7895a61

File tree

7 files changed

+53
-64
lines changed

7 files changed

+53
-64
lines changed

Misc/NEWS.d/next/Core and Builtins/2024-02-04-07-45-29.gh-issue-107674.q8mCmi.rst

Lines changed: 0 additions & 1 deletion
This file was deleted.

Python/bytecodes.c

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -143,23 +143,22 @@ dummy_func(
143143

144144
tier1 inst(RESUME, (--)) {
145145
assert(frame == tstate->current_frame);
146-
if (tstate->tracing == 0) {
147-
uintptr_t global_version =
148-
_Py_atomic_load_uintptr_relaxed(&tstate->eval_breaker) &
149-
~_PY_EVAL_EVENTS_MASK;
150-
uintptr_t code_version = _PyFrame_GetCode(frame)->_co_instrumentation_version;
151-
assert((code_version & 255) == 0);
152-
if (code_version != global_version) {
153-
int err = _Py_Instrument(_PyFrame_GetCode(frame), tstate->interp);
154-
ERROR_IF(err, error);
155-
next_instr = this_instr;
156-
DISPATCH();
157-
}
146+
uintptr_t global_version =
147+
_Py_atomic_load_uintptr_relaxed(&tstate->eval_breaker) &
148+
~_PY_EVAL_EVENTS_MASK;
149+
uintptr_t code_version = _PyFrame_GetCode(frame)->_co_instrumentation_version;
150+
assert((code_version & 255) == 0);
151+
if (code_version != global_version) {
152+
int err = _Py_Instrument(_PyFrame_GetCode(frame), tstate->interp);
153+
ERROR_IF(err, error);
154+
next_instr = this_instr;
158155
}
159-
if ((oparg & RESUME_OPARG_LOCATION_MASK) < RESUME_AFTER_YIELD_FROM) {
160-
CHECK_EVAL_BREAKER();
156+
else {
157+
if ((oparg & RESUME_OPARG_LOCATION_MASK) < RESUME_AFTER_YIELD_FROM) {
158+
CHECK_EVAL_BREAKER();
159+
}
160+
this_instr->op.code = RESUME_CHECK;
161161
}
162-
this_instr->op.code = RESUME_CHECK;
163162
}
164163

165164
inst(RESUME_CHECK, (--)) {
@@ -170,13 +169,13 @@ dummy_func(
170169
uintptr_t eval_breaker = _Py_atomic_load_uintptr_relaxed(&tstate->eval_breaker);
171170
uintptr_t version = _PyFrame_GetCode(frame)->_co_instrumentation_version;
172171
assert((version & _PY_EVAL_EVENTS_MASK) == 0);
173-
DEOPT_IF(eval_breaker != version && tstate->tracing == 0);
172+
DEOPT_IF(eval_breaker != version);
174173
}
175174

176175
inst(INSTRUMENTED_RESUME, (--)) {
177176
uintptr_t global_version = _Py_atomic_load_uintptr_relaxed(&tstate->eval_breaker) & ~_PY_EVAL_EVENTS_MASK;
178177
uintptr_t code_version = _PyFrame_GetCode(frame)->_co_instrumentation_version;
179-
if (code_version != global_version && tstate->tracing == 0) {
178+
if (code_version != global_version) {
180179
if (_Py_Instrument(_PyFrame_GetCode(frame), tstate->interp)) {
181180
GOTO_ERROR(error);
182181
}

Python/ceval.c

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -806,23 +806,17 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, _PyInterpreterFrame *frame, int
806806
{
807807
_Py_CODEUNIT *prev = frame->instr_ptr;
808808
_Py_CODEUNIT *here = frame->instr_ptr = next_instr;
809-
int original_opcode = 0;
810-
if (tstate->tracing) {
811-
PyCodeObject *code = _PyFrame_GetCode(frame);
812-
original_opcode = code->_co_monitoring->lines[(int)(here - _PyCode_CODE(code))].original_opcode;
813-
} else {
814-
_PyFrame_SetStackPointer(frame, stack_pointer);
815-
original_opcode = _Py_call_instrumentation_line(
816-
tstate, frame, here, prev);
817-
stack_pointer = _PyFrame_GetStackPointer(frame);
818-
if (original_opcode < 0) {
819-
next_instr = here+1;
820-
goto error;
821-
}
822-
next_instr = frame->instr_ptr;
823-
if (next_instr != here) {
824-
DISPATCH();
825-
}
809+
_PyFrame_SetStackPointer(frame, stack_pointer);
810+
int original_opcode = _Py_call_instrumentation_line(
811+
tstate, frame, here, prev);
812+
stack_pointer = _PyFrame_GetStackPointer(frame);
813+
if (original_opcode < 0) {
814+
next_instr = here+1;
815+
goto error;
816+
}
817+
next_instr = frame->instr_ptr;
818+
if (next_instr != here) {
819+
DISPATCH();
826820
}
827821
if (_PyOpcode_Caches[original_opcode]) {
828822
_PyBinaryOpCache *cache = (_PyBinaryOpCache *)(next_instr+1);

Python/ceval_macros.h

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -347,16 +347,12 @@ do { \
347347
// for an exception handler, displaying the traceback, and so on
348348
#define INSTRUMENTED_JUMP(src, dest, event) \
349349
do { \
350-
if (tstate->tracing) {\
351-
next_instr = dest; \
352-
} else { \
353-
_PyFrame_SetStackPointer(frame, stack_pointer); \
354-
next_instr = _Py_call_instrumentation_jump(tstate, event, frame, src, dest); \
355-
stack_pointer = _PyFrame_GetStackPointer(frame); \
356-
if (next_instr == NULL) { \
357-
next_instr = (dest)+1; \
358-
goto error; \
359-
} \
350+
_PyFrame_SetStackPointer(frame, stack_pointer); \
351+
next_instr = _Py_call_instrumentation_jump(tstate, event, frame, src, dest); \
352+
stack_pointer = _PyFrame_GetStackPointer(frame); \
353+
if (next_instr == NULL) { \
354+
next_instr = (dest)+1; \
355+
goto error; \
360356
} \
361357
} while (0);
362358

Python/executor_cases.c.h

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Python/generated_cases.c.h

Lines changed: 16 additions & 17 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Python/instrumentation.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1156,13 +1156,15 @@ int
11561156
_Py_call_instrumentation_line(PyThreadState *tstate, _PyInterpreterFrame* frame, _Py_CODEUNIT *instr, _Py_CODEUNIT *prev)
11571157
{
11581158
PyCodeObject *code = _PyFrame_GetCode(frame);
1159-
assert(tstate->tracing == 0);
11601159
assert(is_version_up_to_date(code, tstate->interp));
11611160
assert(instrumentation_cross_checks(tstate->interp, code));
11621161
int i = (int)(instr - _PyCode_CODE(code));
11631162

11641163
_PyCoMonitoringData *monitoring = code->_co_monitoring;
11651164
_PyCoLineInstrumentationData *line_data = &monitoring->lines[i];
1165+
if (tstate->tracing) {
1166+
goto done;
1167+
}
11661168
PyInterpreterState *interp = tstate->interp;
11671169
int8_t line_delta = line_data->line_delta;
11681170
int line = compute_line(code, i, line_delta);

0 commit comments

Comments
 (0)