Static Arrays: Behaviour of Passing & Returning

Quantum Nerd qnerd at posteo.org
Thu Mar 22 13:24:34 UTC 2018


On Wednesday, 21 March 2018 at 12:15:13 UTC, Simen Kjærås wrote:

>
> I'm pretty sure you're seeing NRVO - Named Return Value 
> Optimization. Also called copy elision:
> https://en.wikipedia.org/wiki/Copy_elision
>
> The compiler sees that when you return r, it's immediately 
> assigned to b. Since it's a waste of cycles to allocate twice 
> and copy the values, the function is told where the result will 
> end up, and writes it there immediately.
>
> The reason you don't see that with c is that the compiler isn't 
> very smart, especially not with optimizations turned off, so it 
> doesn't know for sure that it can safely overwrite the values 
> already in c at that point.
>


Thanks a lot, that explains things.

I was really surprised, as I had compiler optimizations
in mind before I posted. I ran the tests without any optimization
using ldc2 -g -O0, but the outcome was the same.



Thanks for your help,


qnerd



More information about the Digitalmars-d-learn mailing list