perf: cache InstrLocation across consecutive positions#202
Merged
MatthieuDartiailh merged 1 commit intoMay 27, 2026
Merged
Conversation
In ConcreteBytecode.from_code, InstrLocation._from_tuple is called once per instruction to construct a location object from the (lineno, end_lineno, col_offset, end_col_offset) tuple yielded by co_positions(). This was 5.31% of total CPU time in profiling. Python bytecodes for a single source expression (e.g. a + b, a for loop header, a CACHE entry) all map to the same source position. Because of this structural property, position tuples repeat frequently and tend to be contiguous — consecutive instructions usually share the same position. In the dis module corpus, 78.4% of the 860 position tuples are repeated. A single-entry cache (remember the last pos/loc pair) turns most iterations into a cheap tuple equality check, avoiding object.__new__ + 4x object.__setattr__ for the common case. A full dict cache was also tried but was slower: the per-iteration dict hash + lookup costs more than tuple equality even though it has a higher hit rate. Performance analysis (own time): | Hotspot | Before | After | |---|---|---| | InstrLocation._from_tuple own | 5.31% | 1.55% | Code object analysis (dis module) | Metric | Value | |---|---| | Total position tuples | 860 | | Unique positions | 318 (37%) | | Repeated positions | 542 (63%) | Throughput (Bytecode.from_code().to_code() on dis module, 30 runs each) with Mann-Whitney U Test | Approach | p95 | vs baseline | |---|---|---| | Baseline (main) | 180 r/s | — | | Dict cache | 190 r/s | +5.6% (✓ significant, p≈0) | | Single-entry cache | 192 r/s | +6.7% (✓ significant, p≈0) |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #202 +/- ##
==========================================
+ Coverage 95.43% 95.45% +0.01%
==========================================
Files 7 7
Lines 2126 2132 +6
Branches 458 459 +1
==========================================
+ Hits 2029 2035 +6
Misses 54 54
Partials 43 43 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
MatthieuDartiailh
approved these changes
May 27, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
In ConcreteBytecode.from_code, InstrLocation._from_tuple is called once per instruction to construct a location object from the (lineno, end_lineno, col_offset, end_col_offset) tuple yielded by co_positions(). This was 5.31% of total CPU time in profiling.
Python bytecodes for a single source expression (e.g. a + b, a for loop header, a CACHE entry) all map to the same source position. Because of this structural property, position tuples repeat frequently and tend to be contiguous — consecutive instructions usually share the same position. In the dis module corpus, 78.4% of the 860 position tuples are repeated.
A single-entry cache (remember the last pos/loc pair) turns most iterations into a cheap tuple equality check, avoiding object.new + 4x object.setattr for the common case. A full dict cache was also tried but was slower: the per-iteration dict hash + lookup costs more than tuple equality even though it has a higher hit rate.
Performance analysis (own time):
Code object analysis (dis module)
Throughput (Bytecode.from_code().to_code() on dis module, 30 runs each) with Mann-Whitney U Test