You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
engine: unit inference deep-copies unit maps on every clone #318
Unit inference clones UnitMap (a BTreeMap<String, i32> of unit name to exponent) many times while substituting and unifying constraints.
Wrapping the map in an Arc would make those clones free.
Example
On WRLD3, one compile spent about 266 KB in about 830 allocations cloning UnitMaps inside ConstraintSet::substitute and unify (DHAT, early 2026; not re-measured since).
Why it matters
Little.
Those numbers are small next to a whole compile, and no one has shown unit inference to be a noticeable part of compile time.
Fix
Change UnitMap to hold Arc<BTreeMap<String, i32>>.
Note the original premise that UnitMap is never mutated is wrong: reciprocal, exp, and multiplication modify the map in place, so those would need Arc::make_mut, which copies whenever the map is shared, and could eat most of the saving. UnitMap.map is also a public field used in about 25 places.
Worth doing only if a profile shows unit inference matters.
changed the title [-]Make UnitMap cheaply cloneable via Arc to reduce per-context allocation overhead[/-][+]engine: unit inference deep-copies unit maps on every clone[/+]on Oct 12, 2026
Measured cost is ~266 KB / ~830 allocations on WRLD3, negligible next to a compile, and the premise is wrong: UnitMap is mutated in place (reciprocal, exp, Mul), so Arc would need make_mut copies anyway.
Activity
[-]Make UnitMap cheaply cloneable via Arc to reduce per-context allocation overhead[/-][+]engine: unit inference deep-copies unit maps on every clone[/+]bpowers commentedon Oct 12, 2026
Closing as won't fix.
Measured cost is ~266 KB / ~830 allocations on WRLD3, negligible next to a compile, and the premise is wrong: UnitMap is mutated in place (reciprocal, exp, Mul), so Arc would need make_mut copies anyway.