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.

If the object is larger than the size of a register on the target machine, it is implicitly passed by ref (i.e. struct fields are accessed by offset from the stack pointer). So the question is: does the compiler need to create temporaries and is this an expensive operation? In C++ the problem is that there are lots of non-POD types which have expensive copy constructors (like std::vector) and that's why taking objects by const& is good guideline. In D structs are implicitly movable (can be memcpy-ed around without their postblit this(this) function called) and that's why I think that passing by value shouldn't be as large problem as in C++, especially if you are using a good optimizing compiler such as LDC or GDC.

Anyway, modern hardware in combination with compiler optimizations can often suprise you, so I recommend profiling your code and doing microbenchmarks to figure out where you may have performance problems. In my experience, large amounts of small memory allocations is orders of magnitude larger problem than the copying of large value types. The next thing to look for is inefficient memory layout with lots of indirections.

Reply via email to