About ref used for performance reasons with struct

deadalnix deadalnix at gmail.com
Mon Feb 11 07:08:52 PST 2013


On Monday, 11 February 2013 at 12:16:14 UTC, Namespace wrote:
> I see. Nice idea. But what is the criterion for the compiler to 
> pass 'a' by ref?
> I'm still therefor, to mark such paramters with '&' or 
> something but that's maybe my C++ background.
> I'm curious to see if Walter or Andrei say something to the 
> idea.

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.

The obvious drawback is possible code bloat, but that isn't that 
bad (and can be used as heuristic by the compiler to decide if it 
should pass by ref or not).


More information about the Digitalmars-d mailing list