<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 Jan 20, 2017, at 11:27 PM, Charles Srstka <<a href="mailto:cocoadev@charlessoft.com" class="">cocoadev@charlessoft.com</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><meta http-equiv="Content-Type" content="text/html charset=utf-8" class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><blockquote type="cite" class="">On Jan 21, 2017, at 12:37 AM, Russ Bishop <<a href="mailto:xenadu@gmail.com" class="">xenadu@gmail.com</a>> wrote:<br class=""></blockquote><div class=""><blockquote type="cite" class=""><br class="Apple-interchange-newline"><div class=""><div class="" style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;"><div class=""><blockquote type="cite" class="" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;"><div class="">On Jan 16, 2017, at 9:43 AM, Charles Srstka via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> wrote:</div></blockquote></div><div class=""><blockquote type="cite" class=""><b class="">I don’t even know how long it actually takes to finish this test, because the last time I did this I eventually got sick of waiting and killed the process. So, I don’t know quite how many orders of magnitude slower it is, but it’s a lot.</b></blockquote><br class=""></div><div class="">That’s all the endorsement I need. +1 from me.</div><div class=""><br class=""></div><div class=""><br class=""></div><div class="">I do wonder if there is any way to get this sort of optimization out of the compiler. I suppose it would be difficult because the compiler doesn’t know what the mutable vs immutable pairs are or if such a pair even exists (array doesn’t have appending()).</div></div></div></blockquote></div><br class=""><div class="">The (somewhat naïve) assumption that some optimization of this sort might be going on is what led me to do the speed test in the first place. However, when you think about it, it’d be really quite hard to do. A reduce that builds an array consists of the closure that adds something to an array, and the reduce function itself. With the code to both of these, it’s not inconceivable that the compiler could figure out what you’re doing, but unfortunately the two components live in different modules / compilation units. The closure doesn’t know that its return value is just going to be replacing the passed-in value, and the reduce function doesn’t know that the closure isn’t going to store the original array somewhere, so neither can really know that it’s safe to modify the array in place.</div><div class=""><br class=""></div><div class="">Charles</div></div></div></blockquote></div><div class=""><br class=""></div><div class=""><br class=""><div class="">I was thinking of an optimization like this:</div><div class=""><br class=""></div><div class="">1. The closure or function doesn’t capture anything (and thus by definition nothing can escape the closure)</div><div class="">2. ???</div><div class="">3. Therefore input returns true for isUniquelyReferenced and no copying of the underlying storage is required (Profit!)</div><div class=""><br class=""></div><div class=""><br class=""></div><div class="">The problem is obviously in step 2. We don’t have any way to express the necessary contract other than inout, which requires a separate definition. If it worked like throws/rethrows where a non-mutating closure promoted to an inout closure then we could just change the definition of reduce (though you’d still have to return the value). The compiler would need to understand that ownership of the underlying array storage moves from the input parameter to the constructed array inside the closure (and ultimately the return value). That’s a bit of a tall order.</div><div class=""><br class=""></div><div class="">That leads me to think about why inout is required (because isKnownUniquelyReferenced returns false otherwise). Why can’t the compiler determine that the intermediate array is unique? Take this program:</div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><div style="margin: 0px; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2" class="">func</span><span style="font-variant-ligatures: no-common-ligatures" class=""> doSomething(</span><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2" class="">_</span><span style="font-variant-ligatures: no-common-ligatures" class=""> x: </span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">MyStruct</span><span style="font-variant-ligatures: no-common-ligatures" class="">) -> </span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">MyStruct</span><span style="font-variant-ligatures: no-common-ligatures" class=""> {</span></div><div style="margin: 0px; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2" class="">var</span><span style="font-variant-ligatures: no-common-ligatures" class=""> mutX = x</span></div><div style="margin: 0px; line-height: normal; color: rgb(62, 30, 129);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2" class="">let</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> isUnique = </span><span style="font-variant-ligatures: no-common-ligatures" class="">isKnownUniquelyReferenced</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">(&mutX.</span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">refTypeInstance</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">)</span></div><div style="margin: 0px; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #3e1e81" class="">print</span><span style="font-variant-ligatures: no-common-ligatures" class="">(</span><span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b" class="">"isUnique = </span><span style="font-variant-ligatures: no-common-ligatures" class="">\</span><span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b" class="">(</span><span style="font-variant-ligatures: no-common-ligatures" class="">isUnique</span><span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b" class="">)"</span><span style="font-variant-ligatures: no-common-ligatures" class="">) </span><span style="font-variant-ligatures: no-common-ligatures; color: #008400" class="">//prints false</span></div><div style="margin: 0px; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2" class="">return</span><span style="font-variant-ligatures: no-common-ligatures" class=""> mutX</span></div><div style="margin: 0px; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">}</span></div><div style="margin: 0px; line-height: normal; color: rgb(49, 89, 93);" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">doSomething</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">(</span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">MyStruct</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">())</span></div></div></div><div class=""><br class=""></div><div class=""><br class=""></div><div class="">The fact that storage is uniquely owned is trivially provable to a human but not to the compiler. Why? </div><div class=""><br class=""></div><div class=""><br class=""></div><div class="">These are just idle musings. My suspicion is you need ownership annotations (aka borrow checker) to make this tractable but I don’t have any proof of that.</div><div class=""><br class=""></div><div class="">Russ</div></div></body></html>