diff --git a/stdlib/public/core/Collection.swift b/stdlib/public/core/Collection.swift index b56f3d15b9d6d..2915cbc3493b4 100644 --- a/stdlib/public/core/Collection.swift +++ b/stdlib/public/core/Collection.swift @@ -1199,18 +1199,18 @@ extension Collection { return [] } - var result = ContiguousArray() - result.reserveCapacity(n) - - var i = self.startIndex - - for _ in 0..(unsafeUninitializedCapacity: n) { ( + storage: inout UnsafeMutableBufferPointer, + initializedCount: inout Int + ) throws(E) -> Void in + var collectionIndex = self.startIndex + for bufferIndex in storage.indices { + unsafe storage.initializeElement(at: bufferIndex, to: try transform(self[collectionIndex])) + formIndex(after: &collectionIndex) + initializedCount += 1 + } + _expectEnd(of: self, is: collectionIndex) + }) } // ABI-only entrypoint for the rethrows version of map, which has been