You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
SlotMap: O(n) insert scans a byte-per-slot occupancy array; add a free list (FIFO) for O(1) allocation #936
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)
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.
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.
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.
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.
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.
Activity
docs: stdlib sourcing plan — three corpora migrated whole, facades ch…
migueldecimal128 commentedon Sep 2, 2026
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 tablewhen 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.migueldecimal128 commentedon Sep 2, 2026
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.
migueldecimal128 commentedon Sep 2, 2026
Instead of wrapping to 0 then skipping, the global generation counter should cycle from 1 thru 2**32-5, the largest prime.
That will eliminate any issues with wrapping with pathological alloc/dealloc patterns that happen to be prime factors.
ehartford commentedon Sep 3, 2026
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.
migueldecimal128 commentedon Sep 3, 2026
SlotMap implementation should not require any sorting.
Complete callable FnAbi descriptors and stdlib Phase 0 (#1129)
C algorithms phase1 (#1142)