Skip to content

Commit 2b47167

Browse files
committed
Simplify another implementation of map() and filter().
This commit removes _UnsafePartiallyInitializedContiguousArrayBuffer from two more methods. I did not measure the performance impact of this change but I am expecting this code to run faster.
1 parent 6ed3e94 commit 2b47167

File tree

1 file changed

+10
-35
lines changed

1 file changed

+10
-35
lines changed

stdlib/public/core/Sequence.swift

Lines changed: 10 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -295,33 +295,20 @@ extension SequenceType {
295295
@noescape transform: (Generator.Element) throws -> T
296296
) rethrows -> [T] {
297297
let initialCapacity = underestimateCount()
298-
var builder =
299-
_UnsafePartiallyInitializedContiguousArrayBuffer<T>(
300-
initialCapacity: initialCapacity)
298+
var builder = ContiguousArray<T>()
299+
builder.reserveCapacity(initialCapacity)
301300

302301
var generator = generate()
303302

304-
// FIXME: Type checker doesn't allow a `rethrows` function to catch and
305-
// throw an error. It'd be nice to separate the success and failure cleanup
306-
// paths more cleanly than this.
307-
// Ensure the buffer is left in a destructible state.
308-
var finished = false
309-
defer {
310-
if !finished {
311-
_ = builder.finish()
312-
}
313-
}
314303
// Add elements up to the initial capacity without checking for regrowth.
315304
for _ in 0..<initialCapacity {
316-
builder.addWithExistingCapacity(try transform(generator.next()!))
305+
builder.append(try transform(generator.next()!))
317306
}
318307
// Add remaining elements, if any.
319308
while let element = generator.next() {
320-
builder.add(try transform(element))
309+
builder.append(try transform(element))
321310
}
322-
let buffer = builder.finish()
323-
finished = true
324-
return Array(buffer)
311+
return Array(builder)
325312
}
326313

327314
/// Return an `Array` containing the elements of `self`,
@@ -330,30 +317,18 @@ extension SequenceType {
330317
public func filter(
331318
@noescape includeElement: (Generator.Element) throws -> Bool
332319
) rethrows -> [Generator.Element] {
333-
var builder =
334-
_UnsafePartiallyInitializedContiguousArrayBuffer<Generator.Element>(
335-
initialCapacity: 0)
320+
321+
var builder = ContiguousArray<Generator.Element>()
336322

337323
var generator = generate()
338324

339-
// FIXME: Type checker doesn't allow a `rethrows` function to catch and
340-
// throw an error. It'd be nice to separate the success and failure cleanup
341-
// paths more cleanly than this.
342-
// Ensure the buffer is left in a destructible state.
343-
var finished = false
344-
defer {
345-
if !finished {
346-
_ = builder.finish()
347-
}
348-
}
349325
while let element = generator.next() {
350326
if try includeElement(element) {
351-
builder.add(element)
327+
builder.append(element)
352328
}
353329
}
354-
let buffer = builder.finish()
355-
finished = true
356-
return Array(buffer)
330+
331+
return Array(builder)
357332
}
358333

359334
/// Returns a subsequence containing all but the first `n` elements.

0 commit comments

Comments
 (0)