Base the allocation request on the currently occupied space, not the
full capacity.
The original code tended to lead to unnecessary doubling of the node
size in each copy, until we exhaust the 32 bit capacity value. (Oops.)
(Internally) standardize on using the word “slot” to refer to an id
for a location within a contiguous storage buffer that can hold a
single item of some kind.
“Offset” is sometimes used in public APIs (to refer to integer
distances within collections, relative to some start position),
and using the same term here causes internal naming difficulties.
A “bucket” is therefore a more specific name for a slot within a
hash table.
The Index type now represents a path into the tree,
consisting of a series of child offsets from the root node,
as well as the item offset within the final node.
(This all fits in a couple of words, as the depth of the tree
is fixed, and the offset path can always be compressed into a
single `UInt` value.)
To speed up basic operations, we also cache the final node
as an unsafe-unowned reference. Dereferencing this is extremely
unsafe, so the Index type carries an unsafe reference to the root
node along with a mutation counter, to allow reliable(*) index
validation. If index’s stored root is identical to the root of the
collection, and they have the same version value, then we can assume
that the index is still valid.
These provide a barebones, read-only, non-generic abstraction,
with just enough functionality to allow navigating around in
the tree structure.
We will use these to implement path-based indices.
If the key implements hashing properly, then this would be a clever
way to try another hash seed to differentiate between colliding keys.
However, if Key has proper hashing, then collision nodes are expected
to be tiny, so it’s better to simply do naive comparisons. If the Key
type has broken (i.e., partial) hashing, then loading colliding items
into a dictionary would just make == even slower.
So we’re better off comparing collidingh items via nested loops — so
let’s just unconditionally do that.
- Switch to using `ManagedBuffer` to represent node storage. This has
useful performance benefits, but it significantly complicates access
to node data. We no longer have direct access to the underlying raw
storage: everything needs to go through closures passed to
`ManagedBuffer.withUnsafeMutablePointers`.
- Introduce `struct _Node` and `struct _Node.UnsafeHandle` to
represent a (strong) reference to a hash tree node, and a
convenient handle for node storage, respectively. Use these instead
of directly accessing the ManagedBuffer object, whenever feasible.
- Implement separate paths for in-place mutations vs copy-on-write
operation, optimizing both flavors.
- Go to great lengths to eliminate unnecessary retain-release traffic,
whenever possible.
- Push subtree counts into parent nodes by moving the corresponding
field from the node header into the `_Node` struct. (This results in
a cool ~20x speedup for jumping around in the tree by integer
offsets.)
- Implement a type-punned empty root singleton, to allow creating
empty trees without allocating anything.
The original approach was a good one — gluing the level (aka shift) onto the hash value isn’t helpful when we’re passing around multiple hashes. The current level also seems like an important enough concept to keep as a standalone parameter.