Skip to content

Commit 5e969ce

Browse files
committed
[stdlib] Set, Dictionary: Simplify _find(_:,startBucket:)
The startBucket argument is _bucket(key) in the vast majority of cases. Add an overload with this default value.
1 parent b1c81e5 commit 5e969ce

File tree

2 files changed

+39
-33
lines changed

2 files changed

+39
-33
lines changed

stdlib/public/core/Dictionary.swift

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1437,7 +1437,7 @@ extension Dictionary: Equatable where Value: Equatable {
14371437
}
14381438

14391439
for (k, v) in lhs {
1440-
let (i, found) = rhsNative._find(k, startBucket: rhsNative._bucket(k))
1440+
let (i, found) = rhsNative._find(k)
14411441
if !found || rhsNative.value(at: i.bucket) != v {
14421442
return false
14431443
}
@@ -2135,8 +2135,7 @@ final internal class _HashableTypedNativeDictionaryStorage<Key: Hashable, Value>
21352135
guard let nativeKey = _conditionallyBridgeFromObjectiveC(aKey, Key.self)
21362136
else { return nil }
21372137

2138-
let (i, found) = native._find(nativeKey,
2139-
startBucket: native._bucket(nativeKey))
2138+
let (i, found) = native._find(nativeKey)
21402139

21412140
if found {
21422141
return native.bridgedValue(at: i)
@@ -2553,6 +2552,12 @@ extension _NativeDictionary where Key: Hashable {
25532552
return (bucket &- 1) & _bucketMask
25542553
}
25552554

2555+
@inlinable // FIXME(sil-serialize-all)
2556+
@inline(__always)
2557+
internal func _find(_ key: Key) -> (pos: Index, found: Bool) {
2558+
return _find(key, startBucket: _bucket(key))
2559+
}
2560+
25562561
/// Search for a given key starting from the specified bucket.
25572562
///
25582563
/// If the key is not present, returns the position where it could be
@@ -2593,7 +2598,7 @@ extension _NativeDictionary where Key: Hashable {
25932598
/// This function does *not* update `count`.
25942599
@inlinable // FIXME(sil-serialize-all)
25952600
internal func unsafeAddNew(key newKey: Key, value: Value) {
2596-
let (i, found) = _find(newKey, startBucket: _bucket(newKey))
2601+
let (i, found) = _find(newKey)
25972602
_precondition(
25982603
!found, "Duplicate key found in Dictionary. Keys may have been mutated after insertion")
25992604
initializeKey(newKey, value: value, at: i.bucket)
@@ -2610,8 +2615,7 @@ extension _NativeDictionary where Key: Hashable {
26102615
var dictionary = _NativeDictionary(minimumCapacity: elements.count)
26112616

26122617
for (key, value) in elements {
2613-
let (i, found) =
2614-
dictionary._find(key, startBucket: dictionary._bucket(key))
2618+
let (i, found) = dictionary._find(key)
26152619
_precondition(!found, "Dictionary literal contains duplicate keys")
26162620
dictionary.initializeKey(key, value: value, at: i.bucket)
26172621
}
@@ -2724,7 +2728,7 @@ extension _NativeDictionary/*: _DictionaryBuffer */ where Key: Hashable {
27242728
// Fast path that avoids computing the hash of the key.
27252729
return nil
27262730
}
2727-
let (i, found) = _find(key, startBucket: _bucket(key))
2731+
let (i, found) = _find(key)
27282732
return found ? i : nil
27292733
}
27302734

@@ -2736,7 +2740,7 @@ extension _NativeDictionary/*: _DictionaryBuffer */ where Key: Hashable {
27362740
return nil
27372741
}
27382742

2739-
let (i, found) = _find(key, startBucket: _bucket(key))
2743+
let (i, found) = _find(key)
27402744
if found {
27412745
return self.value(at: i.bucket)
27422746
}
@@ -3552,7 +3556,7 @@ extension Dictionary._Variant: _DictionaryBuffer {
35523556
internal mutating func nativeUpdateValue(
35533557
_ value: Value, forKey key: Key
35543558
) -> Value? {
3555-
var (i, found) = asNative._find(key, startBucket: asNative._bucket(key))
3559+
var (i, found) = asNative._find(key)
35563560

35573561
let minBuckets = found
35583562
? asNative.bucketCount
@@ -3562,7 +3566,7 @@ extension Dictionary._Variant: _DictionaryBuffer {
35623566

35633567
let (_, capacityChanged) = ensureUniqueNative(withBucketCount: minBuckets)
35643568
if capacityChanged {
3565-
i = asNative._find(key, startBucket: asNative._bucket(key)).pos
3569+
i = asNative._find(key).pos
35663570
}
35673571

35683572
let oldValue: Value? = found ? asNative.value(at: i.bucket) : nil
@@ -3638,7 +3642,7 @@ extension Dictionary._Variant: _DictionaryBuffer {
36383642
forKey key: Key, insertingDefault defaultValue: () -> Value
36393643
) -> (inserted: Bool, pointer: UnsafeMutablePointer<Value>) {
36403644

3641-
var (i, found) = asNative._find(key, startBucket: asNative._bucket(key))
3645+
var (i, found) = asNative._find(key)
36423646
if found {
36433647
let pointer = nativePointerToValue(at: ._native(i))
36443648
return (inserted: false, pointer: pointer)
@@ -3648,7 +3652,7 @@ extension Dictionary._Variant: _DictionaryBuffer {
36483652
let (_, capacityChanged) = ensureUniqueNative(withCapacity: minCapacity)
36493653

36503654
if capacityChanged {
3651-
i = asNative._find(key, startBucket: asNative._bucket(key)).pos
3655+
i = asNative._find(key).pos
36523656
}
36533657

36543658
asNative.initializeKey(key, value: defaultValue(), at: i.bucket)
@@ -3669,7 +3673,7 @@ extension Dictionary._Variant: _DictionaryBuffer {
36693673
_ value: Value, forKey key: Key
36703674
) -> (inserted: Bool, memberAfterInsert: Value) {
36713675

3672-
var (i, found) = asNative._find(key, startBucket: asNative._bucket(key))
3676+
var (i, found) = asNative._find(key)
36733677
if found {
36743678
return (inserted: false, memberAfterInsert: asNative.value(at: i.bucket))
36753679
}
@@ -3678,7 +3682,7 @@ extension Dictionary._Variant: _DictionaryBuffer {
36783682
let (_, capacityChanged) = ensureUniqueNative(withCapacity: minCapacity)
36793683

36803684
if capacityChanged {
3681-
i = asNative._find(key, startBucket: asNative._bucket(key)).pos
3685+
i = asNative._find(key).pos
36823686
}
36833687

36843688
asNative.initializeKey(key, value: value, at: i.bucket)
@@ -3720,7 +3724,7 @@ extension Dictionary._Variant: _DictionaryBuffer {
37203724
uniquingKeysWith combine: (Value, Value) throws -> Value
37213725
) rethrows where S.Element == (Key, Value) {
37223726
for (key, value) in keysAndValues {
3723-
var (i, found) = asNative._find(key, startBucket: asNative._bucket(key))
3727+
var (i, found) = asNative._find(key)
37243728

37253729
if found {
37263730
// This is in a separate variable to make the uniqueness check work in
@@ -3737,7 +3741,7 @@ extension Dictionary._Variant: _DictionaryBuffer {
37373741
let minCapacity = asNative.count + 1
37383742
let (_, capacityChanged) = ensureUniqueNative(withCapacity: minCapacity)
37393743
if capacityChanged {
3740-
i = asNative._find(key, startBucket: asNative._bucket(key)).pos
3744+
i = asNative._find(key).pos
37413745
}
37423746

37433747
asNative.initializeKey(key, value: value, at: i.bucket)
@@ -3763,14 +3767,14 @@ extension Dictionary._Variant: _DictionaryBuffer {
37633767
defer { _fixLifetime(asNative) }
37643768
for value in values {
37653769
let key = try keyForValue(value)
3766-
var (i, found) = asNative._find(key, startBucket: asNative._bucket(key))
3770+
var (i, found) = asNative._find(key)
37673771
if found {
37683772
asNative.values[i.bucket].append(value)
37693773
} else {
37703774
let minCapacity = asNative.count + 1
37713775
let (_, capacityChanged) = ensureUniqueNative(withCapacity: minCapacity)
37723776
if capacityChanged {
3773-
i = asNative._find(key, startBucket: asNative._bucket(key)).pos
3777+
i = asNative._find(key).pos
37743778
}
37753779

37763780
asNative.initializeKey(key, value: [value], at: i.bucket)

stdlib/public/core/Set.swift

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,7 @@ extension Set: Equatable {
439439

440440
for member in lhs {
441441
let (_, found) =
442-
rhsNative._find(member, startBucket: rhsNative._bucket(member))
442+
rhsNative._find(member)
443443
if !found {
444444
return false
445445
}
@@ -1715,9 +1715,7 @@ final internal class _HashableTypedNativeSetStorage<Element: Hashable>
17151715
guard let nativeKey = _conditionallyBridgeFromObjectiveC(aKey, Element.self)
17161716
else { return nil }
17171717

1718-
let (i, found) = asNative._find(nativeKey,
1719-
startBucket: asNative._bucket(nativeKey))
1720-
1718+
let (i, found) = asNative._find(nativeKey)
17211719
if found {
17221720
return asNative.bridgedValue(at: i)
17231721
}
@@ -2071,6 +2069,12 @@ extension _NativeSet/*: _SetBuffer*/ where Element: Hashable {
20712069
return (bucket &- 1) & _bucketMask
20722070
}
20732071

2072+
@inlinable // FIXME(sil-serialize-all)
2073+
@inline(__always)
2074+
internal func _find(_ key: Element) -> (pos: Index, found: Bool) {
2075+
return _find(key, startBucket: _bucket(key))
2076+
}
2077+
20742078
/// Search for a given key starting from the specified bucket.
20752079
///
20762080
/// If the key is not present, returns the position where it could be
@@ -2112,7 +2116,7 @@ extension _NativeSet/*: _SetBuffer*/ where Element: Hashable {
21122116

21132117
@inlinable // FIXME(sil-serialize-all)
21142118
internal func unsafeAddNew(key newKey: Element) {
2115-
let (i, found) = _find(newKey, startBucket: _bucket(newKey))
2119+
let (i, found) = _find(newKey)
21162120
_precondition(
21172121
!found, "Duplicate element found in Set. Elements may have been mutated after insertion")
21182122
initializeKey(newKey, at: i.bucket)
@@ -2129,8 +2133,7 @@ extension _NativeSet/*: _SetBuffer*/ where Element: Hashable {
21292133

21302134
var count = 0
21312135
for key in elements {
2132-
let (i, found) =
2133-
native._find(key, startBucket: native._bucket(key))
2136+
let (i, found) = native._find(key)
21342137
if found {
21352138
continue
21362139
}
@@ -2227,7 +2230,7 @@ extension _NativeSet/*: _SetBuffer*/ where Element: Hashable {
22272230
// Fast path that avoids computing the hash of the key.
22282231
return nil
22292232
}
2230-
let (i, found) = _find(key, startBucket: _bucket(key))
2233+
let (i, found) = _find(key)
22312234
return found ? i : nil
22322235
}
22332236

@@ -2239,7 +2242,7 @@ extension _NativeSet/*: _SetBuffer*/ where Element: Hashable {
22392242
return nil
22402243
}
22412244

2242-
let (i, found) = _find(key, startBucket: _bucket(key))
2245+
let (i, found) = _find(key)
22432246
if found {
22442247
return self.key(at: i.bucket)
22452248
}
@@ -2363,8 +2366,7 @@ final internal class _SwiftDeferredNSSet<Element: Hashable>
23632366
_conditionallyBridgeFromObjectiveC(object, Element.self)
23642367
else { return nil }
23652368

2366-
let (i, found) = native._find(
2367-
element, startBucket: native._bucket(element))
2369+
let (i, found) = native._find(element)
23682370
if found {
23692371
bridgeEverything()
23702372
return bridgedSet.value(at: i.bucket)
@@ -2923,7 +2925,7 @@ extension Set._Variant: _SetBuffer {
29232925
internal mutating func nativeUpdate(
29242926
with key: Element
29252927
) -> Element? {
2926-
var (i, found) = asNative._find(key, startBucket: asNative._bucket(key))
2928+
var (i, found) = asNative._find(key)
29272929

29282930
let minBuckets = found
29292931
? asNative.bucketCount
@@ -2933,7 +2935,7 @@ extension Set._Variant: _SetBuffer {
29332935

29342936
let (_, capacityChanged) = ensureUniqueNative(withBucketCount: minBuckets)
29352937
if capacityChanged {
2936-
i = asNative._find(key, startBucket: asNative._bucket(key)).pos
2938+
i = asNative._find(key).pos
29372939
}
29382940

29392941
let old: Element? = found ? asNative.key(at: i.bucket) : nil
@@ -2972,7 +2974,7 @@ extension Set._Variant: _SetBuffer {
29722974
) -> (inserted: Bool, memberAfterInsert: Element) {
29732975
ensureNative()
29742976

2975-
var (i, found) = asNative._find(key, startBucket: asNative._bucket(key))
2977+
var (i, found) = asNative._find(key)
29762978
if found {
29772979
return (inserted: false, memberAfterInsert: asNative.key(at: i.bucket))
29782980
}
@@ -2981,7 +2983,7 @@ extension Set._Variant: _SetBuffer {
29812983
let (_, capacityChanged) = ensureUniqueNative(withCapacity: minCapacity)
29822984

29832985
if capacityChanged {
2984-
i = asNative._find(key, startBucket: asNative._bucket(key)).pos
2986+
i = asNative._find(key).pos
29852987
}
29862988

29872989
asNative.initializeKey(key, at: i.bucket)

0 commit comments

Comments
 (0)