Commit Graph
41 Commits
Author SHA1 Message Date
Karoy Lorentey f76630db8e [Internals] Add missing internal checking predicates & docs for all 2022-10-18 22:13:19 -07:00
Karoy Lorentey 6286cd0b99 Restore support for building with Swift 5.5
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.
2022-10-10 18:07:49 -07:00
Karoy Lorentey d7dbaee8b9 [PersistentCollections] Minor enhancements 2022-10-09 15:20:40 -07:00
Karoy Lorentey 3238b9b575 [PersistentDictionary] PersistentDictionary, .Iterator, .Index, .Keys, .Keys.Iterator, .Values, .Values.Iterator: Explicitly conform to Sendable when Key and Value are both Sendable 2022-10-05 11:46:43 -07:00
Karoy Lorentey f71ebd9494 [PersistentDictionary] Implement structural merge operations
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.)
2022-09-30 19:55:04 -07:00
Karoy Lorentey 6dfad714a1 [PersistentCollections] _Node.update → _Node.updateValue
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.
2022-09-30 12:19:40 -07:00
Karoy Lorentey 19c9f282c9 Delete _FastMembershipCheckable
We don’t actually need this, as `_customContainsEquatableElement`
returning non-nil is a perfectly suitable indication that containment
checking is fast.
2022-09-29 17:37:48 -07:00
Karoy Lorentey 562b21e70f [PersistentCollections] filter, intersection, subtracting: Don’t build a result node until we know for sure we won’t just clone self 2022-09-28 14:45:44 -07:00
Karoy Lorentey 03c1bd958a [PersistentSet] Implement structural union operation (untested) 2022-09-27 14:18:39 -07:00
Karoy Lorentey ba303dca1d [PersistentDictionary] Add a structural initializer that takes a PersistentSet and a value transform 2022-09-24 17:12:40 -07:00
Karoy Lorentey 826e22c91e [PersistentDictionary] Implement structural filter & compactMapValues
Plus add tests covering these as well and mapValues.
2022-09-24 17:09:36 -07:00
Karoy Lorentey 1c6e5cb30c [PersistentDictionary] Add CustomReflectable conformance 2022-09-24 17:02:16 -07:00
Karoy Lorentey 3d10964905 [PersistentSet] Flesh out isSubset/iSuperset/isDisjoint/etc overloads (untested)
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 ==.
2022-09-24 17:02:16 -07:00
Karoy Lorentey 00ae83df1e [PersistentSet] Add ExpressibleByArrayLiteral, Codable, Custom[Debug]StringConvertible, CustomReflectable conformances 2022-09-24 17:02:15 -07:00
Karoy Lorentey 92d1d1297e [PersistentDictionary] Add a quick&dirty memory benchmark
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.
2022-09-23 16:47:30 -07:00
Karoy Lorentey 9150bbf06e [PersistentCollections] Cache the item hash within collision nodes
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.
2022-09-23 11:15:56 -07:00
Karoy Lorentey 77f205f45e [PersistentCollections] Implement structural index(_:offsetBy:) & index(_:offsetBy:limitedBy:) 2022-09-23 11:15:46 -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 LorenteyandMichael Steindorfer 39fc818e0e Update Sources/PersistentCollections/PersistentDictionary/PersistentDictionary+Values.swift
Co-authored-by: Michael Steindorfer <michael@steindorfer.name>
2022-09-20 12:03:03 -07:00
Karoy LorenteyandMichael Steindorfer a28921bb81 Update Sources/PersistentCollections/PersistentDictionary/PersistentDictionary+Keys.swift
Co-authored-by: Michael Steindorfer <michael@steindorfer.name>
2022-09-20 12:02:45 -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 6dc8de17b0 [PersistentDictionary] Add more initializers 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 e11f923d98 [PersistentDictionary] Don’t bump version in uniqueKeys initializers 2022-09-19 21:25:07 -07:00
Karoy Lorentey 12e3867ccb [Collections] Allow determining if internal checking is enabled
In the benchmark target, complain loudly if consistency checks
are enabled.
2022-09-19 21:25:07 -07:00
Karoy Lorentey d45f264ef0 [PersistentDictionary] Fix index(forKey:) performance 2022-09-17 20:04:19 -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 3b7d241e15 [PersistentDictionary] Add some docs 2022-09-16 23:19:40 -07:00
Karoy Lorentey 1661bd431c [PersistentDictionary] Implement in-place mutations for defaulted subscript 2022-09-16 23:19:40 -07:00
Karoy Lorentey 1324c4e863 [OrderedDictionary] Implement index invalidation 2022-09-16 16:03:45 -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 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 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