Skip to content

[mypyc] Only track registers in must-defined analysis - #22425

Merged
JukkaL merged 1 commit into
python:masterfrom
Jahongir-Qurbonov:mypyc-must-defined-universe
Nov 12, 2026
Merged

JukkaL merged 1 commit into
python:masterfrom
Jahongir-Qurbonov:mypyc-must-defined-universe

Conversation

@Jahongir-Qurbonov

Copy link
Copy Markdown
Contributor

The refcount and uninit transforms pass all values, including temporaries, as the universe of the must-defined analysis (analyze_must_defined_regs). DefinedVisitor only generates and kills registers, and both callers only check registers (is_maybe_undefined in refcount, split_blocks_at_uninits in 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

    a{i} = xs[{i % 7}] + {i}
    if a{i}:
        t = a{i}

timing insert_ref_count_opcodes alone (max RSS of the process):

n ops blocks master this PR
1000 9k 4k 0.86s, 1.2 GiB 0.33s, 0.27 GiB
2000 18k 8k 3.8s, 4.7 GiB 1.8s, 1.1 GiB
4000 36k 16k ~110s, 15 GiB 6.2s, 3.3 GiB

Fix: leave everything except registers out of the universe. The analysis results for registers are the same, so the output doesn't change:

  • The generated IR for the benchmark functions is identical.
  • The generated C for mypy/checker.py, mypy/checkexpr.py, mypy/semanal.py and mypy/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_analysis builds 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.

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 JukkaL left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Good catch! I verified that this improves compilation perf and memory use significantly, and the generated C remains identical.

@JukkaL
JukkaL merged commit c59a7a7 into python:master Nov 12, 2026
18 checks passed
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.
@Jahongir-Qurbonov
Jahongir-Qurbonov deleted the mypyc-must-defined-universe branch November 14, 2026 22:04
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.

2 participants