Skip to content

engine: unit inference deep-copies unit maps on every clone #318

Closed as not planned

Description

@bpowers

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.

Pointers

  • src/simlin-engine/src/datamodel.rs (UnitMap)
  • src/simlin-engine/src/units_infer.rs (substitute, unify)

Activity

  1. added
    engine
    on Jun 8, 2026
  2. 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
  3. added
    enhancement
    wontfix
    on Oct 12, 2026
  4. bpowers commented on Oct 12, 2026

    @bpowers
    OwnerAuthor

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    engineenhancementwontfix

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions