Repository navigation
[mypyc] Only track registers in must-defined analysis - #22425
Merged
JukkaL merged 1 commit intoNov 12, 2026
Merged
Conversation
The refcount and uninit transforms pass all values, including temporaries, as the universe of the must-defined analysis. The analysis only generates and kills registers, and the callers only check registers, so the temporaries are never needed. However, a must analysis starts with a copy of the universe for every basic block, so time and memory used were O(blocks * values). This made the refcount transform very slow for large functions. For a function with 4000 statements (36k ops, 16k blocks), it took ~110s and used 15 GiB of memory. Now it takes 6s and uses 3.3 GiB. Leave everything except registers out of the universe. The generated IR and C are unchanged.
JukkaL
approved these changes
Nov 12, 2026
JukkaL
left a comment
Collaborator
There was a problem hiding this comment.
Good catch! I verified that this improves compilation perf and memory use significantly, and the generated C remains identical.
JukkaL
pushed a commit
that referenced
this pull request
Nov 13, 2026
`run_analysis` keeps a Python `set` for every basic block, and a new one
for every op whose gen/kill sets aren't empty. In large functions these
sets have thousands of items, so the data-flow analyses take O(ops *
values) time and memory. The must-defined and borrowed-arguments
analyses are the worst, since a must analysis starts every block from a
copy of the full universe. This makes the refcount transform very slow
for large functions, such as generated code or long module top levels.
This PR represents the sets as `int` bit masks:
- `BitIndex` assigns a bit position to each value an analysis sees.
- The worklist algorithm works on masks, so union, intersection and
difference are integer operations. Since ints are immutable, an op whose
gen/kill sets are empty shares the previous result, as before.
- The gen/kill masks of each op are computed once, instead of running
the visitor again in the op-level pass.
- The results are `BitSet` objects that implement `collections.abc.Set`,
so the code using the results only needed annotation changes
(`set[Value]` -> `AbstractSet[Value]` in refcount.py).
- `analyze_live_regs`, `analyze_borrowed_arguments` and
`analyze_must_defined_regs` take an optional `index`. The refcount
transform shares one index between the three, because it combines their
results (e.g. `live - live - borrowed` after a branch); with a shared
index these operations are integer operations too. With different
indexes, `BitSet` still works and, like `set`, iterates over the smaller
operand.
- Reflected operations (`set - bitset`, `set & bitset`, `set | bitset`)
return a real `set`, since typeshed says the result is a `set` and
compiled code checks this. `find_always_defined_attributes` relies on
it.
## Benchmark
A function with n statements of this form:
```python
a{i} = xs[{i % 7}] + {i}
if a{i}:
t = a{i}
```
Each analysis timed separately, then `insert_ref_count_opcodes`
(interpreted). Max RSS is for the whole process:
| n | ops / blocks | | live | borrowed | must-defined | refcount
transform | max RSS |
|---|---|---|---|---|---|---|---|
| 1000 | 9k / 2k | master | 0.02s | 0.13s | 0.61s | 0.83s | 1.2 GiB |
| | | this PR | 0.02s | 0.00s | 0.01s | 0.09s | 73 MiB |
| 2000 | 18k / 4k | master | 0.03s | 0.67s | 2.2s | 3.5s | 4.7 GiB |
| | | this PR | 0.06s | 0.01s | 0.02s | 0.23s | 109 MiB |
| 4000 | 36k / 8k | master | 0.09s | 2.2s | 93s | 28–84s (swapping) | 16
GiB |
| | | this PR | 0.19s | 0.02s | 0.05s | 0.63s | 225 MiB |
Liveness is a bit slower, because live sets are small and a mask grows
with the number of values. It is still a small part of the total.
On ordinary code, compiling `mypy/checker.py`, `mypy/checkexpr.py`,
`mypy/semanal.py` and `mypy/messages.py` with the analysis and transform
modules compiled by mypyc:
| | master | this PR |
|---|---|---|
| `insert_ref_count_opcodes` | 1.59–1.73s | 0.94–0.95s |
| `insert_uninit_checks` | 0.28–0.29s | 0.21s |
## Testing
- The generated C for those four modules is identical to master, both
with interpreted and compiled analysis modules, and the IR for the
benchmark functions is identical.
- The full mypyc test suite passes interpreted. The IR, refcount,
exceptions, always-defined, spill and run tests also pass with
`dataflow.py` and its users compiled by mypyc.
- New `mypyc/test/test_bitset.py` covers `BitSet` operations: same
index, different index, mixed with `set`, and the types the reflected
operations return.
This is independent of #22425 (which removes temporaries from the
must-defined universe). The two merge without conflicts and help in
different ways: #22425 makes the sets smaller, this PR makes each item
cost a bit instead of a hash table entry.
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 refcount and uninit transforms pass all values, including temporaries, as the universe of the must-defined analysis (
analyze_must_defined_regs).DefinedVisitoronly generates and kills registers, and both callers only check registers (is_maybe_undefinedin refcount,split_blocks_at_uninitsin uninit), so the temporaries are never needed. However, a must analysis starts with a copy of the universe for every basic block, so time and memory were O(blocks * values).This makes the refcount transform very slow for large functions, such as generated code or long module top levels. Benchmark: a function with n statements of the form
timing
insert_ref_count_opcodesalone (max RSS of the process):Fix: leave everything except registers out of the universe. The analysis results for registers are the same, so the output doesn't change:
mypy/checker.py,mypy/checkexpr.py,mypy/semanal.pyandmypy/messages.py(29 MB) is identical to master.For typical functions the gain is small (must-defined analysis on those four modules: 1.28s -> 1.14s), since they are short.
The remaining cost for large functions is still superlinear, because the must-defined sets contain O(registers) items per block and
run_analysisbuilds a new set for every op. Using bitsets could fix that, but it's a bigger change, so I left it out of this PR.