Skip to content

Sema spends ~35% of check src/main.w in SipHash on i32 keys #2372

Closed

Description

@ehartford

Problem

with check src/main.w takes 109 s on eric-5090 (106 s user, single-threaded,
release compiler at 4716e86). Profiling it with perf record -g, self time:

15.2%  sip_round
 9.9%  with_hasher_write
 7.0%  hm_find_slot
 5.6%  with_hasher_finish
 4.4%  hm_keys_eq
 3.9%  with_hasher_init
 3.1%  hm_hash_key

About 35% of the self-check is SipHash, hashing i32 keys: symbol IDs, node
IDs and type IDs in Sema's HashMap[i32, _] tables (scope_name_map,
binding_value_nodes, needs_drop_visit, the type caches). SipHash resists
hash flooding from attacker-chosen keys. A compiler's own symbol table has
no attacker, which is why rustc uses FxHash internally and Go and Zig use
cheap integer hashes.

The quadratic scope exit (2b4a12e removed the uncached type_owns_user_drop
walk, worth about 37%) has two remaining loops: check_live_views_for_origin
and poison_live_views_for_origin scan every live binding in the function at
every scope exit. Each scan does a scope_name_map lookup per binding, even
though the binding's index bi is already in hand.

Proposal

  • Integer keys hash with a cheap mixer (a multiply-xorshift, FxHash-style)
    wherever the key is not attacker-controlled. Decide whether that is a
    property of the key type (i32/u32/i64 keys) or of a declared
    compiler-internal map. That is a std question, so it gets a brief before
    any change.
  • Dense ID spaces (symbol, node and type IDs) use direct-indexed side tables
    instead of hash maps where the hot paths read them.
  • The scope-exit scans read bind_provenance[bi] directly, and an
    origin → dependents index replaces the scan over every live binding. First
    check how scope_name_map resolves a shadowed binding, because the index
    and the map can disagree there.

Expected effect: roughly halve check src/main.w, and with it the gate,
battery and fixpoint times, and the margin on every timeout in the battery.

Activity

  1. ehartford commented on Oct 14, 2026

    @ehartford
    ContributorAuthor

    Brief (four parts) for the open question in this issue: how integer keys hash in HashMap. It is on the battery-time critical path: on a profiled -O1 compile of the compiler (release binary, today) Sema is 40 s of 68 s, and #2372's perf shows ~35% of a self-check in SipHash on i32 keys.

    1. What the others do (checked in .reference/). Rust std: SipHash-1-3 with a per-process random key for every key type (library/std/src/hash/random.rs); rustc itself uses FxHash for its internal tables (rustc_data_structures/src/fx.rs). Go: a per-map random seed, with a fast 32/64-bit path (runtime/alg.go memhash32/memhash64, AES or wyhash-style mixing). Zig: Wyhash with seed 0 (std/hash_map.zig, no randomization). Swift: SipHash-1-3, per-process seed. Mojo: AHasher with a fixed seed (hashlib/hasher.mojo).

    2. What With does today. rt/rt_core.w: SipHash-1-3, keyed once per process from the OS's randomness, for every key; a map whose key kind is 2 (a user Hash impl) calls its own hash_fn. No spec section rules the hash; the runtime comment states the flooding rationale.

    3. Mission fit. Flooding resistance matters for maps keyed by input an attacker chooses (strings from a network). It costs 30–40 ns per integer key in every program, including the compiler, whose keys are symbol, node and type IDs. Go's shape keeps both: the random per-process seed stays, and fixed-size keys of 8 bytes or less take a seeded multiply-mix (one or two multiplies, a few ns), while str and other byte keys keep SipHash. A compiler-only map type would also work but adds a second map spelling to the compiler source, which is ceremony that the type already decides. Unseeded FxHash for everyone (Zig/Mojo) is dropped: it gives up flooding resistance on integer keys entirely.

    4. Prediction. Eric rules: "integer and other fixed-size keys of 8 bytes or less hash with a seeded multiply-mix; str keys keep SipHash-1-3; the per-process random seed stays" (70%). Not delegated: it is below 75% and changes a security property of every user's maps.

    Measured effect to expect: sema.bodies and mir.lower both read these tables; the issue's perf puts the hashing at ~35% of a self-check, so roughly 15–20 s off each compile of the compiler, and the same fraction off every test compile.

  2. ehartford commented on Oct 25, 2026

    @ehartford
    ContributorAuthor

    Triage 2026-10-25, with v0.15.3.0 (main a261b65): reproduced, and the next step is Eric's ruling. Non-str keys still hash through SipHash: hm_hash_key (rt/rt_core.w:4069) uses a type's own hash_fn when set and otherwise runs with_hasher_init/with_hasher_write (sip_round, :3927).

    The question is the one the brief above frames: should fixed-size keys of 8 bytes or less hash with a seeded multiply-mix while str keys keep SipHash-1-3 and the per-process random seed stays? The brief predicts yes at 70%, below the D98 delegation bar, because it changes a security property of every user's maps.

  3. added this to the post-v0.16.0 milestone on Oct 28, 2026
  4. ehartford commented on Nov 7, 2026

    @ehartford
    ContributorAuthor

    Ruled as D196 (Eric, 2026-11-08; docs/meetings/2026-11-08-D196-…md): the per-process seed stays the default hasher for every key type (D96), and a map or set whose keys are IDs the program minted names the unseeded hasher where it is declared — HashMap[K, V, Minted], HashSet[T, Minted] — a multiply-mix, never identity, refused over a non-integer key, never chosen by key type. Iteration order is insertion order either way, so the sweep is a performance change the fixpoint proves.

    Landing in two steps: (1) the hasher (runtime key kind, std.collections.Minted, Sema's arity check with the hasher slot — which also closes the silent acceptance of HashMap[i32, i32, i32] — both backends, spec §11.7, fixtures); (2) the src/ sweep, which waits for a seed that knows Minted since src/ is compiled by the pinned seed against its std. The measurement for (2) comes from a stage2 built from a swept tree, before the sweep is committed. After it, the probe count is the next thing to measure, per the ruling.

  5. added 2 commits that reference this issue on Nov 7, 2026
  6. ehartford commented on Nov 8, 2026

    @ehartford
    ContributorAuthor

    D196 measured on the compiler's own maps (PR #3084, 772 maps Minted): check src/main.w with the unswept and swept compilers on the same source, alternating, on a loaded machine (load 20–30): unswept 177 / 130 / 125 s, swept 102 / 108 / 100 s. Roughly a fifth off, in line with the first measurement (123.8 → 98.7 s). The probe-count measurement the ruling asked for next is still open.

  7. ehartford commented on Nov 8, 2026

    @ehartford
    ContributorAuthor

    Done: D196 landed (#3069), the compiler's own 772 integer-keyed maps hash with Minted (#3084), and check src/main.w runs about a fifth faster (unswept 125–177 s vs swept 100–108 s under load; 123.8 → 98.7 s in the first quiet measurement). The ruling's next measurement, the probe count, is its own issue.

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions