<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><br class=""><div><blockquote type="cite" class=""><div class="">On 7 Apr 2016, at 18:54, Dmitri Gribenko via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class="">On Thu, Apr 7, 2016 at 12:20 AM, Vladimir.S via swift-evolution<br class="">&lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; wrote:<br class=""><blockquote type="cite" class="">But. .successor() .predecessor() methods for Int values do not follow these<br class="">rules for overflow situations. I.e. :<br class="">let i : Int8 = Int8.max<br class="">let k : Int8 = i.successor()<br class="">- is OK for current Swift compiler. We have i==127 and k==-128, no run-time<br class="">error.<br class=""></blockquote><br class="">This was done for performance reasons. &nbsp;Array's indices are Ints, and<br class="">adding an overflow check here was causing significant performance<br class="">issues when iterating over arrays.</div></blockquote><br class=""></div><div>Sorry to bump this after it’s been idle for a little while, but I was thinking about this again recently and I can’t come up with a test that verifies a meaningful performance difference. I just threw the following into a playground:</div><div><br class=""></div><div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class=""><div style="margin: 0px;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">import</span> Foundation</div><div style="margin: 0px; min-height: 13px;" class=""><br class=""></div><div style="margin: 0px;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">do</span> {</div><div style="margin: 0px; color: rgb(112, 61, 170);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">&nbsp; &nbsp; </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">let</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> startTime = </span>NSDate<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">().</span>timeIntervalSince1970</div><div style="margin: 0px;" class="">&nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">var</span> i = <span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">0</span></div><div style="margin: 0px;" class="">&nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">while</span> i &lt; <span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">1000000</span> { i = i &amp;+ <span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">1</span> }</div><div style="margin: 0px;" class="">&nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">let</span> elapsed = <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">NSDate</span>().<span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">timeIntervalSince1970</span> - startTime</div><div style="margin: 0px;" class="">}</div><div style="margin: 0px; min-height: 13px;" class=""><br class=""></div><div style="margin: 0px;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">do</span> {</div><div style="margin: 0px; color: rgb(112, 61, 170);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">&nbsp; &nbsp; </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">let</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> startTime = </span>NSDate<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">().</span>timeIntervalSince1970</div><div style="margin: 0px;" class="">&nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">var</span> i = <span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">0</span></div><div style="margin: 0px;" class="">&nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">while</span> i &lt; <span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">1000000</span> { i = i + <span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">1</span> }</div><div style="margin: 0px;" class="">&nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">let</span> elapsed = <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">NSDate</span>().<span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">timeIntervalSince1970</span> - startTime</div><div style="margin: 0px;" class="">}</div></div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class=""><br class=""></div><div style="margin: 0px;" class="">My results come out with no discernible performance difference; I suspect that this a side-effect of the iteration and storing overhead, but it seems to me that this is the kind of minimum boilerplate you are going to have anyway if you’re using .successor(). I know the issue is specifically with array iteration, but I don’t believe I actually need an array involved to demonstrate this, in fact the extra overhead would make the difference even less noticeable.</div><div style="margin: 0px;" class=""><br class=""></div><div style="margin: 0px;" class="">Is there a test that can demonstrate a more extreme difference? Even so, if the issue is with array iteration then it seems that the best place to fix that is in the array’s generator, rather than using the more generic IndexingGenerator that has no awareness of the underlying index type.</div></div></body></html>