Commit Graph
33 Commits
Author SHA1 Message Date
Karoy Lorentey f79fd630bf [PersistentCollections] _Node.isDisjoint: Add new fast path 2022-09-22 16:33:55 -07:00
Karoy LorenteyandMichael Steindorfer f2539a4b21 Update Sources/PersistentCollections/Node/_Stack.swift
Co-authored-by: Michael Steindorfer <michael@steindorfer.name>
2022-09-22 16:31:21 -07:00
Karoy LorenteyandMichael Steindorfer 24619dfe31 Update Sources/PersistentCollections/Node/_Node+isDisjoint.swift
Co-authored-by: Michael Steindorfer <michael@steindorfer.name>
2022-09-22 11:31:11 -07:00
Karoy Lorentey 958fd47539 [PersistentSet] Start drafting the new type 2022-09-21 10:45:20 -07:00
Karoy Lorentey 04b134e9aa [PersistentCollections] Fix memory management for the hash tree iterator
This slows iteration down by about 37% percent but it now works within ManagedBuffer’s requirements.
2022-09-21 10:45:20 -07:00
Karoy Lorentey c355812e53 [PersistentDictionary] Small cleanups 2022-09-21 10:45:20 -07:00
Karoy Lorentey 8d7c777e47 [PersistentCollections] _children → children, itemEnd → itemsEndSlot, childEnd → childrenEndSlot 2022-09-19 21:25:07 -07:00
Karoy Lorentey 71c881cfdb [PersistentDictionary] mapValues, compactMapValues, filter 2022-09-19 21:25:07 -07:00
Karoy Lorentey 75edf6cd69 [PersistentDictionary] Add support for merging dictionaries & for in-place value mutations 2022-09-19 21:25:07 -07:00
Karoy Lorentey ea30715a6c [PersistentDictionary] Implement Keys and Values views 2022-09-19 21:25:07 -07:00
Karoy Lorentey 310f801deb [Utilities] Add UInt16 rank utilities 2022-09-19 21:25:07 -07:00
Karoy Lorentey a2491aac45 [PersistentDictionary] Improved copy & resize operations
Don’t copy the current node and then insert a new item into it.
Instead, create the copy with the new item already inserted.

Ditto for new collisions.
2022-09-19 21:25:07 -07:00
Karoy Lorentey f7c419d7cf [PersistentDictionary] Rework allocations 2022-09-19 21:25:07 -07:00
Karoy Lorentey 53895c6537 [PersistentCollections] Split up node mutations into multiple files 2022-09-19 21:24:37 -07:00
Karoy Lorentey b263fb442e [Benchmarks] Move source files under Sources/
This eliminates the need to use path overrides.

Also, explicitly define a product for the “benchmark” executable.
2022-09-19 16:58:25 -07:00
Karoy Lorentey d45f264ef0 [PersistentDictionary] Fix index(forKey:) performance 2022-09-17 20:04:19 -07:00
Karoy Lorentey 5240bf6e0a [PersistentCollections] Reduce _Bucket’s storage size 2022-09-17 17:20:34 -07:00
Karoy Lorentey 2321b34d80 [PersistentCollections] Fix node sizing logic
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.)
2022-09-17 17:20:34 -07:00
Karoy Lorentey 57deed5f0f [PersistentDictionary] Implement in-place mutations
# Conflicts:
#	Tests/PersistentCollections2Tests/PersistentMap Fixtures.swift
2022-09-17 17:20:34 -07:00
Karoy Lorentey 1661bd431c [PersistentDictionary] Implement in-place mutations for defaulted subscript 2022-09-16 23:19:40 -07:00
Karoy Lorentey 6cf128e6ea [PersistentCollections] Internal doc updates 2022-09-16 14:49:37 -07:00
Karoy Lorentey 0fe7f080c0 [PersistentCollections] offset → slot
(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.
2022-09-16 01:30:06 -07:00
Karoy Lorentey 16393ea2c8 [PersistentCollections] Allow dumping hash trees in iteration order 2022-09-15 20:56:50 -07:00
Karoy Lorentey 02133f1262 [PersistentCollections] Implement path-based indices
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.
2022-09-15 20:56:50 -07:00
Karoy Lorentey 547ac9094f [PersistentCollections] Rework basic node properties 2022-09-15 20:56:50 -07:00
Karoy Lorentey 6dd02114b8 [PersistentCollections] Flesh out _RawNode, add _UnmanagedNode
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.
2022-09-15 20:56:50 -07:00
Karoy Lorentey edaadc9a8a [PersistentCollections] _Level: Store the shift amount in an UInt8 2022-09-15 20:56:50 -07:00
Karoy Lorentey 3e38184f2a [PersistentCollections] Optimize _Node.==
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.
2022-09-15 20:56:50 -07:00
Karoy Lorentey 2e523b1045 [PersistentCollections] Reverse ordering of items in node storage 2022-09-15 20:56:50 -07:00
Karoy Lorentey 9d9659a079 [PersistenCollections] Fixup remove implementation
Don’t force the caller to clean up any of the edge cases.
2022-09-14 14:27:26 -07:00
Karoy Lorentey 893e0ff79e [PersistentCollections] Convert _Node to a wrapper around _RawNode
This makes it more explicit that they are assumed to be layout-compatible.
2022-09-14 14:27:26 -07:00
Karoy Lorentey 8a789c2d6f [PersistentCollections] _Node._FindResult: Add docs, rename newSplit to expansion 2022-09-14 13:40:16 -07:00
Karoy Lorentey c9ca59fbb9 [PersistentCollections] Improved storage representation
- 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.
2022-09-13 23:48:54 -07:00