mirror of
https://github.com/apple/swift-collections
synced 2026-11-27 04:54:21 +00:00
[PersistentDictionary] Fix index(forKey:) performance
This commit is contained in:
1 parent
5240bf6e0a
commit
d45f264ef0
6 files changed
+86
-35
No files matched your search
@@ -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) {
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -41,7 +41,7 @@ extension _RawNode {
|
||||
}
|
||||
|
||||
extension _RawNode {
|
||||
@inline(__always)
|
||||
@inlinable @inline(__always)
|
||||
internal var unmanaged: _UnmanagedNode {
|
||||
_UnmanagedNode(storage)
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user