From 2e6a6614679b1cf71d7fae911dd1ada59b445279 Mon Sep 17 00:00:00 2001 From: Jaap Wijnen Date: Thu, 24 Jul 2025 15:59:19 +0200 Subject: [PATCH 1/5] change map implementation to use unsafeUninitializedCapacity array initializer --- stdlib/public/core/Collection.swift | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/stdlib/public/core/Collection.swift b/stdlib/public/core/Collection.swift index b56f3d15b9d6d..382a6856806b7 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 0 ..< n { + unsafe storage[bufferIndex] = try transform(self[collectionIndex]) + formIndex(after: &collectionIndex) + initializedCount += 1 + } + _expectEnd(of: self, is: collectionIndex) } - - _expectEnd(of: self, is: i) - return Array(result) } // ABI-only entrypoint for the rethrows version of map, which has been From de689fd4fe0ea79f87a86e6148cb76fb610d7567 Mon Sep 17 00:00:00 2001 From: Jaap Wijnen Date: Sat, 26 Jul 2025 01:51:43 +0200 Subject: [PATCH 2/5] Update stdlib/public/core/Collection.swift Avoid capturing state that's also available from our parameters Co-authored-by: Guillaume Lessard --- stdlib/public/core/Collection.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stdlib/public/core/Collection.swift b/stdlib/public/core/Collection.swift index 382a6856806b7..dc66a7db8f0d2 100644 --- a/stdlib/public/core/Collection.swift +++ b/stdlib/public/core/Collection.swift @@ -1204,7 +1204,7 @@ extension Collection { initializedCount: inout Int ) throws(E) -> Void in var collectionIndex = self.startIndex - for bufferIndex in 0 ..< n { + for bufferIndex in storage.indices { unsafe storage[bufferIndex] = try transform(self[collectionIndex]) formIndex(after: &collectionIndex) initializedCount += 1 From 36eddee6495180636309933b52f780e19a709d67 Mon Sep 17 00:00:00 2001 From: Jaap Wijnen Date: Sat, 26 Jul 2025 01:53:00 +0200 Subject: [PATCH 3/5] Update stdlib/public/core/Collection.swift Co-authored-by: Guillaume Lessard --- stdlib/public/core/Collection.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stdlib/public/core/Collection.swift b/stdlib/public/core/Collection.swift index dc66a7db8f0d2..d04323910ec59 100644 --- a/stdlib/public/core/Collection.swift +++ b/stdlib/public/core/Collection.swift @@ -1205,7 +1205,7 @@ extension Collection { ) throws(E) -> Void in var collectionIndex = self.startIndex for bufferIndex in storage.indices { - unsafe storage[bufferIndex] = try transform(self[collectionIndex]) + unsafe storage.initializeElement(at: bufferIndex, to: try transform(self[collectionIndex]) formIndex(after: &collectionIndex) initializedCount += 1 } From 3b759d2a4dc6a0c9892e07f133a63d074a40bc55 Mon Sep 17 00:00:00 2001 From: Jaap Wijnen Date: Sat, 26 Jul 2025 02:48:49 +0200 Subject: [PATCH 4/5] Update Collection.swift Co-authored-by: Guillaume Lessard --- stdlib/public/core/Collection.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stdlib/public/core/Collection.swift b/stdlib/public/core/Collection.swift index d04323910ec59..a309ba6ed2c9d 100644 --- a/stdlib/public/core/Collection.swift +++ b/stdlib/public/core/Collection.swift @@ -1205,7 +1205,7 @@ extension Collection { ) throws(E) -> Void in var collectionIndex = self.startIndex for bufferIndex in storage.indices { - unsafe storage.initializeElement(at: bufferIndex, to: try transform(self[collectionIndex]) + unsafe storage.initializeElement(at: bufferIndex, to: try transform(self[collectionIndex])) formIndex(after: &collectionIndex) initializedCount += 1 } From 4d2bb42d8494a960aa7faaddab1a91e6cd9dcb98 Mon Sep 17 00:00:00 2001 From: Guillaume Lessard Date: Mon, 28 Jul 2025 12:01:28 -0700 Subject: [PATCH 5/5] Apply suggestions from code review Avoid bridging by building a `ContiguousArray` --- stdlib/public/core/Collection.swift | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/stdlib/public/core/Collection.swift b/stdlib/public/core/Collection.swift index a309ba6ed2c9d..2915cbc3493b4 100644 --- a/stdlib/public/core/Collection.swift +++ b/stdlib/public/core/Collection.swift @@ -1199,7 +1199,7 @@ extension Collection { return [] } - return try unsafe Array(unsafeUninitializedCapacity: n) { ( + return try unsafe Array(ContiguousArray(unsafeUninitializedCapacity: n) { ( storage: inout UnsafeMutableBufferPointer, initializedCount: inout Int ) throws(E) -> Void in @@ -1210,7 +1210,7 @@ extension Collection { initializedCount += 1 } _expectEnd(of: self, is: collectionIndex) - } + }) } // ABI-only entrypoint for the rethrows version of map, which has been