<div dir="ltr"><span style="font-size:13px">Hello swift-dev,</span><div style="font-size:13px"><br></div><div style="font-size:13px">I was wondering why type erasers (e.g. for AnyIterator) are implemented the way they are. I would implement AnyIterator like this:</div><div style="font-size:13px"><br></div><div style="font-size:13px"><div>final class MyAnyIterator<A>: IteratorProtocol {</div><div> var nextImpl: () -> A?</div><div> </div><div> init<I: IteratorProtocol>(iterator: I) where I.Element == A {</div><div> var copy = iterator</div><div> nextImpl = { copy.next() }</div><div> }</div><div> </div><div> func next() -> A? {</div><div> return nextImpl()</div><div> }</div><div>}</div></div><div style="font-size:13px"><br></div><div style="font-size:13px">Instead, it is implemented in a class which contains a box. The trick with subclassing and generics to actually erase the type feels like a bit of a hack, but I figured it would be there for performance reasons. In my limited testing, I noticed my version is 20% slower when compiling without optimizations. I didn't really understand why, even after reading through the SIL. However, when compiling with optimizations, I noticed my version is 10% faster.</div><div style="font-size:13px"><br></div><div style="font-size:13px">I would like to make a PR to change the stdlib, but before I go through the hassle, I'd like to hear what the reasons (at the time?) were for the current solution. Maybe it's just that the optimizer got better?</div><div style="font-size:13px"><br></div><div style="font-size:13px">Thanks!</div><div style="font-size:13px"><br></div><div style="font-size:13px">(Apologies if you receive this mail twice, my first message bounced saying it was waiting for moderator approval, it didn't seem to get approved)</div><div><br></div>-- <br><div class="gmail_signature">Chris Eidhof</div>
</div>