The liabilities of binding rvalues to ref

deadalnix deadalnix at gmail.com
Sun May 5 09:07:11 PDT 2013


I wish to add to the discussion that you want to pass by ref for 
2 reasons (and the intent is very different) :
  - You intend to modify the value in some meaningful way for the 
caller. In which case, binding to rvalue don't make any sense.
  - You want to avoid creating copies, when this isn't necessary. 
In this case, this is a performance reason, and binding to rvalue 
make sense.

Note that knowing when to pass by value or by reference for 
performance is more tricky than many people tend to think. The 
reason is simple : you win an indirection, so if the data are 
small enough, and especially is the ABI say they are passed via 
registers, this is a win to pass by value. Many C++ dev will tell 
you that this is faster, but the fact is that C++ compiler can 
transform pass by ref into pass by value when it speedup things. 
It is especially hard problem to solve in generic code.

Here is to state the problem. Now let's discuss solutions.

As shown before the problem is about performance? And when it 
come to performance, the first thing we should look for is to 
allow the optimizer to kick in. The first change I'd propose is 
to allow the compiler to optimize away all pure copy operation 
(which include memory allocation). I don't know how far we can go 
with that, but that is IMO the first direction we should explore.

It is simpler for the dev, less error prone, will improve all 
programs, and do not require any new feature.

I'm well aware that the famous sufficiently smart compiler may 
lurk here, and so we may need to come back on the issue later if 
that approach fail. It is still worth trying.


More information about the Digitalmars-d mailing list