pass a struct by value/ref and size of the struct

tsbockman via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Mar 21 18:54:39 PDT 2016


On Monday, 21 March 2016 at 23:31:06 UTC, ref2401 wrote:
> I have got a plenty of structs in my project. Their size varies 
> from 12 bytes to 128 bytes.
> Is there a rule of thumb that states which structs I pass by 
> value and which I should pass by reference due to their size?
>
> Thanks.

Not really. It depends on the platform - most especially on the 
details of the cache hierarchy, I think.

The best answer to this kind of thing is generally: Do some 
benchmarks. (Although, you'll have to be somewhat skilled at 
benchmarking to get useful results, since the
  performance difference in most code is probably not that large 
either way.)

Note, also, that functions which are doing so little work with 
their arguments that you can actually notice the speed difference 
of pass-by-ref versus pass-by-value for small data structures are 
likely to be inlined anyway - in which case it makes no 
difference which you chose (I think...).

If you really just want a number, though - maybe try 
pass-by-reference for anything larger than 32 bytes.


More information about the Digitalmars-d-learn mailing list