Skip to content

Commit f9b03d9

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. (cherry picked from commit cf7e438)
1 parent aec9c50 commit f9b03d9

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
@@ -2177,28 +2177,32 @@ final internal class _HashableTypedNativeDictionaryStorage<Key: Hashable, Value>
21772177
andKeys keys: UnsafeMutablePointer<AnyObject>?,
21782178
count: Int) {
21792179
_precondition(count >= 0, "Invalid count")
2180-
// The user is expected to provide a storage of the correct size
2180+
guard count > 0 else { return }
2181+
var i = 0 // Current position in the output buffers
21812182
if let unmanagedKeys = _UnmanagedAnyObjectArray(keys) {
21822183
if let unmanagedObjects = _UnmanagedAnyObjectArray(objects) {
21832184
// keys nonnull, objects nonnull
2184-
for (offset: i, element: (key: key, value: val)) in full.enumerated() {
2185-
guard i < count else { break }
2186-
unmanagedObjects[i] = _bridgeAnythingToObjectiveC(val)
2185+
for (key, value) in full {
2186+
unmanagedObjects[i] = _bridgeAnythingToObjectiveC(value)
21872187
unmanagedKeys[i] = _bridgeAnythingToObjectiveC(key)
2188+
i += 1
2189+
guard i < count else { break }
21882190
}
21892191
} else {
21902192
// keys nonnull, objects null
2191-
for (offset: i, element: (key: key, value: _)) in full.enumerated() {
2192-
guard i < count else { break }
2193+
for (key, _) in full {
21932194
unmanagedKeys[i] = _bridgeAnythingToObjectiveC(key)
2195+
i += 1
2196+
guard i < count else { break }
21942197
}
21952198
}
21962199
} else {
21972200
if let unmanagedObjects = _UnmanagedAnyObjectArray(objects) {
21982201
// keys null, objects nonnull
2199-
for (offset: i, element: (key: _, value: val)) in full.enumerated() {
2202+
for (_, value) in full {
2203+
unmanagedObjects[i] = _bridgeAnythingToObjectiveC(value)
2204+
i += 1
22002205
guard i < count else { break }
2201-
unmanagedObjects[i] = _bridgeAnythingToObjectiveC(val)
22022206
}
22032207
} else {
22042208
// do nothing, both are null
@@ -2971,29 +2975,29 @@ final internal class _SwiftDeferredNSDictionary<Key: Hashable, Value>
29712975
_ count: Int
29722976
) {
29732977
_precondition(count >= 0, "Invalid count")
2978+
guard count > 0 else { return }
29742979
bridgeEverything()
2975-
// The user is expected to provide a storage of the correct size
2976-
var i = 0 // Position in the input storage
2980+
var i = 0 // Current position in the output buffers
29772981
let bucketCount = nativeBuffer.bucketCount
29782982

29792983
if let unmanagedKeys = _UnmanagedAnyObjectArray(keys) {
29802984
if let unmanagedObjects = _UnmanagedAnyObjectArray(objects) {
29812985
// keys nonnull, objects nonnull
29822986
for position in 0..<bucketCount {
29832987
if bridgedBuffer.isInitializedEntry(at: position) {
2984-
guard i < count else { break }
29852988
unmanagedObjects[i] = bridgedBuffer.value(at: position)
29862989
unmanagedKeys[i] = bridgedBuffer.key(at: position)
29872990
i += 1
2991+
guard i < count else { break }
29882992
}
29892993
}
29902994
} else {
29912995
// keys nonnull, objects null
29922996
for position in 0..<bucketCount {
29932997
if bridgedBuffer.isInitializedEntry(at: position) {
2994-
guard i < count else { break }
29952998
unmanagedKeys[i] = bridgedBuffer.key(at: position)
29962999
i += 1
3000+
guard i < count else { break }
29973001
}
29983002
}
29993003
}
@@ -3002,9 +3006,9 @@ final internal class _SwiftDeferredNSDictionary<Key: Hashable, Value>
30023006
// keys null, objects nonnull
30033007
for position in 0..<bucketCount {
30043008
if bridgedBuffer.isInitializedEntry(at: position) {
3005-
guard i < count else { break }
30063009
unmanagedObjects[i] = bridgedBuffer.value(at: position)
30073010
i += 1
3011+
guard i < count else { break }
30083012
}
30093013
}
30103014
} else {

0 commit comments

Comments
 (0)