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
Evicting oldest-first from InsertionOrderMap/ContiguousMap is quadratic: emptying a 40k map takes ~3.6 s #101
Remove(TKey) on both insertion-ordered maps rewrites the key→index dictionary for every entry after the removed one:
Containers/InsertionOrderMap.cs:283-288: for (i = index; i < items.Count; i++) keyToIndex[items[i].Key] = i;
Containers/ContiguousMap.cs:375-410 (loop at ~:404): same pattern.
Remove is documented as O(n), but the O(n) here is a hash lookup+write per shifted element rather than a memmove, and it is hit worst by the most natural use of an insertion-order map: evicting the oldest key (LRU/FIFO caches), which shifts every remaining entry.
Measured (Release; remove keys 0..n-1 in insertion order)
n
InsertionOrderMap
ContiguousMap
Dictionary
10k
301 ms
277 ms
0 ms
20k
893 ms
919 ms
0 ms
40k
3572 ms
3708 ms
0 ms
4× per doubling, so quadratic; ~22 s extrapolated at 100k.
Suggested fix / acceptance criteria
InsertionOrderMap (no span API, so layout is free): tombstone removed slots and compact + reindex once tombstones exceed ~half the list, giving amortised O(1) Remove while preserving order. Enumeration and IVersionedEntries.KeyAt/ValueAt must skip tombstones (or compact before enumerating).
ContiguousMap (contiguity/AsSpan must hold): at minimum add a bulk RemoveWhere(Predicate<...>)/RemoveRange that compacts and reindexes once (O(n) total); optionally a head offset to make removing index 0 cheap.
Add a benchmark removing oldest-first at n = 100k.
Distinct from #82 (Ordered* bulk construction) and #83 (ContiguousSet/InsertionOrderSet set operations).
Category: Improvement. Algorithmic performance; it is labelled enhancement.
Priority: Medium. It is quadratic in the most natural use of an insertion-ordered map (oldest-first LRU/FIFO eviction): 3.6 s at 40k and about 22 s at 100k. Results are correct, only slow.
Suggested area / assignment:Containers/InsertionOrderMap.cs and Containers/ContiguousMap.cs (Remove paths)
Recommended next step: Do the InsertionOrderMap tombstone and amortised-compaction fix first, because it has no layout constraint. Then add RemoveWhere/RemoveRange to ContiguousMap, and add the 100k oldest-first benchmark.
Activity
matt-edmondson commentedon Oct 6, 2026
Triage
enhancement.Containers/InsertionOrderMap.csandContainers/ContiguousMap.cs(Remove paths)ContiguousMap.cswith ContiguousMap.GetKeysSpan/GetValuesSpan allocate and copy the whole map on every call, making indexed loops O(n²) #102, so coordinate edits.Recommended next step: Do the
InsertionOrderMaptombstone and amortised-compaction fix first, because it has no layout constraint. Then addRemoveWhere/RemoveRangetoContiguousMap, and add the 100k oldest-first benchmark.Generated by Claude Code