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
BTreeMap/BTreeSet: sorted Vec with linear lookup and an O(n²) insert (remove(0) rebuild); needs binary search and in-place insert #937
Found in the container survey prompted by #936 (SlotMap). Same species: the name promises O(log n), the implementation is a linear scan.
lib/std/collections.w:
BTreeMap[K, V] is { entries: Vec[(K, V)] } kept sorted; BTreeSet[T] is { values: Vec[T] }.
last_index_of / contains / get walk entries from 0 with </> comparisons — O(n) lookup, no binary search.
insert (≈ line 104) scans to the insertion point, then rebuilds the whole Vec through reordered.push(self.entries.remove(0)) in two loops. Each remove(0) memmoves the remainder, so one insert is O(n²) and filling a map of n keys in non-ascending order is O(n³).
remove is entries.remove(idx) — O(n), acceptable for a sorted vector.
Fix
A sorted Vec is a fine representation at this scale; the operations must match it:
binary search for last_index_of/contains/get (O(log n));
insert via one Vec.insert(idx, value) (single memmove, O(n)) instead of the rebuild;
same for BTreeSet.
A real B-tree is not needed until profiles say so; the O(n²)/O(n³) cliffs are the bug.
Fixture: insert N keys descending, then ascending, then random; check keys() sorted and lookups; time bound sanity for N = 10k (should be milliseconds).
Ruling (2026-09-01): the engine comes from the migrated corpora (c-algorithms / TommyDS / STC, migrated whole; M*LIB's B+ tree surgically) with a With facade choosing the winner — see docs/stdlib_sourcing_plan.md and the note on #940. This issue stays open as the facade deliverable.
Closed by #2572 (Phase 4, D151) and #2579. BTreeMap/BTreeSet are facades over the MLIB B+tree (lib/std/engines/mlib, slot shape): complexity rows btree-map-/btree-set-* PASS (they were XFAIL on this issue), drop-audit cells btree_map_/btree_set_, behav_btree_engine.w. Measured (test/benchmark/stc_engines.w, n = 100000 i32, ns/op median of 3, insert/lookup/remove): BTreeSet 945/444/617 and BTreeMap 936/438/611 beside the retired sorted-List BTreeSet at n = 4000: 4371/481/4525. Upstream's tests/test-mbptree.c runs under With (test/corpora/mlib/programs/test_mbptree, all thirteen tests exit 0, in the mlib-test lane and the migrator gate). Copy class under D152: an engine-backed collection is a resource; a copy is .clone().
Activity
ehartford commentedon Sep 2, 2026
Ruling (2026-09-01): the engine comes from the migrated corpora (c-algorithms / TommyDS / STC, migrated whole; M*LIB's B+ tree surgically) with a With facade choosing the winner — see docs/stdlib_sourcing_plan.md and the note on #940. This issue stays open as the facade deliverable.
docs: stdlib sourcing plan — three corpora migrated whole, facades ch…
Complete callable FnAbi descriptors and stdlib Phase 0 (#1129)
C algorithms phase1 (#1142)
docs: refresh the lib/std plan against the tree and the sourcing plan…
Phase 3 facades over the STC engines (D148): PriorityQueue, OrderedSe…
Phase 4: BTreeMap and BTreeSet over the M*LIB B+tree (D151); D152; D1…
ehartford commentedon Oct 23, 2026
Closed by #2572 (Phase 4, D151) and #2579. BTreeMap/BTreeSet are facades over the MLIB B+tree (lib/std/engines/mlib, slot shape): complexity rows btree-map-/btree-set-* PASS (they were XFAIL on this issue), drop-audit cells btree_map_/btree_set_, behav_btree_engine.w. Measured (test/benchmark/stc_engines.w, n = 100000 i32, ns/op median of 3, insert/lookup/remove): BTreeSet 945/444/617 and BTreeMap 936/438/611 beside the retired sorted-List BTreeSet at n = 4000: 4371/481/4525. Upstream's tests/test-mbptree.c runs under With (test/corpora/mlib/programs/test_mbptree, all thirteen tests exit 0, in the mlib-test lane and the migrator gate). Copy class under D152: an engine-backed collection is a resource; a copy is .clone().