From 893e0ff79ea911aaedd9ee623738d6c8d60a2d0a Mon Sep 17 00:00:00 2001 From: Karoy Lorentey Date: Wed, 14 Sep 2022 12:39:33 -0700 Subject: [PATCH] [PersistentCollections] Convert _Node to a wrapper around _RawNode This makes it more explicit that they are assumed to be layout-compatible. --- .../Node/_Node+Equatable.swift | 2 +- .../Node/_Node+Storage.swift | 9 ++--- .../PersistentCollections/Node/_Node.swift | 33 ++++++++++++++----- .../PersistentDictionary.swift | 2 +- 4 files changed, 31 insertions(+), 15 deletions(-) diff --git a/Sources/PersistentCollections/Node/_Node+Equatable.swift b/Sources/PersistentCollections/Node/_Node+Equatable.swift index 7493c794..5ec3f2d6 100644 --- a/Sources/PersistentCollections/Node/_Node+Equatable.swift +++ b/Sources/PersistentCollections/Node/_Node+Equatable.swift @@ -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 diff --git a/Sources/PersistentCollections/Node/_Node+Storage.swift b/Sources/PersistentCollections/Node/_Node+Storage.swift index d473923d..d0cfe646 100644 --- a/Sources/PersistentCollections/Node/_Node+Storage.swift +++ b/Sources/PersistentCollections/Node/_Node+Storage.swift @@ -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) } } diff --git a/Sources/PersistentCollections/Node/_Node.swift b/Sources/PersistentCollections/Node/_Node.swift index 1546e642..d08893ca 100644 --- a/Sources/PersistentCollections/Node/_Node.swift +++ b/Sources/PersistentCollections/Node/_Node.swift @@ -50,31 +50,46 @@ internal struct _RawNode { @usableFromInline @frozen internal struct _Node { + // 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(_ body: (UnsafeHandle) -> R) -> R { - UnsafeHandle.read(storage, body) + UnsafeHandle.read(raw.storage, body) } @inlinable @inline(__always) internal mutating func update(_ body: (UnsafeHandle) -> R) -> R { - UnsafeHandle.update(storage, body) + UnsafeHandle.update(raw.storage, body) } } diff --git a/Sources/PersistentCollections/PersistentDictionary/PersistentDictionary.swift b/Sources/PersistentCollections/PersistentDictionary/PersistentDictionary.swift index e3e9cf7d..c50c1bd8 100644 --- a/Sources/PersistentCollections/PersistentDictionary/PersistentDictionary.swift +++ b/Sources/PersistentCollections/PersistentDictionary/PersistentDictionary.swift @@ -25,7 +25,7 @@ public struct PersistentDictionary 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