Skip to content

SlotMap: O(n) insert scans a byte-per-slot occupancy array; add a free list (FIFO) for O(1) allocation #936

Closed

Description

@ehartford

Reported by Michael (Miguel) Howard after reading the runtime SlotMap (rt/rt_core.w, "SlotMap operations", ~line 2685). Verified against the source:

  • with_slotmap_insert_out (rt/rt_core.w:2794) finds a free slot by scanning occupied linearly from index 0 on every insert — O(n) per allocation, O(n²) to fill a map — and panics if the scan finds nothing after sm_grow.
  • occupied is one byte per slot (layout: values, occupied, generations, len, cap, elem_size; 48-byte header).
  • There is no free list; freed slots are only rediscovered by the scan.

Suggested change (Miguel's)

  1. Keep a free list so allocation is O(1). The occupied byte array can go: a slot is free iff it is on the free list (or the generation carries a tombstone bit), which also drops a byte per slot.
  2. Make the free list FIFO rather than LIFO, so a pathological alloc/free loop on one handle cycles through all free slots instead of hammering one slot's generation toward rollover (§2.5.5 generational handles: rollover would let a stale handle validate).

Sequencing per Miguel: "at the point when performance becomes important" — not urgent, but it is the data-structure shape §1 promises for pools, arenas, and ECS stores. He offered to do it.

Notes for the implementer

  • sm_normalize_generation / sm_write_handle / sm_valid define the handle contract; a tombstone-in-generation design must keep sm_valid rejecting freed slots and never hand out generation 0 if that is the reserved value.
  • The stdlib surface lives in lib/std/collections.w; the spec's guarantees are §2.5.5 and the §1 mission text on contiguous pools.
  • A throughput fixture (insert N, remove half, insert N/2, check handles) belongs in test/behavior alongside the change; the drop audit covers ownership of values.

Activity

  1. migueldecimal128 commented on Sep 2, 2026

    @migueldecimal128
    Contributor

    There is another issue related to SlotMap allocation. The current naive implementation uses parallel arrays that double in size when capacity is exceeded. This is the correct behavior for SlotMaps with relatively low cardinality. However, if they become big then I don't this is the correct behavior. If a SlotMap has 8M entries, I don't think one wants to be allocating new 16M arrays and copying all of the existing arrays. I think one wants to start the same way, but switch to a page lookup table when the cardinality gets large. Page size to be something like 64k to 1M. I will look at the Rust implementation of SlotMap to see how they handle this.

  2. migueldecimal128 commented on Sep 2, 2026

    @migueldecimal128
    Contributor

    Consider ... The current implementation uses 0xFFFF_FFFF as a sentinel. When I have built data structures like this in the past I have found it convenient to reserve handle 0 so that it is never used ... looks more like NULL pointer. Since these handles contain a non-zero generation, it is not strictly necessary. However, the free-list will need a sentinel, so 0 might be slightly preferable.

  3. migueldecimal128 commented on Sep 2, 2026

    @migueldecimal128
    Contributor

    Instead of wrapping to 0 then skipping, the global generation counter should cycle from 1 thru 2**32-5, the largest prime.

    counter = if counter == 4294967291: 1 else: counter + 1
    

    That will eliminate any issues with wrapping with pathological alloc/dealloc patterns that happen to be prime factors.

  4. ehartford commented on Sep 3, 2026

    @ehartford
    ContributorAuthor

    It seems like the implementation will require Sort - I think you could use a placeholder for now - or, I am actively working on the real Sort (along with all the other basic data structures and algorithms). I expect to finish tomorrow or so.

    #940.

  5. migueldecimal128 commented on Sep 3, 2026

    @migueldecimal128
    Contributor

    SlotMap implementation should not require any sorting.

  6. added a commit that references this issue on Sep 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

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