mirror of
https://github.com/apple/swift-collections
synced 2026-11-25 04:37:48 +00:00
[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.
This commit is contained in:
7 files changed
+40
-64
No files matched your search
+9
-16
@@ -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<Bucket>) -> Void
|
||||
) {
|
||||
var it = makeBucketIterator()
|
||||
while let next = it.nextOccupiedRegion() {
|
||||
consumer(next)
|
||||
}
|
||||
clear()
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -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,
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
Reference in new issue
Block a user