perf: move BasicBlock type check from __iter__ to insertion methods#198
Merged
MatthieuDartiailh merged 4 commits intoMay 12, 2026
Merged
Conversation
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 Report✅ All modified and coverable lines are covered by tests. 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. 🚀 New features to boost your workflow:
|
Owner
MatthieuDartiailh
left a comment
There was a problem hiding this comment.
I would like to see an extra test but otherwise LGTM
MatthieuDartiailh
previously approved these changes
May 11, 2026
Owner
|
Actually looking at codecov report you are missing test cases for valid setitem (index and slice). |
MatthieuDartiailh
approved these changes
May 12, 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.
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
BasicBlock.__iter__ownControlFlowGraph.from_bytecodeownThroughput (Bytecode.from_code().to_code() on dis module's code object, 1 second timed window, 5 runs):