Commit 9244884
committed
perf: move BasicBlock type check from __iter__ to insertion methods
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 |1 parent 25bf1bc commit 9244884
2 files changed
Lines changed: 43 additions & 10 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
42 | 42 | | |
43 | 43 | | |
44 | 44 | | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
45 | 83 | | |
46 | 84 | | |
47 | 85 | | |
48 | 86 | | |
49 | 87 | | |
50 | 88 | | |
51 | | - | |
52 | | - | |
53 | | - | |
54 | | - | |
55 | | - | |
56 | | - | |
57 | 89 | | |
58 | 90 | | |
59 | 91 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
55 | 55 | | |
56 | 56 | | |
57 | 57 | | |
58 | | - | |
| 58 | + | |
59 | 59 | | |
60 | | - | |
61 | 60 | | |
62 | | - | |
| 61 | + | |
63 | 62 | | |
64 | | - | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
65 | 66 | | |
66 | 67 | | |
67 | 68 | | |
| |||
0 commit comments