<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 Aug 6, 2017, at 11:15 AM, Karl Wagner &lt;<a href="mailto:razielim@gmail.com" class="">razielim@gmail.com</a>&gt; 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=""><br class=""><div class=""><blockquote type="cite" class=""><div class="">On 4. Aug 2017, at 20:15, John McCall 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=""><div style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><blockquote type="cite" class=""><div class=""><br class="Apple-interchange-newline">On Aug 4, 2017, at 1:19 PM, Félix Cloutier 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=""><div class="" style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;">That's not a concern with the `let` case that Robert brought up, since you can't mutate a `let` array at all.<div class=""><br class=""></div><div class="">The big thing is that unconstrained escape analysis is uncomputable. Since Swift array storage is COW, any function that receives the array as a parameter is allowed to take a reference on its storage. If the storage lives on the stack and that reference outlives the stack frame, you've got a problem. It's the same problem that motivated @escaping for closures.</div><div class=""><br class=""></div><div class="">You could allow storage to be on the stack by forcing user to make a pessimistic copy, which is possibly not an improvement.</div></div></div></blockquote><div class=""><br class=""></div>Right. &nbsp;I think maybe the name people keeping using for this feature is misleading; a better name would be "inline arrays" or "directly-stored arrays". &nbsp;Having a fixed size is a necessary condition for storing the array elements directly, but the people asking for this feature are really asking for the different representation, not just the ability to statically constrain the size of an array.</div><div style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><br class=""></div><div style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class="">That representation difference comes with a lot of weaknesses and trade-offs, but it's also useful sometimes.</div><div style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><br class=""></div><div style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class="">John.</div><div style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><br class=""></div></div></blockquote><div class=""><br class=""></div></div>Right, and the question I’ve been asking (indirectly) is: why is this only useful for arrays? Doesn’t it really apply to any value-type which allocates storage which it manages with COW semantics (e.g. Dictionary, Set, Data, your own custom types…)? Really, we want to inform the compiler that the dynamically-allocated memory is part of the value - and if it sees that the storage is only allocated once, it should be allowed to allocate that storage inline with the value, on the stack.</div></div></blockquote><div><br class=""></div>There are absolutely things we could do as part of the Array implementation to dynamically avoid heap allocations for temporary arrays. &nbsp;However, it would be very difficult to take advantage of that for arrays stored in temporary structs or enums; worse, even if we could, it still wouldn't have the optimal representation that people want for mathematical types like vectors and matrices.</div><div><br class=""></div><div>John.</div><div><br class=""></div><div><br class=""><blockquote type="cite" class=""><div class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class=""><br class=""></div><div class="">As I understand it, the only problem with this is when a function takes such a value as a parameter and assigns it to some escaping reference (an ivar, global, or capturing it inside an escaping closure).</div><div class=""><br class=""></div><div class="">So why can’t such assignments simply check if the value has inline storage and copy it to the heap if necessary? The compiler should be able to optimise the function so the check (which is really cheap anyway) only needs to happen once per function. Because the entire type has value semantics, we can substitute the original value with the copy for the rest of the function (preventing further copies down the line).</div><div class=""><br class=""></div><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class=""><div class=""><font face="Menlo" class=""><br class=""></font></div><div class=""><font face="Menlo" class="">// Module one</font></div><div class=""><font face="Menlo" class=""><br class=""></font></div><div class=""><font face="Menlo" class="">import ModuleTwo</font></div><div class=""><font face="Menlo" class=""><br class=""></font></div><div class=""><font face="Menlo" class="">func doSomething() {</font></div><div class=""><font face="Menlo" class=""><br class=""></font></div><div class=""><font face="Menlo" class="">&nbsp; &nbsp; let values = (0..&lt;5).map { _ in random() } &nbsp; &nbsp;// allocated inline, since the size can never change</font></div><div class=""><font face="Menlo" class="">&nbsp; &nbsp; ModuleTwo.setGlobalItems(values) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//&nbsp;passes&nbsp;</font><span style="font-family: Menlo;" class="">a stack-allocated array to</span><span style="font-family: Menlo;" class="">&nbsp;the (opaque) function</span></div><div class=""><font face="Menlo" class="">}</font></div><div class=""><font face="Menlo" class=""><br class=""></font></div><div class=""><div class=""><font face="Menlo" class="">// Module two</font></div></div><div class=""><font face="Menlo" class=""><br class=""></font></div><div class=""><div class=""><font face="Menlo" class="">var GlobalItems = [Int]()</font></div></div><div class=""><div class=""><font face="Menlo" class="">var MoreGlobalItems = [Int]()</font></div></div><div class=""><div class=""><font face="Menlo" class=""><br class=""></font></div></div><div class=""><div class=""><font face="Menlo" class="">func setGlobalItems(_ newItems: [Int]) {</font></div></div><div class=""><font face="Menlo" class=""><br class=""></font></div><div class=""><div class=""><font face="Menlo" class="">&nbsp; &nbsp; GlobalItems = newItems &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// assignment to escaping reference: checks for inline storage, copies to heap if needed</font></div></div><div class=""><font face="Menlo" class=""><br class=""></font></div><div class=""><font face="Menlo" class="">&nbsp; &nbsp; // all references to&nbsp;‘newItems’ from this point refer to the copy known to be on the heap</font></div><div class=""><font face="Menlo" class=""><br class=""></font></div><div class=""><div class=""><font face="Menlo" class="">&nbsp; &nbsp; MoreGlobalItems = newItems &nbsp;// we already have a known out-of-line copy of the value; no checks or copying needed</font></div></div><div class=""><div class=""><font face="Menlo" class="">}</font></div></div><div class=""><br class=""></div><div class=""><font face="Menlo" class="">// To make it more explicit...</font></div><div class=""><font face="Menlo" class=""><br class=""></font></div><div class=""><div class=""><font face="Menlo" class="">func setGlobalItems_explicit(_ newItems: [Int]) {</font></div><div class=""><font face="Menlo" class=""><br class=""></font></div><div class=""><font face="Menlo" class="">&nbsp; &nbsp;&nbsp;let newItems_heap = newItems.backing.isAllocatedInline ? newItems(withBacking: newItems.backing.clone()) : newItems</font></div><div class=""><font face="Menlo" class="">&nbsp; &nbsp; GlobalItems &nbsp; &nbsp; &nbsp; =&nbsp;</font><span style="font-family: Menlo;" class="">newItems_heap</span></div><div class=""><font face="Menlo" class="">&nbsp; &nbsp; MoreGlobalItems &nbsp; =&nbsp;</font><span style="font-family: Menlo;" class="">newItems_heap</span></div><div class=""><font face="Menlo" class="">}</font></div></div></blockquote><div class=""><br class=""></div><div class=""><br class=""></div><div class="">This would require some special language support for values that allocate memory which is managed as COW.</div><div class=""><br class=""></div><div class="">- Karl</div><div class=""><br class=""></div></div></div></blockquote></div><br class=""></body></html>