Using in as a parameter qualifier

Jonathan M Davis jmdavisProg at gmx.com
Fri May 31 18:15:20 PDT 2013


On Saturday, June 01, 2013 04:39:07 Shriramana Sharma wrote:
> On Sat, Jun 1, 2013 at 1:13 AM, Jonathan M Davis <jmdavisProg at gmx.com> 
wrote:
> > Certainly,
> > for anything that's small enough to fit in a register, it's likely to be
> > faster to pass by value.
> 
> So pardon my ignorance, but a pair which only contains two doubles --
> it would fit in a CPU register? How about a bezier class/struct which
> contains 4 pairs i.e. 8 doubles?

I really don't know. I don't generally deal with anything at the register 
level. Normally, what you do is you just pass by value unless you know that 
the struct is huge, and then you just worry about whether passing by value or 
by reference if profiling makes it clear that passing by value is a performance 
hit. But it seems to me that in general, worrying about whether something with 
a few ints or floats in it is going to be able to be passed in a register or 
not isn't particularly productive unless you've already found it to be a 
performance problem or know that you're in an environment where it really 
matters (in which case, you probably know enough about registers to be far 
better informed about what is and isn't efficient).

> And that would mean that passing by value is faster 'cause it's
> cheaper to copy the data to a register and have the processor operate
> on it directly than to copy the pointer (which lies beneath the
> reference) and have the indirection and *then* copy (?) the data? (I
> mean I'm not sure how these things work -- even when you are using a
> pointer/reference, copying from the memory to CPU register is
> unavoidable right? C++'s const T & only makes it so that copying from
> memory-to-memory can be avoided, right?

Why would you be copying the data if it was a pointer that was passed? If you 
pass by ref or by pointer, then only a pointer to the data is copied. Member 
variables may be copid into registers when operated on, but only the pieces 
that you operated on would end up being copied into registers, and only then 
if what you're doing requires a register. There's plenty of stuff that just 
gets passed around on the stack without any operations being done on them 
besides copying or moving them. It all depends on what's being done with them.

But it's been too long since I studied assembly for me to be able to go into 
all of the details with registers and the like.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list