Skip to content

Commit cf7e438

Browse files
committed
[stdlib] Dictionary: Optimize loops in getObjects:andKeys:count: implementations
This breaks out of the loop immediately when the last slot has been filled in the output buffer, skipping a final sequence of iterations over empty buckets.
1 parent c7da0f3 commit cf7e438

File tree

1 file changed

+17
-13
lines changed

1 file changed

+17
-13
lines changed

stdlib/public/core/Dictionary.swift

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2139,28 +2139,32 @@ final internal class _HashableTypedNativeDictionaryStorage<Key: Hashable, Value>
21392139
andKeys keys: UnsafeMutablePointer<AnyObject>?,
21402140
count: Int) {
21412141
_precondition(count >= 0, "Invalid count")
2142-
// The user is expected to provide a storage of the correct size
2142+
guard count > 0 else { return }
2143+
var i = 0 // Current position in the output buffers
21432144
if let unmanagedKeys = _UnmanagedAnyObjectArray(keys) {
21442145
if let unmanagedObjects = _UnmanagedAnyObjectArray(objects) {
21452146
// keys nonnull, objects nonnull
2146-
for (offset: i, element: (key: key, value: val)) in full.enumerated() {
2147-
guard i < count else { break }
2148-
unmanagedObjects[i] = _bridgeAnythingToObjectiveC(val)
2147+
for (key, value) in full {
2148+
unmanagedObjects[i] = _bridgeAnythingToObjectiveC(value)
21492149
unmanagedKeys[i] = _bridgeAnythingToObjectiveC(key)
2150+
i += 1
2151+
guard i < count else { break }
21502152
}
21512153
} else {
21522154
// keys nonnull, objects null
2153-
for (offset: i, element: (key: key, value: _)) in full.enumerated() {
2154-
guard i < count else { break }
2155+
for (key, _) in full {
21552156
unmanagedKeys[i] = _bridgeAnythingToObjectiveC(key)
2157+
i += 1
2158+
guard i < count else { break }
21562159
}
21572160
}
21582161
} else {
21592162
if let unmanagedObjects = _UnmanagedAnyObjectArray(objects) {
21602163
// keys null, objects nonnull
2161-
for (offset: i, element: (key: _, value: val)) in full.enumerated() {
2164+
for (_, value) in full {
2165+
unmanagedObjects[i] = _bridgeAnythingToObjectiveC(value)
2166+
i += 1
21622167
guard i < count else { break }
2163-
unmanagedObjects[i] = _bridgeAnythingToObjectiveC(val)
21642168
}
21652169
} else {
21662170
// do nothing, both are null
@@ -2944,29 +2948,29 @@ final internal class _SwiftDeferredNSDictionary<Key: Hashable, Value>
29442948
_ count: Int
29452949
) {
29462950
_precondition(count >= 0, "Invalid count")
2951+
guard count > 0 else { return }
29472952
bridgeEverything()
2948-
// The user is expected to provide a storage of the correct size
2949-
var i = 0 // Position in the input storage
2953+
var i = 0 // Current position in the output buffers
29502954
let bucketCount = nativeBuffer.bucketCount
29512955

29522956
if let unmanagedKeys = _UnmanagedAnyObjectArray(keys) {
29532957
if let unmanagedObjects = _UnmanagedAnyObjectArray(objects) {
29542958
// keys nonnull, objects nonnull
29552959
for position in 0..<bucketCount {
29562960
if bridgedBuffer.isInitializedEntry(at: position) {
2957-
guard i < count else { break }
29582961
unmanagedObjects[i] = bridgedBuffer.value(at: position)
29592962
unmanagedKeys[i] = bridgedBuffer.key(at: position)
29602963
i += 1
2964+
guard i < count else { break }
29612965
}
29622966
}
29632967
} else {
29642968
// keys nonnull, objects null
29652969
for position in 0..<bucketCount {
29662970
if bridgedBuffer.isInitializedEntry(at: position) {
2967-
guard i < count else { break }
29682971
unmanagedKeys[i] = bridgedBuffer.key(at: position)
29692972
i += 1
2973+
guard i < count else { break }
29702974
}
29712975
}
29722976
}
@@ -2975,9 +2979,9 @@ final internal class _SwiftDeferredNSDictionary<Key: Hashable, Value>
29752979
// keys null, objects nonnull
29762980
for position in 0..<bucketCount {
29772981
if bridgedBuffer.isInitializedEntry(at: position) {
2978-
guard i < count else { break }
29792982
unmanagedObjects[i] = bridgedBuffer.value(at: position)
29802983
i += 1
2984+
guard i < count else { break }
29812985
}
29822986
}
29832987
} else {

0 commit comments

Comments
 (0)