Robert Jacques:

> Well, fixed length arrays are an implicit/explicit pointer to some  
> (stack/heap) allocated memory. So returning a fixed length array usually  
> means returning a pointer to now invalid stack memory. Allowing  
> fixed-length arrays to be returned by value would be nice, but basically  
> means the compiler is wrapping the array in a struct, which is easy enough  
> to do yourself. Using wrappers also avoids the breaking the logical  
> semantics of arrays (i.e. pass by reference).

As usual this discussion is developing into other directions that are both 
interesting and bordeline too complex for me :-)

Arrays are the most common and useful data structure (beside single 
values/variables). And experience shows me that in some situations static 
arrays can lead to higher performance (for example if you have a matrix, and 
its number of columns is known at compile time and such number is a power of 2, 
then the compiler can use just a shift to find a cell).

So I'd like to see improving the D management of such arrays (for me it's a 
MUCH more common problem than for example the last contravariant argument types 
discussed by Andrei. I am for improving simple things that I can understand and 
use every day first, and complex things later. D2 is getting too much difficult 
for me), even if some extra annotations are necessary.

The possible ways that can be useful:
- To return small arrays (for example the ones used by SSE/AVX registers) by 
value. Non need to create silly wrapper structs. The compiler has to show a 
performance warning when such arrays is bigger than 1024 bytes of RAM.
- LLVM has good stack-allocated (alloca) arrays, like the ones introduced by 
C99. Having a way to use them in D too is good.
- A way to return just the reference to a dynamic array when the function 
already takes in input the reference to it.
- To automatically allocate and copy returned static arrays on the heap, to 
keep the situation safe and avoid too many copies of large arrays (so it gets 
copied only once here). I'm not sure about this.

Bye,
bearophile

Reply via email to