Skip to content

Large allocations (>4 KiB) are mmapped one by one and tracked in a shifting sorted array: ~20% of check src/main.w by sampling #3103

Closed

Description

@ehartford

sample (15 s, 12,483 samples of the main thread) on with-stage2 check src/main.w (tree at #3101), self time by frame:

frame samples
hm_find_slot (all maps) 1292
rt_range_store 1032
rt_memcmp 651
current_worker_fiber + pthread_self (per allocation, the lock) ~930
rt_record_large_range 499
rt_forget_large_range 427
__mmap + __munmap 492
Sema.get_type_kind 497
Sema.save_scope_states, snapshot_poison_syms, snapshot_poison_nodes 635

So about a fifth of the check is the allocator's large-block path: every allocation over RT_LARGE_THRESHOLD (4096) is its own mmap, freed with munmap, and recorded in a sorted array by shift-insert (rt_record_large_range) and shift-remove (rt_forget_large_range), O(n) each, with n the number of live large blocks. The callers that allocate large blocks most in the sample: Sema.pop_scope (350), check_concrete_trait_method_call → find_dyn_trait_method_info → lookup_trait_named (322–348), check_if_expr (~180 each at three sites) — List buffers over 4 KiB made and freed per scope or per method-call check (save_scope_states and the poison snapshots copy lists per scope).

Two fixes, both in rt/rt_core.w and both measurable with the same sample:

  1. A cache of freed large blocks by rounded size (pages, power of two up to some ceiling), so a scope's snapshot list reuses the block the previous scope freed instead of munmap + mmap; WITH_ALLOC_NO_REUSE keeps the debug behavior.
  2. Ownership by page hash, not a sorted array: slabs already map page → start through rt_owned_put; large blocks can use the same table (one entry per page of the block, or per first page with the size in the header), making record/forget O(pages) with no shifting, and rt_payload_start_is_owned's large branch a hash probe instead of a binary search.

Then the Sema side (#3100): why pop_scope and a trait-method lookup allocate over 4 KiB per call; a snapshot that copies a whole list per scope wants a stack mark or an epoch.

Activity

  1. added 2 commits that reference this issue on Nov 8, 2026
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