[BasicContainers] Scramble small hashed containers

Don’t just append new items to the end of storage — it misleads clients into assuming that the containers preserve insertion order, when they most certainly don’t do that.

Instead, insert new items into the middle of storage, using an extra swap.
This commit is contained in:
Karoy Lorentey committed 2026-03-10 23:06:08 -07:00
1 parent e69d570c59
commit 5636b32db7
2 files changed
+24 -6

No files matched your search

@@ -18,7 +18,10 @@ extension _HTable {
swapper: (Bucket) -> Void,
) -> Bucket {
assert(isSmall)
// FIXME: Scramble order by swapping things in some way
if _count > 0 {
// Scramble order by swapping the new item in the middle of the table.
swapper(Bucket(offset: _count / 2))
}
let r = Bucket(offset: _count)
_count &+= 1
_maxProbeLength = _count