regalloc2 makes too many memory allocations
Nobody has claimed this yet.
Assessment
- Difficulty
- 5/5
- Estimated time
- Over a week
- Newbie friendliness
- 35/100
- Issue type
- Refactor
- Clarity
- Mostly clear
- Activity status
- Stale
- Tech stack
- rust
- Domain
- compilers, performance
Research direction
Start with the named hotspots: try_to_allocate_bundle_to_reg, merge_bundles, insert_use_into_liverange, add_liverange_to_vreg, create_pregs_and_vregs, and Env::new. Review how Env and the listed SmallVec, HashSet, BTreeMap, and Vec fields are created and reused, then evaluate a Context design using cranelift-entity and cranelift-bforest. Done means reducing repeated allocations while preserving allocator behavior and addressing the relationship to #62.
Written by the indexing model from the issue text.
Description
I used heaptrack to analyze the memory allocation patterns of my compiler which uses regalloc2. I used a large benchmark (LLVM library) which has 1257955 basic blocks in 104913 functions. Out of a total of 46780667 allocations, 46779011 of them come from regalloc2 and only 1656 come from other parts of the compiler.
My compiler uses a similar structure [^1] to Cranelift where a Context structure allows memory allocations to be reused across compilation units. Specifically, this allows various Vecs and HashMaps to grow to a sufficient size only once while being reused many times for compiling multiple functions.
[^1]: In fact I am using the cranelift-entity crate directly, it's very well written.
regalloc2 unfortunately doesn't follow this design:
- A fresh
Envstructure is allocated and freed for each function. No memory allocations are preserved. - Heavy use of temporary
SmallVecs andHashSets. Heaptrack reports that 19865290 allocations (42% of the total) are "temporary". These are allocations which are immediately followed by a free, with no other allocations in between. - Heavy use of
BTreeMapwhich needs to allocate many nodes separately and cannot reuse memory unlikeHashMapandVec.
Heaptrack results
Here are the major allocation hotspots shown by heaptrack:
In try_to_allocate_bundle_to_reg:
- 16158324 temporary allocations from the
FxHashSet. - 9677435 allocations from inserting liveranges into
PRegData::allocations(LiveRangeSetbacked by aBTreeMap).
In merge_bundles:
- 4478881 allocations from pushing a
VRegtoSpillSet::vregs(SmallVec). - 1895443 allocations from appending
LiveRangeListEntrys toLiveBundle::ranges(SmallVec).
In insert_use_into_liverange:
- 2578957 allocations from pushing a
UsetoLiveRange::uses(SmallVec).
In add_liverange_to_vreg:
- 1432853 allocations from pushing a
LiveRangeListEntrytoVRegData::ranges(SmallVec).
In create_pregs_and_vregs:
- 948582 allocations from pushing to
Env::vregsandEnv::allocsandEnv::inst_alloc_offsets. (Vec)
In Env::new:
- 1154043 allocations from initializing the various
Vecs withVec::with_capacity.
Performance impact
Benchmarking shows that approximately 10% of the time is spent in memory allocation functions (malloc, realloc, free). Additionally, another 10% of the time is spent in memcpy/memmove which could be due to vector reallocation.
Compiling with multiple threads can cause additional contention on the global allocator. This was noticed on musl targets where the global allocator doesn't have thread-local caches: compiler performance on 6 threads (on a 6-core machine) was reduced to that of a single thread whereas other allocators allow almost-perfect performance scaling.
Proposed solution
I believe the best way to improve regalloc2 would be to use a design similar to Cranelift with a Context structure that can preserve memory allocation across multiple invocations of the register allocator.
- Temporary
SmallVecs andHashSets should be replaced with a single instance inContext. - The many
BTreeMaps should be replaced withcranelift-bforestwhich maintains a pool of nodes that can be reused. - The many
SmallVecs should be replaced withEntityListfromcranelift-entityand aListPoolinContext.
Since this would require a dependency on the cranelift-entity and cranelift-bforest crates, this is also a good opportunity to resolve #62 and switch to using proper indexed types internally.
- Dominant language
- Rust
- Stars
- 266
- Forks
- 54
- PR merge metrics
- No merged PRs in 30d
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from bytecodealliance/regalloc2
-
Difficulty 4/5 3-5 days Newbie friendliness 48/100
bytecodealliance/regalloc2#265 · 7 comments ·
-
Difficulty 5/5 Over a week Newbie friendliness 25/100
bytecodealliance/regalloc2#247 · 3 comments ·
-
Difficulty 4/5 3-5 days Newbie friendliness 45/100
bytecodealliance/regalloc2#222 · 4 comments ·
-
Difficulty 5/5 Over a week Newbie friendliness 25/100
bytecodealliance/regalloc2#206 · 2 comments ·
-
Difficulty 3/5 1-2 days Newbie friendliness 35/100
bytecodealliance/regalloc2#194 · 7 comments ·
All issues in bytecodealliance/regalloc2
Similar issues
-
todo:perf
Difficulty 2/5 1-3 hours Newbie friendliness 84/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
-
ACP agents get no MCP servers when the thread is created before the project's first worktree loads Openstate:needs triage
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
zed-industries/zed#64611 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
ontola/atomic-server#1625 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
objectionary/phie#154 ·