Repository navigation
[mypyc] Use bitsets in data-flow analyses - #22426
Merged
Merged
Conversation
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
approved these changes
Nov 13, 2026
JukkaL
left a comment
Collaborator
There was a problem hiding this comment.
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.
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.
run_analysiskeeps a Pythonsetfor 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
intbit masks:BitIndexassigns a bit position to each value an analysis sees.BitSetobjects that implementcollections.abc.Set, so the code using the results only needed annotation changes (set[Value]->AbstractSet[Value]in refcount.py).analyze_live_regs,analyze_borrowed_argumentsandanalyze_must_defined_regstake an optionalindex. The refcount transform shares one index between the three, because it combines their results (e.g.live - live - borrowedafter a branch); with a shared index these operations are integer operations too. With different indexes,BitSetstill works and, likeset, iterates over the smaller operand.set - bitset,set & bitset,set | bitset) return a realset, since typeshed says the result is asetand compiled code checks this.find_always_defined_attributesrelies on it.Benchmark
A function with n statements of this form:
Each analysis timed separately, then
insert_ref_count_opcodes(interpreted). Max RSS is for the whole process: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.pyandmypy/messages.pywith the analysis and transform modules compiled by mypyc:insert_ref_count_opcodesinsert_uninit_checksTesting
dataflow.pyand its users compiled by mypyc.mypyc/test/test_bitset.pycoversBitSetoperations: same index, different index, mixed withset, 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.