Skip to content

Commit ef00751

Browse files
committed
stdlib: move Dictionary's find functions into __RawDictionaryStorage.
The find functions do not require the generic Value parameter. Moving them to __RawDictionaryStorage allows to define them with only one generic parameter: the Key. This allows the optimizer to share specializations for dictionaries which have the same Key, but a different Value. Also, prevent inlining of the find-functions to save some additional code size.
1 parent 9e0721e commit ef00751

File tree

2 files changed

+31
-10
lines changed

2 files changed

+31
-10
lines changed

stdlib/public/core/DictionaryStorage.swift

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,35 @@ extension __RawDictionaryStorage {
196196
return Builtin.bridgeFromRawPointer(
197197
Builtin.addressof(&_swiftEmptyDictionarySingleton))
198198
}
199+
200+
@_alwaysEmitIntoClient
201+
@inline(__always)
202+
internal final func uncheckedKey<Key: Hashable>(at bucket: _HashTable.Bucket) -> Key {
203+
defer { _fixLifetime(self) }
204+
_internalInvariant(_hashTable.isOccupied(bucket))
205+
let keys = _rawKeys.assumingMemoryBound(to: Key.self)
206+
return keys[bucket.offset]
207+
}
208+
209+
@_alwaysEmitIntoClient
210+
@inline(never)
211+
internal final func find<Key: Hashable>(_ key: Key) -> (bucket: _HashTable.Bucket, found: Bool) {
212+
return find(key, hashValue: key._rawHashValue(seed: _seed))
213+
}
214+
215+
@_alwaysEmitIntoClient
216+
@inline(never)
217+
internal final func find<Key: Hashable>(_ key: Key, hashValue: Int) -> (bucket: _HashTable.Bucket, found: Bool) {
218+
let hashTable = _hashTable
219+
var bucket = hashTable.idealBucket(forHashValue: hashValue)
220+
while hashTable._isOccupied(bucket) {
221+
if uncheckedKey(at: bucket) == key {
222+
return (bucket, true)
223+
}
224+
bucket = hashTable.bucket(wrappedAfter: bucket)
225+
}
226+
return (bucket, false)
227+
}
199228
}
200229

201230
@usableFromInline

stdlib/public/core/NativeDictionary.swift

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ extension _NativeDictionary { // Low-level lookup operations
161161
@inlinable
162162
@inline(__always)
163163
internal func find(_ key: Key) -> (bucket: Bucket, found: Bool) {
164-
return find(key, hashValue: self.hashValue(for: key))
164+
return _storage.find(key)
165165
}
166166

167167
/// Search for a given element, assuming it has the specified hash value.
@@ -174,15 +174,7 @@ extension _NativeDictionary { // Low-level lookup operations
174174
_ key: Key,
175175
hashValue: Int
176176
) -> (bucket: Bucket, found: Bool) {
177-
let hashTable = self.hashTable
178-
var bucket = hashTable.idealBucket(forHashValue: hashValue)
179-
while hashTable._isOccupied(bucket) {
180-
if uncheckedKey(at: bucket) == key {
181-
return (bucket, true)
182-
}
183-
bucket = hashTable.bucket(wrappedAfter: bucket)
184-
}
185-
return (bucket, false)
177+
return _storage.find(key, hashValue: hashValue)
186178
}
187179
}
188180

0 commit comments

Comments
 (0)