Skip to content

Commit 751143f

Browse files
committed
Make line number lookup O(1) regardless of the size of the code object
1 parent 180d417 commit 751143f

File tree

5 files changed

+183
-132
lines changed

5 files changed

+183
-132
lines changed

Include/cpython/code.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ typedef struct {
3838
Line instrumentation creates an array of
3939
these. One entry per code unit.*/
4040
typedef struct {
41-
uint8_t original_opcode;
42-
int8_t line_delta;
41+
uint8_t bytes_per_entry;
42+
uint8_t data[1];
4343
} _PyCoLineInstrumentationData;
4444

4545

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
The time to handle a ``LINE`` event in sys.monitoring (and sys.settrace) is
2+
now independent of the number of lines in the code object.

Python/bytecodes.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4731,7 +4731,8 @@ dummy_func(
47314731
int original_opcode = 0;
47324732
if (tstate->tracing) {
47334733
PyCodeObject *code = _PyFrame_GetCode(frame);
4734-
original_opcode = code->_co_monitoring->lines[(int)(this_instr - _PyFrame_GetBytecode(frame))].original_opcode;
4734+
int index = (int)(this_instr - _PyFrame_GetBytecode(frame));
4735+
original_opcode = code->_co_monitoring->lines->data[index*code->_co_monitoring->lines->bytes_per_entry];
47354736
next_instr = this_instr;
47364737
} else {
47374738
original_opcode = _Py_call_instrumentation_line(

Python/generated_cases.c.h

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

0 commit comments

Comments
 (0)