Skip to content

A Sema table with ~1500 live of ~3570 entries takes 306M lookups in one check: find it and ask whether it wants a map #3100

Closed

Description

@ehartford

From #3093's threshold mode (WITH_TRACE_HM_PROBES=1000000): the heaviest single map freed in check src/main.w is a minted 4-byte-key map, 1520 live entries of 3569 ever inserted (an 8192-slot index), freed by a round's Sema going out of scope, after 306,458,751 lookups: 200k lookups per live entry, with constant insert/remove traffic (the dead entries are tombstones the index carries until it rebuilds).

Which table: a per-node or per-symbol map that a check pushes and pops as it enters and leaves scopes or bodies (a scope table, a live-binding set, a "current" marker set). Route: WITH_TRACE_HM_PROBES=100000000 reports it at its free with a backtrace; its shape (len/entries/index-cap) is in the report, and the Sema field is found by matching the shape against the tables Sema drops in that order, or by breaking on with_hashmap_free under lldb with the reported address.

Then: a table pushed and popped in LIFO order is a stack or an epoch-stamped array, not a map (SemaCheck.w's binding_use_epoch is the pattern already in use); the lookups it takes should fall by orders of magnitude, and check src/main.w by a measurable share.

Activity

  1. ehartford commented on Nov 8, 2026

    @ehartford
    ContributorAuthor

    Sampled instead of guessed (sample, 15 s, on with-stage2 check src/main.w): hm_find_slot is 10% of self time across all maps, so the 306M-lookup table is real but not the top item. The table is almost certainly named_types: lookup_named_type_filtered reads it twice per call, and Self and every generic type parameter are inserted into it and removed again around each body and impl (the comment at its scoped tier says so), which is the insert/remove churn the shape showed (1520 live of 3569 entries). Two leads from the same sample outrank it: the allocator's large-block path (~20%, filed as its own issue) and the per-scope snapshots (save_scope_states, snapshot_poison_syms/nodes, ~5%, which also feed the large-block path from pop_scope). This issue stays for the Self/type-parameter overlay (a small LIFO binding stack consulted before named_types) once those two have landed, when its share can be measured in isolation.

  2. ehartford commented on Nov 8, 2026

    @ehartford
    ContributorAuthor

    Tried the Self slot (branch i3100b, commit 9070f9a, gate green, not merged): Self kept in self_type_tid instead of being inserted into and removed from named_types around every impl and body, with flat_named_type as the one read path. Result: no measurable change. check src/main.w 77 s (main) vs 82 s (slot), within noise, and the heavy table is not named_types: with Self out of that table the 306M-lookup map still shows 1520 live of 3599 entries, so its churn is something else, and the removal traffic was never the cost (probing averages 1.18 slots). Not landing it: a slot and an accessor with no measured gain is code for its own sake.

    What is still true: one 4-byte-minted-key table takes 306M lookups per check, a 208-entry one 325M, a 645-entry one 300M, a 167-live-of-395 one 157M (freed from the compilation's drop and from each round's Sema). Those are lookup counts: the fix is calling less, which needs the call sites. The threshold report now needs to say which field: the cheap way is to print the map's address in the report and have Sema print self's address at check_module under the same switch, so map - self is the field offset, read against the type layout (WITH_DEBUG_TYPE_LAYOUT=1 names fields in order but not offsets; the drop glue's disassembly at the freeing PC gives the offset directly). That is the next step here, not the overlay.

  3. ehartford commented on Nov 9, 2026

    @ehartford
    ContributorAuthor

    Identified (a scratch build printed every Sema map field's address at check_module, and the free report its map's address; WITH_TRACE_HM_PROBES=300000000 with-stage2 check src/main.w, tree at #3105 so Sema runs once):

    field lookups in one check shape (live/entries/index)
    generic_inst_templates 581M 214/214/512
    type_decl_nodes_by_tid 537M 645/645/2048
    scope_name_map 308M 1520/4056/8192

    So the Self hypothesis was wrong twice over: the churned table is scope_name_map (bindings pushed and popped per scope: the LIFO case), and the two biggest by lookups are not churned at all, they are tiny tid-keyed tables read half a billion times each. Those two are the cheap win: a type id is a dense small integer, so a List[i32] indexed by tid (grown in add_type, 0 for none) answers in one load with no hash, no probe, no contains+get pair. scope_name_map is the second step: a scope's bindings are a stack, and a name→binding lookup over a stack of ~1500 can be a per-symbol head index (the pattern named_type_candidate_head already uses) instead of a map that tombstones on every pop.

  4. added 2 commits that reference this issue on Nov 9, 2026
  5. ehartford commented on Nov 9, 2026

    @ehartford
    ContributorAuthor

    Landed in #3107 and #3108 (main d1fc508): the two tid-keyed tables and the scope table are Lists indexed by their dense ids. check src/main.w 82–86 s → 70–71 s on top of the single-pass Sema. The remaining heavy tables are the symbol pool (str keys, 1.02 slots per lookup, fine) and per-node tables read once per node; nothing left here that a table shape would fix.

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions