Skip to content

perf: replace O(n) name/varname list scan with O(1) dict lookup#199

Merged
MatthieuDartiailh merged 4 commits into
MatthieuDartiailh:mainfrom
P403n1x87:perf/name-mappings
May 21, 2026
Merged

perf: replace O(n) name/varname list scan with O(1) dict lookup#199
MatthieuDartiailh merged 4 commits into
MatthieuDartiailh:mainfrom
P403n1x87:perf/name-mappings

Conversation

@P403n1x87
Copy link
Copy Markdown
Contributor

@P403n1x87 P403n1x87 commented May 18, 2026

_ConvertBytecodeToConcrete.add() used list.index() to find whether a name or varname was already registered, making each lookup O(n) in the number of names accumulated so far. For functions with many locals or names this becomes a non-trivial cost on every HAS_LOCAL / HAS_NAME instruction.

Replace the generic static add() method with two instance methods, add_varname() and add_name(), each backed by a parallel dict (varnames_map / names_map). Lookups are now O(1). The argnames pre-population loop is also updated to go through add_varname() so the map stays in sync.

Benchmark (round-trip test):

r/s
Baseline 204.1 +/- 4.2
This change 221.3 +/- 2.9

So a +8.4% improvement overall. We also move some more instruction validation checks from iteration to insertion, which also contributes to the total uplift (~ +3% of the +8.4% total).

_ConvertBytecodeToConcrete.add() used list.index() to find whether a
name or varname was already registered, making each lookup O(n) in the
number of names accumulated so far. For functions with many locals or
names this becomes a non-trivial cost on every HAS_LOCAL / HAS_NAME
instruction.

Replace the generic static add() method with two instance methods,
add_varname() and add_name(), each backed by a parallel dict
(varnames_map / names_map). Lookups are now O(1). The argnames
pre-population loop is also updated to go through add_varname() so the
map stays in sync.

We also move some more instruction validation checks from iteration to
insertion, which also contributes to the total uplift (~+3%).

Benchmark (round-trip test):
  Baseline:     204.1 r/s  (stdev 4.2)
  This change:  221.3 r/s  (stdev 2.9)
  Uplift:       +8.4%
@P403n1x87 P403n1x87 force-pushed the perf/name-mappings branch from 5caa36b to c7234a7 Compare May 18, 2026 10:45
@codecov-commenter
Copy link
Copy Markdown

codecov-commenter commented May 18, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 95.39%. Comparing base (8a249bb) to head (a563e70).

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #199      +/-   ##
==========================================
+ Coverage   95.24%   95.39%   +0.14%     
==========================================
  Files           7        7              
  Lines        2083     2107      +24     
  Branches      449      456       +7     
==========================================
+ Hits         1984     2010      +26     
+ Misses         55       54       -1     
+ Partials       44       43       -1     

☔ 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 18, 2026 10:50
Copy link
Copy Markdown
Owner

@MatthieuDartiailh MatthieuDartiailh left a comment

Choose a reason for hiding this comment

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

This makes sense I think even though it is a breaking change for CFG. Could you add a changelog entry so that I do not forget to document when doing the next release ?

@P403n1x87
Copy link
Copy Markdown
Contributor Author

it is a breaking change for CFG.

In what sense? The only change in CFG is the time when validation happens, but no API has been changed (_ConvertBytecodeToConcrete is to be considered private given the _ prefix I guess). Is this what you would like to have documented?

@MatthieuDartiailh
Copy link
Copy Markdown
Owner

The fact the validation of the content now occurs earlier could break some usages (extend node and then break it into sub nodes). It is not a large breakage but worth documenting.

@MatthieuDartiailh
Copy link
Copy Markdown
Owner

Looking at codecov report we are actually missing tests for the error paths of append and insert. Would you mind adding those ?

Copy link
Copy Markdown
Owner

@MatthieuDartiailh MatthieuDartiailh left a comment

Choose a reason for hiding this comment

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

I promise those are the last comments. Recently I only manage to review from my phone which makes me miss the broader context quite often sorry for this.

Comment thread tests/test_cfg.py
Comment thread tests/test_cfg.py Outdated
@P403n1x87
Copy link
Copy Markdown
Contributor Author

I promise those are the last comments.

No worries! Changes made.

@MatthieuDartiailh MatthieuDartiailh merged commit e5d1d74 into MatthieuDartiailh:main May 21, 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