Skip to content

[mypyc] Use bitsets in data-flow analyses - #22426

Merged
JukkaL merged 1 commit into
python:masterfrom
Jahongir-Qurbonov:mypyc-dataflow-bitsets
Nov 13, 2026
Merged

JukkaL merged 1 commit into
python:masterfrom
Jahongir-Qurbonov:mypyc-dataflow-bitsets

Conversation

@Jahongir-Qurbonov

Copy link
Copy Markdown
Contributor

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:

    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.

run_analysis stored a Python set for every basic block and for every op
whose gen/kill sets were non-empty. For large functions these sets have
thousands of items, so the analyses used O(ops * values) memory and time.
The must-defined and borrowed-arguments analyses were the worst, since
they start from the full universe in every block.

Represent the sets as int bit masks instead. The results are BitSet
objects, which implement collections.abc.Set, so the users of the
analyses only needed annotation changes. The refcount transform shares a
BitIndex between its three analyses, so the set operations it does
between their results are integer operations.

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.

Cool, this speeds up C generation significantly, and also reduces memory use! I've thought about experimenting with bitsets myself, but never got around to it.

@JukkaL
JukkaL merged commit c76426e into python:master Nov 13, 2026
18 checks passed
@Jahongir-Qurbonov
Jahongir-Qurbonov deleted the mypyc-dataflow-bitsets 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