Skip to content

Commit 9d908ec

Browse files
authored
[stdlib] Removes redundant buffer zeroing in IndexPath initialiser by using `init(unsafeUninitializedCapacity:initializingWith:) (#31122)
* Remove redundant buffer zeroing in IndexPath initializer * Use switch instead of multibranch if * Rename buf back into buffer to make diff clearer
1 parent 3cd4258 commit 9d908ec

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed

stdlib/public/Darwin/Foundation/IndexPath.swift

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -617,16 +617,17 @@ public struct IndexPath : ReferenceConvertible, Equatable, Hashable, MutableColl
617617

618618
fileprivate init(nsIndexPath: __shared ReferenceType) {
619619
let count = nsIndexPath.length
620-
if count == 0 {
620+
switch count {
621+
case 0:
621622
_indexes = []
622-
} else if count == 1 {
623+
case 1:
623624
_indexes = .single(nsIndexPath.index(atPosition: 0))
624-
} else if count == 2 {
625+
case 2:
625626
_indexes = .pair(nsIndexPath.index(atPosition: 0), nsIndexPath.index(atPosition: 1))
626-
} else {
627-
var indexes = Array<Int>(repeating: 0, count: count)
628-
indexes.withUnsafeMutableBufferPointer { (buffer: inout UnsafeMutableBufferPointer<Int>) -> Void in
627+
default:
628+
let indexes = Array<Int>(unsafeUninitializedCapacity: count) { buffer, initializedCount in
629629
nsIndexPath.getIndexes(buffer.baseAddress!, range: NSRange(location: 0, length: count))
630+
initializedCount = count
630631
}
631632
_indexes = .array(indexes)
632633
}

0 commit comments

Comments
 (0)