Here are two versions of map
, the first one representing the current implementation, slightly simplified. I've tested some expensive transforms with a class and struct and the difference in speed seems negligible. I have two questions:
- Is the head-on
reserve
->append
expected to be faster than initializing the contiguous array with a lazy map wrapper (_copyToContiguousArray
)? - If not, does
map
intendedly stick to the head-on approach?
extension Sequence {
@inlinable func map2<T>(
_ transform: (Element) -> T
) -> [T] {
let initialCapacity = underestimatedCount
var result = ContiguousArray<T>()
result.reserveCapacity(initialCapacity)
var iterator = self.makeIterator()
for _ in 0..<initialCapacity {
let next = iterator.next()!
result.append(transform(next))
}
while let element = iterator.next() {
result.append(transform(element))
}
return Array(result)
}
}
extension Sequence {
@inlinable func map1<T>(
_ transform: @escaping (Element) -> T
) -> [T] {
let result = ContiguousArray(lazy.map(transform))
return Array(result)
}
}