Skip to content

GH-127953: Make line number lookup O(1) regardless of the size of the code object #128350

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jan 21, 2025

Conversation

markshannon
Copy link
Member

@markshannon markshannon commented Dec 30, 2024

Changes the line data array from 2 bytes per instruction to 2-5 bytes per instruction depending on the line length of the code object, changing the size of the line delta from 1 byte to 1-4 bytes as needed.

Most code objects are fewer than 250 lines, and still use 1 byte for line delta per instruction.
Code objects up to ~65k lines need 2 bytes per instruction.
3 bytes are needed for code objects up to ~16M lines long.
4 bytes are needed for code objects up to the maximum of ~1 billion lines.
I doubt 4 bytes will ever be needed, but it is supported.

This PR also fixes the INSTRUMENT_DEBUG mode which was broken in 2e95c5b

The time taken to execute the example given in #127953 (comment) is about the same as 3.10 or 3.11. Maybe a bit faster, but I didn't benchmark it rigorously.

Modifying the script to generate a file of 100k lines, this PR is a bit faster than 3.11.
At 1M lines, this PR is a bit slower than 3.11.

@markshannon
Copy link
Member Author

Benchmarking shows no significant change in performance. The coverage benchmark shows a small speedup, but it is probably noise.

return COMPUTED_LINE;
int delta = line - code->co_firstlineno;
assert(delta > NO_LINE);
return delta;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like line_delta is actually an offset from the start now.

get_line_delta(_PyCoLineInstrumentationData *line_data, int index)
{
uint8_t *ptr = &line_data->data[index*line_data->bytes_per_entry+1];
uint32_t value = *ptr;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could assert here that bytes_per_entry is within range.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can't easily assert anything here, as we don't know the max line number without traversing the entire locations table.
I've added an assert to set_line_delta instead.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I meant that bytes_per_entry is between 1 and 4.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(bytes_per_entry is between 2 and 5 because it includes the original opcode)

@gaogaotiantian
Copy link
Member

I’m taking a vacation now and I’ll take a look once I’m back!

@markshannon markshannon merged commit 7239da7 into python:main Jan 21, 2025
61 checks passed
@miss-islington-app
Copy link

Thanks @markshannon for the PR 🌮🎉.. I'm working now to backport this PR to: 3.12, 3.13.
🐍🍒⛏🤖

@miss-islington-app
Copy link

Sorry, @markshannon, I could not cleanly backport this to 3.13 due to a conflict.
Please backport using cherry_picker on command line.

cherry_picker 7239da75592081b6e8d0917a2cd2bf19907c8165 3.13

@miss-islington-app
Copy link

Sorry, @markshannon, I could not cleanly backport this to 3.12 due to a conflict.
Please backport using cherry_picker on command line.

cherry_picker 7239da75592081b6e8d0917a2cd2bf19907c8165 3.12

@picnixz picnixz removed the needs backport to 3.13 bugs and security fixes label Apr 5, 2025
Yhg1s pushed a commit that referenced this pull request Apr 7, 2025
… of the code object (#129127)

GH-127953: Make line number lookup O(1) regardless of the size of the code object (GH-128350)
markshannon added a commit to faster-cpython/cpython that referenced this pull request Apr 8, 2025
…e size of the code object (python#129127)

pythonGH-127953: Make line number lookup O(1) regardless of the size of the code object (pythonGH-128350)
@hugovk hugovk removed the needs backport to 3.12 only security fixes label Apr 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants