Skip to content

Commit 8885997

Browse files
committed
Simplify map() to a simple loop
This commit simplifies map() of collections into a simple append-loop. The Swift optimizer can do a better job optimizing code without unsafe constructs. This change accelerates the MapReduce benchmark by 2x.
1 parent 7dd34bf commit 8885997

File tree

1 file changed

+5
-20
lines changed

1 file changed

+5
-20
lines changed

stdlib/public/core/Collection.swift

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -314,32 +314,17 @@ extension CollectionType {
314314
return []
315315
}
316316

317-
var builder =
318-
_UnsafePartiallyInitializedContiguousArrayBuffer<T>(
319-
initialCapacity: count)
317+
var builder = ContiguousArray<T>()
318+
builder.reserveCapacity(count)
320319

321320
var i = self.startIndex
322321

323-
// FIXME: Type checker doesn't allow a `rethrows` function to catch and
324-
// throw an error. It'd be nice to separate the success and failure cleanup
325-
// paths more cleanly than this.
326-
// Ensure the buffer is left in a destructible state.
327-
var finished = false
328-
defer {
329-
if !finished {
330-
_ = builder.finish()
331-
}
332-
}
333-
334-
// On the success path, we know we'll have exactly `count` elements
335-
// in the buffer, so we can bypass checks.
336322
for _ in 0..<count {
337-
builder.addWithExistingCapacity(try transform(self[i++]))
323+
builder.append(try transform(self[i++]))
338324
}
325+
339326
_expectEnd(i, self)
340-
let buffer = builder.finishWithOriginalCount()
341-
finished = true
342-
return Array(buffer)
327+
return Array(builder)
343328
}
344329

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

0 commit comments

Comments
 (0)