DIP 1016 and const ref parameters

XavierAP n3minis-git at yahoo.es
Wed Jun 19 22:45:04 UTC 2019


On Wednesday, 19 June 2019 at 21:06:48 UTC, XavierAP wrote:
>
> Now with an rvalue returned from get, interesting, no copy. 
> Still, I wonder what really happened. Again, moving between 
> stacks would still be work. And a different optimization can 
> explain this non copy, for example inlining.

My guess as to what may be happening (I've never used a 
disassembler and I wasn't planning on starting today yet) is 
simple. The rvalue returned by get() is possibly not popped out 
from the stack, but rather left in the same place as sum() is 
called on it. This is actually optimum, but hardly an 
optimization, rather it's the easiest and least effort for the 
compiler. Again this would mean no moving -- which is good, 
because moving is work.

And also, this doesn't work in the general case. If parameters 
are by value, everything works perfect when I pass rvalues (but 
we already knew that, not answering my question); however if I 
pass lvalues they will be copied every time.

So my question is open... what about const ref rvalue parameters?

Or another possible optimization: in case the function is 
declared with const parameters (not ref), lvalues can be accessed 
by ref as an optimization; and rvalues can use the same trick 
they do now. As a consequence, no unnecessary copies ever -- 
thanks to const. This is even better because I don't have to type 
so many ref, which were never part of the algorithm but baby 
instructions for the dumb compiler (const & everywhere is one of 
the ugliest and most annoying things in C++).


More information about the Digitalmars-d-learn mailing list