Skip to content

perf: move BasicBlock type check from __iter__ to insertion methods#198

Merged
MatthieuDartiailh merged 4 commits into
MatthieuDartiailh:mainfrom
P403n1x87:perf/type-check-on-insertion
May 12, 2026
Merged

perf: move BasicBlock type check from __iter__ to insertion methods#198
MatthieuDartiailh merged 4 commits into
MatthieuDartiailh:mainfrom
P403n1x87:perf/type-check-on-insertion

Conversation

@P403n1x87
Copy link
Copy Markdown
Contributor

The isinstance check on every yielded item in BasicBlock.iter was purely defensive — it detected invalid types that had already entered the list. Moving it to append/extend/insert/setitem catches invalid types at the point of insertion instead, eliminating the check from the hot iteration path entirely.

The structural checks (jump must be last, jump/TryBegin target must be a BasicBlock) remain in iter as they depend on the full block context and cannot be verified at insertion time.

Profiling data

Hotspot Before After
BasicBlock.__iter__ own 4.91% 2.82%
ControlFlowGraph.from_bytecode own 4.61% 4.04%

Throughput (Bytecode.from_code().to_code() on dis module's code object, 1 second timed window, 5 runs):

r/s range
Before 133–144
After 142–144

The isinstance check on every yielded item in BasicBlock.__iter__ was
purely defensive — it detected invalid types that had already entered
the list. Moving it to append/extend/insert/__setitem__ catches invalid
types at the point of insertion instead, eliminating the check from the
hot iteration path entirely.

The structural checks (jump must be last, jump/TryBegin target must be
a BasicBlock) remain in __iter__ as they depend on the full block
context and cannot be verified at insertion time.

Profiling data

| Hotspot | Before | After |
|---|---|---|
| `BasicBlock.__iter__` own | 4.91% | 2.82% |
| `ControlFlowGraph.from_bytecode` own | 4.61% | 4.04% |

Throughput (Bytecode.from_code().to_code() on dis module's code object,
1 second timed window, 5 runs):

| | r/s range |
|---|---|
| Before | 133–144 |
| After | 142–144 |
@codecov-commenter
Copy link
Copy Markdown

codecov-commenter commented May 10, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 95.24%. Comparing base (b67adbf) to head (108f3ff).

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #198      +/-   ##
==========================================
+ Coverage   95.19%   95.24%   +0.05%     
==========================================
  Files           7        7              
  Lines        2061     2083      +22     
  Branches      446      449       +3     
==========================================
+ Hits         1962     1984      +22     
  Misses         55       55              
  Partials       44       44              

☔ 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 11, 2026 13:43
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 would like to see an extra test but otherwise LGTM

Comment thread tests/test_cfg.py
@MatthieuDartiailh
Copy link
Copy Markdown
Owner

Actually looking at codecov report you are missing test cases for valid setitem (index and slice).

@MatthieuDartiailh MatthieuDartiailh merged commit 8a249bb into MatthieuDartiailh:main May 12, 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