[swift-evolution] [Proposal] Fix lazy filter

Dave Abrahams dabrahams at apple.com
Sun Jun 19 17:56:42 CDT 2016


on Sun Jun 19 2016, Антон Жилин <swift-evolution at swift.org> wrote:

>>
>> It's not a bug.  Measuring the length of the source before allocating
>> the destination array is usually a big win when compared to repeatedly
>> growing the array's memory and copying all its elements.
>> --
>> -Dave
>
> Usually yes, but not in the case of lazy filter. If predicate contains
> anything more than a dozen CPU instructions, single-pass version is
> faster.

...and predicates are often very lightweight.  I don't know how to prove
which approach will be faster.

> We often want the predicate to have side effects, but we cannot with
> current implementation: the side effects will be doubled.

Though I can't really endorse using a filter with side-effects, that's
why we have eager filter.

> I also wonder if it's possible to allocate array with capacity of
> underlying collection (before all lazy stuff) and shrink it in the end.

It might be possible, but right now we don't have a way to shrink memory
or efficiently find the length of the underlying collection, which could
itself be a lazy filter collection.

-- 
-Dave



More information about the swift-evolution mailing list