Skip to content

Commit 9846011

Browse files
authored
Merge pull request #2702 from valeriyvan/RedundantZeroingIndexPath
Removes redundant buffer zeroing in IndexPath
2 parents c1fee3a + 2511bde commit 9846011

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed

Sources/Foundation/IndexPath.swift

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

635635
fileprivate init(nsIndexPath: ReferenceType) {
636636
let count = nsIndexPath.length
637-
if count == 0 {
637+
switch count {
638+
case 0:
638639
_indexes = []
639-
} else if count == 1 {
640+
case 1:
640641
_indexes = .single(nsIndexPath.index(atPosition: 0))
641-
} else if count == 2 {
642+
case 2:
642643
_indexes = .pair(nsIndexPath.index(atPosition: 0), nsIndexPath.index(atPosition: 1))
643-
} else {
644-
var indexes = Array<Int>(repeating: 0, count: count)
645-
indexes.withUnsafeMutableBufferPointer { (buffer: inout UnsafeMutableBufferPointer<Int>) -> Void in
646-
nsIndexPath.getIndexes(buffer.baseAddress!, range: NSRange(location: 0, length: count))
644+
default:
645+
let indexes = Array<Int>(unsafeUninitializedCapacity: count) { buf, initializedCount in
646+
nsIndexPath.getIndexes(buf.baseAddress!, range: NSRange(location: 0, length: count))
647+
initializedCount = count
647648
}
648649
_indexes = .array(indexes)
649650
}

0 commit comments

Comments
 (0)