Skip to content

Commit e686d28

Browse files
committed
change map implementation to use unsafeUninitializedCapacity array initializer
1 parent 07b846d commit e686d28

File tree

1 file changed

+11
-11
lines changed

1 file changed

+11
-11
lines changed

stdlib/public/core/Collection.swift

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1199,18 +1199,18 @@ extension Collection {
11991199
return []
12001200
}
12011201

1202-
var result = ContiguousArray<T>()
1203-
result.reserveCapacity(n)
1204-
1205-
var i = self.startIndex
1206-
1207-
for _ in 0..<n {
1208-
result.append(try transform(self[i]))
1209-
formIndex(after: &i)
1202+
return try unsafe Array<T>(unsafeUninitializedCapacity: n) { (
1203+
storage: inout UnsafeMutableBufferPointer<T>,
1204+
initializedCount: inout Int
1205+
) throws(E) -> Void in
1206+
var collectionIndex = self.startIndex
1207+
for bufferIndex in 0 ..< n {
1208+
unsafe storage[bufferIndex] = try transform(self[collectionIndex])
1209+
formIndex(after: &collectionIndex)
1210+
initializedCount += 1
1211+
}
1212+
_expectEnd(of: self, is: collectionIndex)
12101213
}
1211-
1212-
_expectEnd(of: self, is: i)
1213-
return Array(result)
12141214
}
12151215

12161216
// ABI-only entrypoint for the rethrows version of map, which has been

0 commit comments

Comments
 (0)