From b9b0b7ede5947a6be96b6a6be751d2473014528d Mon Sep 17 00:00:00 2001 From: Alex Hoppen Date: Sun, 24 Jun 2018 07:11:15 -0700 Subject: [PATCH 1/2] [stdlib] Make test for Sequence.map actually test a sequence AnySequence just forwards the call to map to its underlying storage which used to be an Array (and thus a collection). stride(from:through:by:) creates a proper Sequence. --- test/stdlib/ErrorHandling.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/stdlib/ErrorHandling.swift b/test/stdlib/ErrorHandling.swift index 945b0dc5fb264..56152fba322c6 100644 --- a/test/stdlib/ErrorHandling.swift +++ b/test/stdlib/ErrorHandling.swift @@ -280,7 +280,7 @@ ErrorHandlingTests.test("ErrorHandling/operators") { ErrorHandlingTests.test("ErrorHandling/Sequence map") { let initialCount = NoisyCount - let sequence = AnySequence([1, 2, 3]) + let sequence = stride(from: 1, through: 3, by: 1) for throwAtCount in 0...3 { var loopCount = 0 do { From fc36bed0e265d27f6bce127f775b4fb6c9aeb01a Mon Sep 17 00:00:00 2001 From: Alex Hoppen Date: Mon, 18 Jun 2018 15:41:06 -0700 Subject: [PATCH 2/2] [stdlib] Improve performance of Array initialization with known capacity --- stdlib/public/core/Collection.swift | 24 +++++++++++++++++++----- stdlib/public/core/Sequence.swift | 26 +++++++++++++++++++++----- 2 files changed, 40 insertions(+), 10 deletions(-) diff --git a/stdlib/public/core/Collection.swift b/stdlib/public/core/Collection.swift index ea57e573f6370..f883df1a83d7b 100644 --- a/stdlib/public/core/Collection.swift +++ b/stdlib/public/core/Collection.swift @@ -1325,17 +1325,31 @@ extension Collection { return [] } - var result = ContiguousArray() - result.reserveCapacity(n) + let result: ContiguousArray + var p: UnsafeMutablePointer + (result, p) = ContiguousArray._allocateUninitialized(n) var i = self.startIndex + var initializedElements = 0 - for _ in 0.. T ) rethrows -> [T] { let initialCapacity = underestimatedCount - var result = ContiguousArray() - result.reserveCapacity(initialCapacity) + var result: ContiguousArray + var p: UnsafeMutablePointer + (result, p) = ContiguousArray._allocateUninitialized(initialCapacity) var iterator = self.makeIterator() - // Add elements up to the initial capacity without checking for regrowth. - for _ in 0..