[swift-users] AnySequence and type erasure
Dmitri Gribenko
gribozavr at gmail.com
Sat Jun 18 03:23:42 CDT 2016
On Fri, Jun 17, 2016 at 12:37 PM, Svein Halvor Halvorsen
<svein.h at lvor.halvorsen.cc> wrote:
> Ok. Good to hear that I'm not too far off :)
>
> Can you provide me with an example of a sequence type or two that has some
> map or other function that would benefit from an optimized wrapper? Or:
> under what circumstances would the stdlib implementation outperform mine?
Compare the default implementations of map() for Sequence and
Collection. In the Sequence case, we don't know the size of the
resulting array, so we have to grow the resulting array as we pull the
elements from the sequence. In the case of running Collection.map()
we know the final size from the count property. In the case of Array
we can do even better and eliminate a check (the _expectEnd()
call). However, certain collections where calculating the number of
elements might be expensive, can opt into the Sequence behavior (e.g.,
various string views); this is up to the designer of the specific
collection.
Dmitri
--
main(i,j){for(i=2;;i++){for(j=2;j<i;j++){if(!(i%j)){j=0;break;}}if
(j){printf("%d\n",i);}}} /*Dmitri Gribenko <gribozavr at gmail.com>*/
More information about the swift-users
mailing list