Skip to content

stdlib: move Dictionary's find functions into __RawDictionaryStorage. #29353

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions stdlib/public/core/DictionaryStorage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,35 @@ extension __RawDictionaryStorage {
return Builtin.bridgeFromRawPointer(
Builtin.addressof(&_swiftEmptyDictionarySingleton))
}

@_alwaysEmitIntoClient
@inline(__always)
internal final func uncheckedKey<Key: Hashable>(at bucket: _HashTable.Bucket) -> Key {
defer { _fixLifetime(self) }
_internalInvariant(_hashTable.isOccupied(bucket))
let keys = _rawKeys.assumingMemoryBound(to: Key.self)
return keys[bucket.offset]
}

@_alwaysEmitIntoClient
@inline(never)
internal final func find<Key: Hashable>(_ key: Key) -> (bucket: _HashTable.Bucket, found: Bool) {
return find(key, hashValue: key._rawHashValue(seed: _seed))
}

@_alwaysEmitIntoClient
@inline(never)
internal final func find<Key: Hashable>(_ key: Key, hashValue: Int) -> (bucket: _HashTable.Bucket, found: Bool) {
let hashTable = _hashTable
var bucket = hashTable.idealBucket(forHashValue: hashValue)
while hashTable._isOccupied(bucket) {
if uncheckedKey(at: bucket) == key {
return (bucket, true)
}
bucket = hashTable.bucket(wrappedAfter: bucket)
}
return (bucket, false)
}
}

@usableFromInline
Expand Down
12 changes: 2 additions & 10 deletions stdlib/public/core/NativeDictionary.swift
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ extension _NativeDictionary { // Low-level lookup operations
@inlinable
@inline(__always)
internal func find(_ key: Key) -> (bucket: Bucket, found: Bool) {
return find(key, hashValue: self.hashValue(for: key))
return _storage.find(key)
}

/// Search for a given element, assuming it has the specified hash value.
Expand All @@ -174,15 +174,7 @@ extension _NativeDictionary { // Low-level lookup operations
_ key: Key,
hashValue: Int
) -> (bucket: Bucket, found: Bool) {
let hashTable = self.hashTable
var bucket = hashTable.idealBucket(forHashValue: hashValue)
while hashTable._isOccupied(bucket) {
if uncheckedKey(at: bucket) == key {
return (bucket, true)
}
bucket = hashTable.bucket(wrappedAfter: bucket)
}
return (bucket, false)
return _storage.find(key, hashValue: hashValue)
}
}

Expand Down