This initializer was interesting for OrderedDictionary, where it made
for more efficient initialization of storage, but it’s less useful for
other dictionary types.
BidirectionalCollection has the connotation that the ordering
of elements is meaningful, which isn’t the case for PersistentSet
and PersistentDictionary.
Building this package with library stability enabled
is entirely unsupported, but let’s add a few @frozen
attributes to fix such builds anyway.
This package is explicitly not ABI stable; the @frozen
attribute does not mean a guarantee to leave
layout unchanged in future revisions.
This sanitizes the semantics of _Node.Builder, removing
the (unneccessary) need to pass around hash paths.
It also slightly refactors removal operations, but stops short of
implementing node shrinking.
# Conflicts:
# Sources/PersistentCollections/Node/_Node+Builder.swift
# Sources/PersistentCollections/Node/_Node+Initializers.swift
# Sources/PersistentCollections/Node/_Node+Structural intersection.swift
# Sources/PersistentCollections/Node/_Node+Structural subtracting.swift
# Sources/PersistentCollections/Node/_Node+Subtree Modify.swift
# Sources/PersistentCollections/Node/_Node+Subtree Removals.swift
# Sources/PersistentCollections/PersistentDictionary/PersistentDictionary+Initializers.swift
# Sources/PersistentCollections/PersistentSet/PersistentSet+SetAlgebra Initializers.swift
# Sources/PersistentCollections/PersistentSet/PersistentSet+SetAlgebra intersection.swift
# Sources/PersistentCollections/PersistentSet/PersistentSet+SetAlgebra subtracting.swift
(cherry picked from commit c73a8caad1)
This is mostly about adding more type declarations to deal with type checker limitations in the older compiler.
This also includes workarounds that allow better support for CodingKeyRepresentable in PersistentDictionary, and it fixes a couple of small oversights on various #else branches.
Merging is best done in-place, as merging a non-empty dictionary will
always mutate the dictionary, so we don’t have to tiptoe around
uniqueness issues.
(The union operation on PersistentSet is vaguely similar, but not
quite — unlike with merge, union is able directly link shared subtrees
into the result. However, a similar optimization may be worthwhile for
symmetricDifference.)
To prevent confusion with the low-level _Node.update method.
This also switches PersistentSet.insert to use `_Node.insert` instead of `_Node.updateValue`, to fix its semantics. `insert` no longer invalidates indices when the new member already exists in the collection.
We don’t actually need this, as `_customContainsEquatableElement`
returning non-nil is a perfectly suitable indication that containment
checking is fast.
This adds an experimental _FastMembershipCheckable protocol to speed
up these relationship predicates when possible — having separate
overloads for each known collection with a fast containment check
is not going to scale for long.
This commit also introduces a set of isEqual(to:) methods that
generalize ==.
The new executable target `memory-benchmark` collects some memory usage statistics and outputs average memory efficiency for a range of sizes in CSV format.
Bump required toolchain version for benchmarks to Swift 5.5.
This will simplify merge operations as we no longer have to spend effort on preventing repeated rehashings of items within collision nodes.
Also, review and simplify node allocation and lookup code.
(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.
- 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.