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.

My suggestion: have two constructors, StaticArray<T>(size: LiteralInteger, 
initialValue: T) and 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.

> On May 31, 2017, at 10:38 AM, Charlie Monroe via swift-evolution 
> <[email protected]> wrote:
> 
> 
>>> On May 31, 2017, at 4:16 PM, Jean-Daniel <[email protected]> wrote:
>>> 
>>> 
>>>> Le 31 mai 2017 à 01:47, Anders Kierulf <[email protected]> a écrit :
>>>> 
>>>> 
>>>>> On May 30, 2017, at 3:36 PM, Jean-Daniel via swift-evolution 
>>>>> <[email protected]> wrote:
>>>>> 
>>>>> 
>>>>> Le 30 mai 2017 à 12:42, Charlie Monroe via swift-evolution 
>>>>> <[email protected]> a écrit :
>>>>> 
>>>>> 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.
>>>>> 
>>>>> This is due to fixed arrays being able to live on stack, while "normal 
>>>>> Array" is dynamically allocated on heap, etc.
>>>> 
>>>> Really ? Isn’t it due to the value semantic of swift arrays ?
>>>> 
>>>> If this is the former, its algorithm can probably be tweak to reuse the 
>>>> array and require less allocations.
>>>> 
>>>> Unless if you algorithm is eager in memory allocation/deallocation, you 
>>>> shouldn't get a significant difference between static array and dynamic 
>>>> array.
>>> 
>>> 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.
>> 
>> I don’t see how static sized array would solve that issue.
>> 
>> If swift wants to keep value semantic it has to support COW or a similar 
>> machinery. 
>> 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).
>> 
>> 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.
> 
> 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).  
> 
> https://github.com/apple/swift/blob/master/stdlib/public/core/ContiguousArrayBuffer.swift#L208
> 
> 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.
> 
> 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.
> 
> 
> _______________________________________________
> swift-evolution mailing list
> [email protected]
> https://lists.swift.org/mailman/listinfo/swift-evolution
_______________________________________________
swift-evolution mailing list
[email protected]
https://lists.swift.org/mailman/listinfo/swift-evolution

Reply via email to