mirror of
https://github.com/apple/swift-collections
synced 2026-11-24 04:27:53 +00:00
[PersistentCollections] Convert _Node to a wrapper around _RawNode
This makes it more explicit that they are assumed to be layout-compatible.
This commit is contained in:
1 parent
8a789c2d6f
commit
893e0ff79e
4 files changed
+31
-15
No files matched your search
@@ -13,7 +13,7 @@
|
||||
extension _Node: Equatable where Value: Equatable {
|
||||
@inlinable
|
||||
static func == (lhs: _Node, rhs: _Node) -> Bool {
|
||||
if lhs.storage === rhs.storage { return true }
|
||||
if lhs.raw.storage === rhs.raw.storage { return true }
|
||||
if lhs.isCollisionNode && rhs.isCollisionNode {
|
||||
return lhs.read { lhs in
|
||||
rhs.read { rhs in
|
||||
|
||||
@@ -84,8 +84,9 @@ extension _Node {
|
||||
header.pointee._bytesFree = header.pointee._byteCapacity
|
||||
assert(byteCapacity <= header.pointee.byteCapacity)
|
||||
}
|
||||
self.storage = unsafeDowncast(object, to: Storage.self)
|
||||
self.count = 0
|
||||
self.raw = _RawNode(
|
||||
storage: unsafeDowncast(object, to: Storage.self),
|
||||
count: 0)
|
||||
}
|
||||
|
||||
@inlinable
|
||||
@@ -125,7 +126,7 @@ extension _Node {
|
||||
|
||||
@inlinable
|
||||
internal mutating func isUnique() -> Bool {
|
||||
isKnownUniquelyReferenced(&self.storage)
|
||||
isKnownUniquelyReferenced(&self.raw.storage)
|
||||
}
|
||||
|
||||
@inlinable
|
||||
@@ -142,7 +143,7 @@ extension _Node {
|
||||
) {
|
||||
if !isUnique {
|
||||
self = copy(withFreeSpace: minimumFreeBytes)
|
||||
} else if self.storage.header.bytesFree < minimumFreeBytes {
|
||||
} else if self.raw.storage.header.bytesFree < minimumFreeBytes {
|
||||
move(withFreeSpace: minimumFreeBytes)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -50,31 +50,46 @@ internal struct _RawNode {
|
||||
@usableFromInline
|
||||
@frozen
|
||||
internal struct _Node<Key: Hashable, Value> {
|
||||
// Warning: This struct must be kept layout-compatible with _RawNode.
|
||||
// Do not add any new stored properties to this type.
|
||||
//
|
||||
// Swift guarantees that frozen structs with a single stored property will
|
||||
// be layout-compatible with the type they are wrapping.
|
||||
//
|
||||
// (_RawNode is used as the Element type of the ManagedBuffer underlying
|
||||
// node storage, and the memory is then rebound to `_Node` later.
|
||||
// This will not work correctly unless `_Node` has the exact same alignment
|
||||
// and stride as `RawNode`.)
|
||||
|
||||
@usableFromInline
|
||||
internal typealias Element = (key: Key, value: Value)
|
||||
|
||||
@usableFromInline
|
||||
internal var storage: _RawStorage
|
||||
|
||||
@usableFromInline
|
||||
internal var count: Int
|
||||
internal var raw: _RawNode
|
||||
|
||||
@inlinable
|
||||
internal init(_storage: _RawStorage, count: Int) {
|
||||
self.storage = _storage
|
||||
self.count = count
|
||||
internal init(storage: _RawStorage, count: Int) {
|
||||
self.raw = _RawNode(storage: storage, count: count)
|
||||
}
|
||||
}
|
||||
|
||||
extension _Node {
|
||||
@inlinable @inline(__always)
|
||||
internal var count: Int {
|
||||
get { raw.count }
|
||||
set { raw.count = newValue }
|
||||
}
|
||||
}
|
||||
|
||||
extension _Node {
|
||||
@inlinable @inline(__always)
|
||||
internal func read<R>(_ body: (UnsafeHandle) -> R) -> R {
|
||||
UnsafeHandle.read(storage, body)
|
||||
UnsafeHandle.read(raw.storage, body)
|
||||
}
|
||||
|
||||
@inlinable @inline(__always)
|
||||
internal mutating func update<R>(_ body: (UnsafeHandle) -> R) -> R {
|
||||
UnsafeHandle.update(storage, body)
|
||||
UnsafeHandle.update(raw.storage, body)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -25,7 +25,7 @@ public struct PersistentDictionary<Key, Value> where Key: Hashable {
|
||||
extension PersistentDictionary {
|
||||
@inlinable
|
||||
public init() {
|
||||
self.init(_root: _Node(_storage: _emptySingleton, count: 0))
|
||||
self.init(_root: _Node(storage: _emptySingleton, count: 0))
|
||||
}
|
||||
|
||||
@inlinable
|
||||
|
||||
Reference in new issue
Block a user