mirror of
https://github.com/apple/swift-collections
synced 2026-11-25 04:37:48 +00:00
- 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.
93 lines
2.9 KiB
Swift
93 lines
2.9 KiB
Swift
//===----------------------------------------------------------------------===//
|
|
//
|
|
// This source file is part of the Swift Collections open source project
|
|
//
|
|
// Copyright (c) 2022 Apple Inc. and the Swift project authors
|
|
// Licensed under Apache License v2.0 with Runtime Library Exception
|
|
//
|
|
// See https://swift.org/LICENSE.txt for license information
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
extension _StorageHeader {
|
|
#if COLLECTIONS_INTERNAL_CHECKS
|
|
@usableFromInline @inline(never)
|
|
internal func _invariantCheck() {
|
|
precondition(bytesFree <= byteCapacity)
|
|
if isCollisionNode {
|
|
precondition(itemMap == childMap)
|
|
precondition(!itemMap.isEmpty)
|
|
} else {
|
|
precondition(itemMap.intersection(childMap).isEmpty)
|
|
}
|
|
}
|
|
#else
|
|
@inlinable @inline(__always)
|
|
internal func _invariantCheck() {}
|
|
#endif
|
|
}
|
|
|
|
extension _Node {
|
|
#if COLLECTIONS_INTERNAL_CHECKS
|
|
@usableFromInline @inline(never)
|
|
internal func _invariantCheck() {
|
|
storage.header._invariantCheck()
|
|
read {
|
|
let itemBytes = $0.itemCount * MemoryLayout<Element>.stride
|
|
let childBytes = $0.childCount * MemoryLayout<_Node>.stride
|
|
assert(itemBytes + $0.bytesFree + childBytes == $0.byteCapacity)
|
|
let actualCount = $0._children.reduce($0.itemCount, { $0 + $1.count })
|
|
assert(actualCount == self.count)
|
|
}
|
|
}
|
|
#else
|
|
@inlinable @inline(__always)
|
|
internal func _invariantCheck() {}
|
|
#endif
|
|
|
|
#if COLLECTIONS_INTERNAL_CHECKS
|
|
@inlinable @inline(never)
|
|
public func _fullInvariantCheck(_ level: _Level, _ path: _Hash) {
|
|
_invariantCheck()
|
|
read {
|
|
precondition(level.isAtRoot || !hasSingletonItem)
|
|
precondition(!isAtrophied)
|
|
if $0.isCollisionNode {
|
|
precondition(count == $0.itemCount)
|
|
precondition(count > 0)
|
|
let key = $0[item: 0].key
|
|
let hash = _Hash(key)
|
|
precondition(
|
|
hash.isEqual(to: path, upTo: level),
|
|
"Misplaced colliding key '\(key)': \(path) isn't a prefix of \(hash)")
|
|
for item in $0._items.dropFirst() {
|
|
precondition(_Hash(item.key) == hash)
|
|
}
|
|
}
|
|
var itemOffset = 0
|
|
var childOffset = 0
|
|
for b in 0 ..< UInt(_Bitmap.capacity) {
|
|
let bucket = _Bucket(b)
|
|
let path = path.appending(bucket, at: level)
|
|
if $0.itemMap.contains(bucket) {
|
|
let key = $0._items[itemOffset].key
|
|
let hash = _Hash(key)
|
|
precondition(
|
|
hash.isEqual(to: path, upTo: level.descend()),
|
|
"Misplaced key '\(key)': \(path) isn't a prefix of \(hash)")
|
|
itemOffset += 1
|
|
}
|
|
if $0.hasChildren && $0.childMap.contains(bucket) {
|
|
$0[child: childOffset]._fullInvariantCheck(level.descend(), path)
|
|
childOffset += 1
|
|
}
|
|
}
|
|
}
|
|
}
|
|
#else
|
|
@inlinable @inline(__always)
|
|
public func _fullInvariantCheck(_ level: _Level, _ path: _Hash) {}
|
|
#endif
|
|
}
|
|
|