Skip to content

perf: cache InstrLocation across consecutive positions#202

Merged
MatthieuDartiailh merged 1 commit into
MatthieuDartiailh:mainfrom
P403n1x87:perf/reuse-instrlocation
May 27, 2026
Merged

perf: cache InstrLocation across consecutive positions#202
MatthieuDartiailh merged 1 commit into
MatthieuDartiailh:mainfrom
P403n1x87:perf/reuse-instrlocation

Conversation

@P403n1x87
Copy link
Copy Markdown
Contributor

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)

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-commenter
Copy link
Copy Markdown

codecov-commenter commented May 27, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 95.45%. Comparing base (41c679b) to head (c2a610c).

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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@P403n1x87 P403n1x87 marked this pull request as ready for review May 27, 2026 11:16
@MatthieuDartiailh MatthieuDartiailh merged commit 40b6bd4 into MatthieuDartiailh:main May 27, 2026
10 checks passed
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.

3 participants