Array concatenation & optimisation

Nick Treleaven nick at geany.org
Sun Jul 21 11:07:13 UTC 2024


On Sunday, 21 July 2024 at 10:33:38 UTC, Nick Treleaven wrote:
>> 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];
>> }
>> ```
>
> If there is enough spare capacity in a's allocation, no 
> allocation will occur.

Sorry, I see what you mean. I've compared the -vasm output 
(without -O) for that function and this one:

```d
void append(ref int[] a){
     enum e = [5, 4, 9];
     a ~= e;
}
```
The ASM output is the same. If you use runtime value elements I'm 
not sure but I think there's still no heap allocation.

I'm not good at reading ASM though.


More information about the Digitalmars-d-learn mailing list