About ref used for performance reasons with struct

Namespace rswhite4 at googlemail.com
Mon Feb 11 08:27:28 PST 2013


> A good rule of thumb to know when to pass by ref for perf is :
>  - The struct is big, or contains mixed entities (floats and 
> ints). 2*size_t seems like a good heuristic from my experience.
>  - The struct have a postblit.
>
> The compiler doing it is superior to the user marking 
> everything :
>  - The user may forget, getting slow down for nothing.
>  - The user can get it wrong the other way around (pass by ref 
> when it isn't appropriate).
>  - This is really hard to get right in generic code.
>  - This is relatively easy to automate.

I thought of something like this:

void foo(A& a) { // the compiler choose if A should pass by ref 
or by value.

}

void bar(A a) { // normal behaviour.

}

That is identical with 'inline' in C++. You _can_ declare a 
function as inline, but finally the compiler choose if he really 
inline the function or not.


More information about the Digitalmars-d mailing list