Array concatenation & optimisation

Johan j at j.nl
Sun Jul 21 15:41:50 UTC 2024


On Sunday, 21 July 2024 at 05:43:32 UTC, IchorDev wrote:
> 
> Does this mean that array literals are *always* separately 
> allocated first, or is this usually optimised out?

Not always allocated, see your example below.
I don't quite know what the heuristic is for allocation or not...

> For instance, will this example *always* allocate a new dynamic 
> array for the array literal, and then append it to the existing 
> one, even in optimised builds?
> ```d
> void append(ref int[] a){
> 	a ~= [5, 4, 9];
> }
> ```

https://d.godbolt.org/z/sG5Kancs4

The short array is not dynamically allocated (it's allocated on 
the stack, or for larger arrays it will be a hidden symbol in the 
binary image), even at `-O0` (i.e. `-O` was not passed).

-Johan



More information about the Digitalmars-d-learn mailing list