[swift-dev] Implementation of Type Erasers

Chris Eidhof chris at eidhof.nl
Tue Sep 6 02:37:04 CDT 2016


Thanks! That explains a lot.

In my version, performance only is improved by 10%, so I'm not sure if the
tradeoff is worth it:

In the isolated case of AnyIterator, there's only one method, so the sizeof
is the same (and the performance is worth it). In the case of AnySequence,
it would be a lot more work to write the implementation (and the size would
change a lot). Having two different mechanisms for type erasure seems like
it might confuse things even more.

On Mon, Sep 5, 2016 at 9:50 PM, Dmitri Gribenko <gribozavr at gmail.com> wrote:

> On Mon, Sep 5, 2016 at 11:13 AM, Chris Eidhof via swift-dev
> <swift-dev at swift.org> wrote:
> > Hello swift-dev,
> >
> > I was wondering why type erasers (e.g. for AnyIterator) are implemented
> the
> > way they are. I would implement AnyIterator like this:
> >
> > final class MyAnyIterator<A>: IteratorProtocol {
> >     var nextImpl: () -> A?
> >
> >     init<I: IteratorProtocol>(iterator: I) where I.Element == A {
> >         var copy = iterator
> >         nextImpl = { copy.next() }
> >     }
> >
> >     func next() -> A? {
> >         return nextImpl()
> >     }
> > }
> >
> > Instead, it is implemented in a class which contains a box.
>
> Your approach requires adding a new closure property for every method
> that is forwarded.  Subclassing does not require that.
>
> If you see a case where performance is not good, we would appreciate a
> bugreport at https://bugs.swift.org/ .  Please attach a self-contained
> reproducer.  Thank you.
>
> 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>*/
>



-- 
Chris Eidhof
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-dev/attachments/20160906/61484876/attachment.html>


More information about the swift-dev mailing list