From a1902c30aa44efef84bce17e425fcf5e45f6fab2 Mon Sep 17 00:00:00 2001 From: Karoy Lorentey Date: Thu, 19 Feb 2026 17:56:22 -0800 Subject: [PATCH] [BasicContainers] Make _HTable nonfrozen and most of its operations noninlinable This is a nongeneric type, so it makes sense to see if Swift has matured enough to use opaque/resilient types in high-performance contexts. It would be ideal if the core hash table implementation would remain flexible. We may need to sprinkle some effect attributes over these. --- ...+Small.swift => _HTable+Consumption.swift} | 25 ++++++--------- .../HashTable/_HTable+Find.swift | 4 +-- .../HashTable/_HTable+Insert.swift | 4 +-- .../HashTable/_HTable+Iteration.swift | 31 +++++++++---------- .../HashTable/_HTable+Sizing.swift | 16 ++-------- .../BasicContainers/HashTable/_HTable.swift | 12 +++---- Xcode/Collections.xcodeproj/project.pbxproj | 12 +++---- 7 files changed, 40 insertions(+), 64 deletions(-) rename Sources/BasicContainers/HashTable/{_HTable+Small.swift => _HTable+Consumption.swift} (61%) diff --git a/Sources/BasicContainers/HashTable/_HTable+Small.swift b/Sources/BasicContainers/HashTable/_HTable+Consumption.swift similarity index 61% rename from Sources/BasicContainers/HashTable/_HTable+Small.swift rename to Sources/BasicContainers/HashTable/_HTable+Consumption.swift index 7f834a65..083d73d9 100644 --- a/Sources/BasicContainers/HashTable/_HTable+Small.swift +++ b/Sources/BasicContainers/HashTable/_HTable+Consumption.swift @@ -10,25 +10,18 @@ //===----------------------------------------------------------------------===// #if compiler(>=6.2) + extension _HTable { @usableFromInline - @frozen - internal enum Variant: ~Copyable { - case small(_HTable.Small) - case large(_HTable) - } - - @usableFromInline - @frozen - internal struct Small: ~Copyable { - @_alwaysEmitIntoClient - internal var count: Int - - @_transparent - @_alwaysEmitIntoClient - internal init() { - self.count = 0 + package mutating func consumeAll( + consumer: (Range) -> Void + ) { + var it = makeBucketIterator() + while let next = it.nextOccupiedRegion() { + consumer(next) } + clear() } } + #endif diff --git a/Sources/BasicContainers/HashTable/_HTable+Find.swift b/Sources/BasicContainers/HashTable/_HTable+Find.swift index 89448a43..1e90ab8d 100644 --- a/Sources/BasicContainers/HashTable/_HTable+Find.swift +++ b/Sources/BasicContainers/HashTable/_HTable+Find.swift @@ -15,7 +15,7 @@ import InternalCollectionsUtilities #if compiler(>=6.2) extension _HTable { - @inlinable + @usableFromInline internal func find_Small( tester: (Bucket) -> Bool, ) -> Bucket? { @@ -29,7 +29,7 @@ extension _HTable { return nil } - @inlinable + @usableFromInline internal func find_Large( hashValue: Int, tester: (Bucket) -> Bool, diff --git a/Sources/BasicContainers/HashTable/_HTable+Insert.swift b/Sources/BasicContainers/HashTable/_HTable+Insert.swift index ee44ea75..c2f98cb9 100644 --- a/Sources/BasicContainers/HashTable/_HTable+Insert.swift +++ b/Sources/BasicContainers/HashTable/_HTable+Insert.swift @@ -11,7 +11,7 @@ #if compiler(>=6.2) extension _HTable { - @inlinable + @usableFromInline internal mutating func insertNew_Small( swapper: (Bucket) -> Void, ) -> Bucket { @@ -25,7 +25,7 @@ extension _HTable { } - @inlinable + @usableFromInline internal mutating func insertNew_Large( hashValue: Int, hashGenerator: (Bucket) -> Int, diff --git a/Sources/BasicContainers/HashTable/_HTable+Iteration.swift b/Sources/BasicContainers/HashTable/_HTable+Iteration.swift index 6431d65b..7d4b9e0e 100644 --- a/Sources/BasicContainers/HashTable/_HTable+Iteration.swift +++ b/Sources/BasicContainers/HashTable/_HTable+Iteration.swift @@ -15,7 +15,7 @@ import InternalCollectionsUtilities #if compiler(>=6.2) extension _HTable { - @_alwaysEmitIntoClient + @usableFromInline @_lifetime(borrow self) package func makeBucketIterator( from start: Bucket = Bucket(offset: 0) @@ -36,7 +36,6 @@ extension _HTable { /// iterator values outside the closure call that produced the original /// hash table. @usableFromInline - @frozen package struct BucketIterator: ~Copyable, ~Escapable { @usableFromInline package typealias Bucket = _HTable.Bucket @@ -92,19 +91,19 @@ extension _HTable { } extension _HTable.BucketIterator { - @_alwaysEmitIntoClient + @usableFromInline @_transparent package var currentBucket: Bucket { _bucket } - @_alwaysEmitIntoClient + @usableFromInline @_transparent package var isAtEnd: Bool { _bucket >= _endBucket } - @_alwaysEmitIntoClient + @usableFromInline @_transparent package var isOccupied: Bool { assert(!isAtEnd) @@ -112,7 +111,6 @@ extension _HTable.BucketIterator { return _nextBits.contains(0) } - @_alwaysEmitIntoClient @_transparent @_lifetime(self: copy self) package mutating func restart() { @@ -124,7 +122,6 @@ extension _HTable.BucketIterator { } } - @_alwaysEmitIntoClient @_transparent @_lifetime(self: copy self) package mutating func _wrap() { @@ -133,7 +130,7 @@ extension _HTable.BucketIterator { } - @_alwaysEmitIntoClient + @usableFromInline @discardableResult @_lifetime(self: copy self) package mutating func _advanceToNextWord() -> Bool { @@ -152,7 +149,7 @@ extension _HTable.BucketIterator { return true } - @_alwaysEmitIntoClient + @usableFromInline @_lifetime(self: copy self) package mutating func _wrapToNextWord() { _bucket.advanceToNextWord() @@ -164,7 +161,7 @@ extension _HTable.BucketIterator { } } - @_alwaysEmitIntoClient + @usableFromInline @discardableResult @_lifetime(self: copy self) package mutating func advanceToNextBit() -> Bool { @@ -188,7 +185,7 @@ extension _HTable.BucketIterator { return true } - @_alwaysEmitIntoClient + @usableFromInline @_lifetime(self: copy self) package mutating func wrapToNextBit() { _bucket._offset &+= 1 @@ -208,7 +205,7 @@ extension _HTable.BucketIterator { } /// If the current bucket is already occupied, this does nothing. - @_alwaysEmitIntoClient + @usableFromInline @discardableResult @_lifetime(self: copy self) package mutating func advanceToOccupied() -> Bool { @@ -228,7 +225,7 @@ extension _HTable.BucketIterator { return true } - @_alwaysEmitIntoClient + @usableFromInline @_lifetime(self: copy self) package mutating func wrapToOccupied() { if _words == nil { @@ -247,7 +244,7 @@ extension _HTable.BucketIterator { } /// If the current bucket is already unoccupied, this does nothing. - @_alwaysEmitIntoClient + @usableFromInline @discardableResult @_lifetime(self: copy self) package mutating func advanceToUnoccupied() -> Bool { @@ -270,7 +267,7 @@ extension _HTable.BucketIterator { } /// If the current bucket is already unoccupied, this does nothing. - @_alwaysEmitIntoClient + @usableFromInline @discardableResult @_lifetime(self: copy self) package mutating func advanceToUnoccupied( @@ -308,7 +305,7 @@ extension _HTable.BucketIterator { return true } - @_alwaysEmitIntoClient + @usableFromInline @_lifetime(self: copy self) package mutating func wrapToUnoccupied() { if _words == nil { @@ -326,7 +323,7 @@ extension _HTable.BucketIterator { } } - @_alwaysEmitIntoClient + @usableFromInline @_lifetime(self: copy self) package mutating func nextOccupiedRegion( maximumCount: Int = .max diff --git a/Sources/BasicContainers/HashTable/_HTable+Sizing.swift b/Sources/BasicContainers/HashTable/_HTable+Sizing.swift index 72c71427..a172b4da 100644 --- a/Sources/BasicContainers/HashTable/_HTable+Sizing.swift +++ b/Sources/BasicContainers/HashTable/_HTable+Sizing.swift @@ -54,23 +54,11 @@ extension _HTable { /// The numerator of the maximum hash table load factor. @_transparent - internal static var _maxLFNum: UInt { -#if COLLECTIONS_NO_ROBIN_HOOD_HASHING - 3 -#else - 7 -#endif - } + internal static var _maxLFNum: UInt { 7 } /// The denominator of the maximum hash table load factor. @_transparent - internal static var _maxLFDenom: UInt { -#if COLLECTIONS_NO_ROBIN_HOOD_HASHING - 4 -#else - 8 -#endif - } + internal static var _maxLFDenom: UInt { 8 } /// The numerator of the minimum hash table load factor. @_transparent diff --git a/Sources/BasicContainers/HashTable/_HTable.swift b/Sources/BasicContainers/HashTable/_HTable.swift index e1cb63d4..85c1c154 100644 --- a/Sources/BasicContainers/HashTable/_HTable.swift +++ b/Sources/BasicContainers/HashTable/_HTable.swift @@ -191,15 +191,13 @@ extension _HTable { extension _HTable { @_transparent - @inlinable - package func idealBucket(forHashValue hashValue: Int) -> Bucket { + internal func idealBucket(forHashValue hashValue: Int) -> Bucket { let hashValue = UInt(truncatingIfNeeded: hashValue) return Bucket(offset: hashValue & (bucketCount &- 1)) } @_transparent - @inlinable - package func probeLength( + internal func probeLength( from start: Bucket, to end: Bucket ) -> Int { @@ -210,8 +208,7 @@ extension _HTable { } @_transparent - @inlinable - package func probeLength( + internal func probeLength( forHashValue hashValue: Int, in bucket: Bucket ) -> Int { @@ -221,11 +218,12 @@ extension _HTable { } extension _HTable { - @inlinable + @usableFromInline package mutating func clear() { self._count = 0 self.bitmap.clearAll() self._totalProbeLength = 0 + self._maxProbeLength = 0 } } #endif diff --git a/Xcode/Collections.xcodeproj/project.pbxproj b/Xcode/Collections.xcodeproj/project.pbxproj index b0fa5e5f..afa2e9d7 100644 --- a/Xcode/Collections.xcodeproj/project.pbxproj +++ b/Xcode/Collections.xcodeproj/project.pbxproj @@ -82,6 +82,7 @@ 7D479CCD2F46D23900A8CB59 /* UniqueSet+BorrowingSequence.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7D479CCC2F46D23400A8CB59 /* UniqueSet+BorrowingSequence.swift */; }; 7D479CD22F46DD8600A8CB59 /* RigidSet+Initializers.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7D479CD12F46DD8600A8CB59 /* RigidSet+Initializers.swift */; }; 7D479CD42F46DD9600A8CB59 /* UniqueSet+Initializers.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7D479CD32F46DD9600A8CB59 /* UniqueSet+Initializers.swift */; }; + 7D479CE72F47D88600A8CB59 /* _HTable+Consumption.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7D479CE62F47D88000A8CB59 /* _HTable+Consumption.swift */; }; 7D48BCDC2E51554900D7F6F7 /* ClassBox.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7D48BCDB2E51554900D7F6F7 /* ClassBox.swift */; }; 7D48BCDF2E51558700D7F6F7 /* TestPrintable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7D48BCDD2E51558700D7F6F7 /* TestPrintable.swift */; }; 7D48BCE02E51558700D7F6F7 /* LifetimeTrackedStruct.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7D48BCDE2E51558700D7F6F7 /* LifetimeTrackedStruct.swift */; }; @@ -111,7 +112,6 @@ 7D72468C2F3D9DDB00112FD0 /* RigidSet.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7D7246852F3D9DDB00112FD0 /* RigidSet.swift */; }; 7D72468D2F3D9DDB00112FD0 /* _HTable+Bucket.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7D72467C2F3D9DDB00112FD0 /* _HTable+Bucket.swift */; }; 7D72468E2F3D9DDB00112FD0 /* _HTable+Bitmap.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7D72467B2F3D9DDB00112FD0 /* _HTable+Bitmap.swift */; }; - 7D72468F2F3D9DDB00112FD0 /* _HTable+Small.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7D7246812F3D9DDB00112FD0 /* _HTable+Small.swift */; }; 7D7246902F3D9DDB00112FD0 /* _HTable+Sizing.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7D72467D2F3D9DDB00112FD0 /* _HTable+Sizing.swift */; }; 7D7246922F3D9E6800112FD0 /* RigidSetTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7D7246912F3D9E6800112FD0 /* RigidSetTests.swift */; }; 7D9748D42E9748930098631A /* BigString+Chunk+UTF16.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7D9748D32E9748930098631A /* BigString+Chunk+UTF16.swift */; }; @@ -614,6 +614,7 @@ 7D479CCC2F46D23400A8CB59 /* UniqueSet+BorrowingSequence.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UniqueSet+BorrowingSequence.swift"; sourceTree = ""; }; 7D479CD12F46DD8600A8CB59 /* RigidSet+Initializers.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "RigidSet+Initializers.swift"; sourceTree = ""; }; 7D479CD32F46DD9600A8CB59 /* UniqueSet+Initializers.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UniqueSet+Initializers.swift"; sourceTree = ""; }; + 7D479CE62F47D88000A8CB59 /* _HTable+Consumption.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "_HTable+Consumption.swift"; sourceTree = ""; }; 7D48BCC92E51524200D7F6F7 /* CMakeLists.txt */ = {isa = PBXFileReference; lastKnownFileType = text; path = CMakeLists.txt; sourceTree = ""; }; 7D48BCDB2E51554900D7F6F7 /* ClassBox.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ClassBox.swift; sourceTree = ""; }; 7D48BCDD2E51558700D7F6F7 /* TestPrintable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TestPrintable.swift; sourceTree = ""; }; @@ -649,7 +650,6 @@ 7D72467E2F3D9DDB00112FD0 /* _HTable+Find.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "_HTable+Find.swift"; sourceTree = ""; }; 7D72467F2F3D9DDB00112FD0 /* _HTable+Insert.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "_HTable+Insert.swift"; sourceTree = ""; }; 7D7246802F3D9DDB00112FD0 /* _HTable+Iteration.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "_HTable+Iteration.swift"; sourceTree = ""; }; - 7D7246812F3D9DDB00112FD0 /* _HTable+Small.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "_HTable+Small.swift"; sourceTree = ""; }; 7D7246832F3D9DDB00112FD0 /* RigidDictionary.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RigidDictionary.swift; sourceTree = ""; }; 7D7246852F3D9DDB00112FD0 /* RigidSet.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RigidSet.swift; sourceTree = ""; }; 7D7246912F3D9E6800112FD0 /* RigidSetTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RigidSetTests.swift; sourceTree = ""; }; @@ -1263,12 +1263,12 @@ 7D72467A2F3D9DDB00112FD0 /* _HTable.swift */, 7D72467B2F3D9DDB00112FD0 /* _HTable+Bitmap.swift */, 7D72467C2F3D9DDB00112FD0 /* _HTable+Bucket.swift */, - 7D72467D2F3D9DDB00112FD0 /* _HTable+Sizing.swift */, + 7D479CE62F47D88000A8CB59 /* _HTable+Consumption.swift */, + 7D9B75762F3E65DB0048157E /* _Htable+Debugging.swift */, 7D72467E2F3D9DDB00112FD0 /* _HTable+Find.swift */, 7D72467F2F3D9DDB00112FD0 /* _HTable+Insert.swift */, 7D7246802F3D9DDB00112FD0 /* _HTable+Iteration.swift */, - 7D7246812F3D9DDB00112FD0 /* _HTable+Small.swift */, - 7D9B75762F3E65DB0048157E /* _Htable+Debugging.swift */, + 7D72467D2F3D9DDB00112FD0 /* _HTable+Sizing.swift */, ); path = HashTable; sourceTree = ""; @@ -2411,7 +2411,6 @@ 7D72468C2F3D9DDB00112FD0 /* RigidSet.swift in Sources */, 7D72468D2F3D9DDB00112FD0 /* _HTable+Bucket.swift in Sources */, 7D72468E2F3D9DDB00112FD0 /* _HTable+Bitmap.swift in Sources */, - 7D72468F2F3D9DDB00112FD0 /* _HTable+Small.swift in Sources */, 7D7246902F3D9DDB00112FD0 /* _HTable+Sizing.swift in Sources */, 7D2A77992F27F2BF005780C7 /* Deque+Descriptions.swift in Sources */, 7D2A779A2F27F2BF005780C7 /* RigidDeque+Testing.swift in Sources */, @@ -2497,6 +2496,7 @@ 7D479CC72F46D21300A8CB59 /* UniqueSet+Resizing.swift in Sources */, 7DE9216129CA70F4004483EB /* _HashNode+Structural merge.swift in Sources */, 7DE9202F29CA70F3004483EB /* OrderedSet+ExpressibleByArrayLiteral.swift in Sources */, + 7D479CE72F47D88600A8CB59 /* _HTable+Consumption.swift in Sources */, 7DE9202B29CA70F3004483EB /* OrderedSet+Equatable.swift in Sources */, 7D479CC52F46D20B00A8CB59 /* UniqueSet+Consumption.swift in Sources */, 7DE920EA29CA70F4004483EB /* BitArray.swift in Sources */,