About structs and performant handling

Daniel Murphy yebblies at nospamgmail.com
Sat Mar 9 18:17:34 PST 2013


"Ali Çehreli" <acehreli at yahoo.com> wrote in message 
news:khgfc6$1m9i$1 at digitalmars.com...
>
> To be fair, I have also accessed the members of the structs inside the 
> function to see whether the pointer dereferencing in the by-ref case 
> brought any cost. Apparently I have been ignorant in modern CPU designs 
> because I was surprised to see that pointer dereferencing seemingly had no 
> cost at all. My guess would be that the object is completely inside the 
> processor's cache.
>

Accessing a member of a stuct on the stack:
mov EDX, dword ptr [ESP+stackoffset+memberoffset]

Accessing a member of a struct on the heap:
(assume pointer to struct is in EAX)
mov EDX, dword ptr [EAX+memberoffset]

A lot of the time the heap pointer will be in a register already.  Stack 
memory will almost always be in the caches, and so will recently used heap 
memory.

If you want to measure the cost of loading the heap pointer then 
dereferencing, you might want to mark all the registers as used so the 
compiler is forced to reload.

eg with
asm {}

>
> > For example:
> > [code]
> > struct A { }
> > void foo(A& a) { }
> > [/code]
> > The compiler will check by these kind of parameters if they are structs
> > and if the size is proven greater as N (maybe 16 - 24) bit. If not, the
> > '&' will be ignored. The function take in this cases normally lvalues as
> > copy and moves rvalues.
> > But if the struct size is greater than N the compiler changes the
> > storage class of this parameter to ref.
>
> I hope others with compiler knowledge will chime in here.
>
> I think the type of the parameter that is passed is intrinsic to how the 
> function gets compiled. I think, for that to work, the compiler would have 
> to compile two versions of the function, one taking by-value and the other 
> taking by-ref.
>

A better way to do with would be to change (or extend) the abi, so that 
structs over a certain size are always passed by reference with this 
parameter type.  Then you only need one version of the function.  We could 
use auto ref for this. 




More information about the Digitalmars-d mailing list