<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 May 31, 2017, at 4:16 PM, Jean-Daniel &lt;<a href="mailto:mailing@xenonium.com" class="">mailing@xenonium.com</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><blockquote type="cite" class="" style="font-family: Helvetica; font-size: 10px; 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=""><br class="Apple-interchange-newline">Le 31 mai 2017 à 01:47, Anders Kierulf &lt;<a href="mailto:anders@smartgo.com" class="">anders@smartgo.com</a>&gt; a écrit :</div><br class="Apple-interchange-newline"><div class=""><div class="" 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;"><blockquote type="cite" class=""><div class=""><br class="Apple-interchange-newline">On May 30, 2017, at 3:36 PM, Jean-Daniel 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="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;"><blockquote type="cite" class=""><div class=""><br class="Apple-interchange-newline">Le 30 mai 2017 à 12:42, Charlie Monroe via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; a écrit :</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;">There was someone a few weeks ago trying to port his Go game to Swift from (I believe) C++ and found out that the lack of fixed-size arrays was causing the move-computing algorithm to slow down significantly.<div class=""><br class=""></div><div class="">This is due to fixed arrays being able to live on stack, while "normal Array" is dynamically allocated on heap, etc.</div></div></div></blockquote><div class=""><br class=""></div><div class="">Really ? Isn’t it due to the value semantic of swift arrays ?</div><div class=""><br class=""></div><div class="">If this is the former, its algorithm can probably be tweak to reuse the array and require less allocations.</div><div class=""><br class=""></div><div class="">Unless if you algorithm is eager in memory allocation/deallocation, you shouldn't get a significant difference between static array and dynamic array.</div></div></div></blockquote><br class=""></div><div class="" 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;">Eliminating the dynamic allocations and extra indirections caused by the Swift array implementation can make a huge difference, not just in itself, but it also gives the compiler more opportunities to optimize the code.</div></div></blockquote><div style="font-family: Helvetica; font-size: 10px; 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: 10px; 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="">I don’t see how static sized array would solve that issue.</div><div style="font-family: Helvetica; font-size: 10px; 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: 10px; 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="">If swift wants to keep value semantic it has to support COW or a similar machinery.&nbsp;</div><div style="font-family: Helvetica; font-size: 10px; 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="">If you want to avoid COW and force the array to be allocated on the stack, it will impose swift to copy it at each function boundary (there is no @unescape for array to tell the compiler it can safely pass a reference).</div><div style="font-family: Helvetica; font-size: 10px; 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><span style="font-family: Helvetica; font-size: 10px; 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; float: none; display: inline !important;" class="">If what you want is just a preallocated array with reference semantic, wrap it in a class. You should get the same speed up than when using imported C arrays.</span></div></blockquote><br class=""></div><div>Creating an Array in Swift immediately makes a heap allocation. You then get COW semantics, but you already made a heap allocation (unless the array is empty). &nbsp;</div><div><br class=""></div><div><a href="https://github.com/apple/swift/blob/master/stdlib/public/core/Arrays.swift.gyb#L946" class="">https://github.com/apple/swift/blob/master/stdlib/public/core/ContiguousArrayBuffer.swift#L208</a></div><div><br class=""></div><div>With sized arrays, they can live on stack and the compiler can access the elements by referencing the stack frame (quick access), vs. the array, where it needs to dereference the pointer. Not to mention that the compiler can actually make some elements accessible via registers.</div><div><br class=""></div><div>Might seem like a small gain, but if you are making something that needs to compute really fast, even getting 2x speed is fast (and accessing heap via Array is more than 2x compared to accessing something on the stack). Not to mention if you create a lot of arrays (e.g. matrix computations), creating a static-sized array means just adding the size to the stack pointer.</div><div><br class=""></div><div><br class=""></div></body></html>