<div dir="ltr">Thanks! That explains a lot.<div><br></div><div>In my version, performance only is improved by 10%, so I'm not sure if the tradeoff is worth it:<div><br></div><div>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.</div></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Mon, Sep 5, 2016 at 9:50 PM, Dmitri Gribenko <span dir="ltr"><<a href="mailto:gribozavr@gmail.com" target="_blank">gribozavr@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span class="">On Mon, Sep 5, 2016 at 11:13 AM, Chris Eidhof via swift-dev<br>
<<a href="mailto:swift-dev@swift.org">swift-dev@swift.org</a>> wrote:<br>
> Hello swift-dev,<br>
><br>
> I was wondering why type erasers (e.g. for AnyIterator) are implemented the<br>
> way they are. I would implement AnyIterator like this:<br>
><br>
> final class MyAnyIterator<A>: IteratorProtocol {<br>
> var nextImpl: () -> A?<br>
><br>
> init<I: IteratorProtocol>(iterator: I) where I.Element == A {<br>
> var copy = iterator<br>
> nextImpl = { copy.next() }<br>
> }<br>
><br>
> func next() -> A? {<br>
> return nextImpl()<br>
> }<br>
> }<br>
><br>
> Instead, it is implemented in a class which contains a box.<br>
<br>
</span>Your approach requires adding a new closure property for every method<br>
that is forwarded. Subclassing does not require that.<br>
<br>
If you see a case where performance is not good, we would appreciate a<br>
bugreport at <a href="https://bugs.swift.org/" rel="noreferrer" target="_blank">https://bugs.swift.org/</a> . Please attach a self-contained<br>
reproducer. Thank you.<br>
<span class="HOEnZb"><font color="#888888"><br>
Dmitri<br>
<br>
--<br>
main(i,j){for(i=2;;i++){for(j=<wbr>2;j<i;j++){if(!(i%j)){j=0;<wbr>break;}}if<br>
(j){printf("%d\n",i);}}} /*Dmitri Gribenko <<a href="mailto:gribozavr@gmail.com">gribozavr@gmail.com</a>>*/<br>
</font></span></blockquote></div><br><br clear="all"><div><br></div>-- <br><div class="gmail_signature" data-smartmail="gmail_signature">Chris Eidhof</div>
</div>