Array concatenation & optimisation

Nick Treleaven nick at geany.org
Mon Jul 22 12:36:27 UTC 2024


On Sunday, 21 July 2024 at 10:33:38 UTC, Nick Treleaven wrote:
> 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?
>
> My understanding is that they do not allocate if used to 
> initialize or assign a static array. That includes passing an 
> array literal as an argument to a static array function 
> parameter.
>
> A scope slice can also be initialized from an array literal in 
> @nogc code:
> https://dlang.org/changelog/2.102.0.html#dmd.scope-array-on-stack

Another (related) case that doesn't heap allocate is when passing 
a literal as a scope parameter:

```d
void main() @nogc {
     f([1,2]);
}

void f(scope int[]) @nogc;
```

I will look at adding these cases to the spec.


More information about the Digitalmars-d-learn mailing list