From d45f264ef0dc625a37801ee6e05b764ccbef30b4 Mon Sep 17 00:00:00 2001 From: Karoy Lorentey Date: Sat, 17 Sep 2022 19:09:28 -0700 Subject: [PATCH] [PersistentDictionary] Fix index(forKey:) performance --- .../Node/_AncestorOffsets.swift | 7 ++ .../Node/_Node+Lookups.swift | 65 ++++++++++++++++--- .../PersistentCollections/Node/_RawNode.swift | 2 +- .../Node/_UnmanagedNode.swift | 2 +- .../Node/_UnsafePath.swift | 41 ++++++------ .../PersistentDictionary.swift | 4 +- 6 files changed, 86 insertions(+), 35 deletions(-) diff --git a/Sources/PersistentCollections/Node/_AncestorOffsets.swift b/Sources/PersistentCollections/Node/_AncestorOffsets.swift index 2a6f793a..f686ec9a 100644 --- a/Sources/PersistentCollections/Node/_AncestorOffsets.swift +++ b/Sources/PersistentCollections/Node/_AncestorOffsets.swift @@ -58,6 +58,13 @@ extension _AncestorSlots { } } + @inlinable @inline(__always) + internal func appending(_ slot: _Slot, at level: _Level) -> Self { + var result = self + result[level] = slot + return result + } + /// Clear the slot at the specified level, by setting it to zero. @inlinable internal mutating func clear(_ level: _Level) { diff --git a/Sources/PersistentCollections/Node/_Node+Lookups.swift b/Sources/PersistentCollections/Node/_Node+Lookups.swift index 112d32f2..3ad13650 100644 --- a/Sources/PersistentCollections/Node/_Node+Lookups.swift +++ b/Sources/PersistentCollections/Node/_Node+Lookups.swift @@ -11,6 +11,55 @@ // MARK: Node-level lookup operations +extension _Node { + @inlinable + internal func find( + _ level: _Level, _ key: Key, _ hash: _Hash + ) -> (descend: Bool, slot: _Slot)? { + read { $0.find(level, key, hash) } + } +} + +extension _Node.UnsafeHandle { + @inlinable + internal func find( + _ level: _Level, _ key: Key, _ hash: _Hash + ) -> (descend: Bool, slot: _Slot)? { + guard !isCollisionNode else { + let r = _findInCollision(level, key, hash) + guard r.code == 0 else { return nil } + return (false, r.slot) + } + let bucket = hash[level] + if itemMap.contains(bucket) { + let slot = itemMap.slot(of: bucket) + guard self[item: slot].key == key else { return nil } + return (false, slot) + } + if childMap.contains(bucket) { + let slot = childMap.slot(of: bucket) + return (true, slot) + } + return nil + } + + @inlinable @inline(never) + internal func _findInCollision( + _ level: _Level, _ key: Key, _ hash: _Hash + ) -> (code: Int, slot: _Slot, expansionHash: _Hash) { + assert(isCollisionNode) + if !level.isAtBottom { + let h = _Hash(self[item: .zero].key) + if h != hash { return (2, .zero, h) } + } + // Note: this searches the items in reverse insertion order. + guard let slot = reverseItems.firstIndex(where: { $0.key == key }) + else { return (1, self.itemEnd, _Hash(_value: 0)) } + return (0, _Slot(itemCount &- 1 &- slot), _Hash(_value: 0)) + } +} + + /// Represents the results of a lookup operation within a single node of a hash /// tree. This enumeration captures all of the different cases that need to be /// covered if we wanted to insert a new item into the tree. @@ -88,17 +137,15 @@ extension _Node.UnsafeHandle { _ level: _Level, _ key: Key, _ hash: _Hash, forInsert: Bool ) -> _FindResult { guard !isCollisionNode else { - if !level.isAtBottom { - let h = _Hash(self[item: .zero].key) - if h != hash { - return .expansion(h) - } + let r = _findInCollision(level, key, hash) + if r.code == 0 { + return .found(.invalid, r.slot) } - // Note: this searches the items in reverse insertion order. - guard let slot = reverseItems.firstIndex(where: { $0.key == key }) else { - return .notFound(.invalid, itemEnd) + if r.code == 1 { + return .notFound(.invalid, self.itemEnd) } - return .found(.invalid, _Slot(itemCount &- 1 &- slot)) + assert(r.code == 2) + return .expansion(r.expansionHash) } let bucket = hash[level] if itemMap.contains(bucket) { diff --git a/Sources/PersistentCollections/Node/_RawNode.swift b/Sources/PersistentCollections/Node/_RawNode.swift index d0f68127..9cbc9848 100644 --- a/Sources/PersistentCollections/Node/_RawNode.swift +++ b/Sources/PersistentCollections/Node/_RawNode.swift @@ -41,7 +41,7 @@ extension _RawNode { } extension _RawNode { - @inline(__always) + @inlinable @inline(__always) internal var unmanaged: _UnmanagedNode { _UnmanagedNode(storage) } diff --git a/Sources/PersistentCollections/Node/_UnmanagedNode.swift b/Sources/PersistentCollections/Node/_UnmanagedNode.swift index 279c5a4d..1928686d 100644 --- a/Sources/PersistentCollections/Node/_UnmanagedNode.swift +++ b/Sources/PersistentCollections/Node/_UnmanagedNode.swift @@ -23,7 +23,7 @@ internal struct _UnmanagedNode { @usableFromInline internal var ref: Unmanaged<_RawStorage> - @inlinable + @inlinable @inline(__always) internal init(_ storage: _RawStorage) { self.ref = .passUnretained(storage) } diff --git a/Sources/PersistentCollections/Node/_UnsafePath.swift b/Sources/PersistentCollections/Node/_UnsafePath.swift index 67b38dc3..d507d343 100644 --- a/Sources/PersistentCollections/Node/_UnsafePath.swift +++ b/Sources/PersistentCollections/Node/_UnsafePath.swift @@ -50,8 +50,7 @@ internal struct _UnsafePath { @usableFromInline internal var _isItem: Bool - @usableFromInline - @_effects(releasenone) + @inlinable internal init(root: __shared _RawNode) { self.level = .top self.ancestors = .empty @@ -193,6 +192,7 @@ extension _UnsafePath { /// /// - Note: It is undefined behavior to call this on a path that is no longer /// valid. + @inlinable internal var isPlaceholder: Bool { _isItem && nodeSlot.value == node.itemCount } @@ -202,6 +202,7 @@ extension _UnsafePath { /// /// - Note: It is undefined behavior to call this on a path that is no longer /// valid. + @inlinable internal var isOnChild: Bool { !_isItem && nodeSlot.value < node.childCount } @@ -211,6 +212,7 @@ extension _UnsafePath { /// /// - Note: It is undefined behavior to call this on a path that is no longer /// valid. + @inlinable internal var isOnNodeEnd: Bool { !_isItem && nodeSlot.value == node.childCount } @@ -231,6 +233,7 @@ extension _UnsafePath { /// /// - Note: It is undefined behavior to call this on a path that is no longer /// valid. + @inlinable internal var currentChild: _UnmanagedNode { assert(isOnChild) return node.unmanagedChild(at: nodeSlot) @@ -304,8 +307,7 @@ extension _UnsafePath { /// /// - Note: It is undefined behavior to call this on a path that is no longer /// valid. - @usableFromInline - @_effects(releasenone) + @inlinable internal mutating func descend() { self.node = currentChild self.ancestors[level] = nodeSlot @@ -626,26 +628,21 @@ extension _Node { /// return nil. @inlinable internal func path( - to key: Key, hash: _Hash - ) -> (found: Bool, path: _UnsafePath) { - var path = _UnsafePath(root: raw) + to key: Key, _ hash: _Hash + ) -> _UnsafePath? { + var node = unmanaged + var level: _Level = .top + var ancestors: _AncestorSlots = .empty while true { - let r = UnsafeHandle.read(path.node) { - $0.find(path.level, key, hash, forInsert: false) - } - switch r { - case .found(_, let slot): - path.selectItem(at: slot) - return (true, path) - case .notFound(_, let slot), .newCollision(_, let slot): - path.selectItem(at: slot) - return (false, path) - case .expansion: - return (false, path) - case .descend(_, let slot): - path.selectChild(at: slot) - path.descend() + let r = UnsafeHandle.read(node) { $0.find(level, key, hash) } + guard let r = r else { break } + guard r.descend else { + return _UnsafePath(level, ancestors, node, itemSlot: r.slot) } + node = node.unmanagedChild(at: r.slot) + ancestors[level] = r.slot + level = level.descend() } + return nil } } diff --git a/Sources/PersistentCollections/PersistentDictionary/PersistentDictionary.swift b/Sources/PersistentCollections/PersistentDictionary/PersistentDictionary.swift index 9f2d05b9..768df3bd 100644 --- a/Sources/PersistentCollections/PersistentDictionary/PersistentDictionary.swift +++ b/Sources/PersistentCollections/PersistentDictionary/PersistentDictionary.swift @@ -257,8 +257,8 @@ extension PersistentDictionary { /// Returns the index for the given key. @inlinable public func index(forKey key: Key) -> Index? { - let (found, path) = _root.path(to: key, hash: _Hash(key)) - guard found else { return nil } + guard let path = _root.path(to: key, _Hash(key)) + else { return nil } return Index(_root: _root.unmanaged, version: _version, path: path) }