<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head><body dir="auto"><div></div><div>Without static arrays, swift cannot be used in high performance applications. The cost of repeated heap accesses is simply too high. And tuples are not ergonomic enough to use in the same manner as arrays. So I think we do need to add static arrays to Swift, if not necessarily in Swift 4.</div><div><br></div><div>My suggestion: have two constructors, <span style="background-color: rgba(255, 255, 255, 0);">StaticArray<T>(size: LiteralInteger, initialValue: T) and</span> StaticArray<T>(dimensions: [LiteralInteger], initialValue: T), one for the single-dimension case and one for the multidimensional case. By LiteralInteger I mean that the compiler rejects cases where the arguments are not integer literals, the same way it rejects non-literal values for enum cases. We should let the compiler turn calls of those constructors into stack-allocated static arrays behind the scenes. This could be as simple as synthesizing the code for a static array (see the gist I linked to). I don't really think we need any specialized syntax for this aside from the compiler limiting what can be passed to the constructor.</div><div><br>On May 31, 2017, at 10:38 AM, Charlie Monroe via swift-evolution <<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>> wrote:<br><br></div><blockquote type="cite"><div><meta http-equiv="Content-Type" content="text/html charset=utf-8"><br class=""><div><blockquote type="cite" class=""><div class="">On May 31, 2017, at 4:16 PM, Jean-Daniel <<a href="mailto:mailing@xenonium.com" class="">mailing@xenonium.com</a>> 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 <<a href="mailto:anders@smartgo.com" class="">anders@smartgo.com</a>> 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 <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> 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 <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> 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. </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). </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></div></blockquote><blockquote type="cite"><div><span>_______________________________________________</span><br><span>swift-evolution mailing list</span><br><span><a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a></span><br><span><a href="https://lists.swift.org/mailman/listinfo/swift-evolution">https://lists.swift.org/mailman/listinfo/swift-evolution</a></span><br></div></blockquote></body></html>