On Wednesday, 12 September 2018 at 14:46:22 UTC, rikki cattermole wrote:
On 13/09/2018 2:34 AM, drug wrote:
12.09.2018 15:14, Timoses пишет:
On Tuesday, 11 September 2018 at 12:07:14 UTC, drug wrote:

If data size is less or equal to total size of available registers (that can be used to pass values) than passing by value is more efficient. Passing data with size less than register size by reference isn't efficient because you pass pointer (that has register size) and access memory using it.

Thank you!
So if I pass by reference it will ALWAYS use the address in memory to fetch the data, whereas passing it by value enables the (compiler?..) to use the register which has already loaded the data from memory (stack for example)?

Honestly, I'm not an expert in this domain, but I think so.

Recently used areas of the stack will be available in the cache in most cases. The issue with passing by reference is it increases the indirection (number of pointers) that it must go through to get to the raw bytes.

This is why classes are bad but structs are good. Even if the struct is allocated on the heap and you're accessing it via a pointer.

This sounds like classes should never be used.. I don't recall right now what issues I'm usually encountering with structs that make me switch to classes (in D).

So passing by reference is generally only applicable (logical) to structs and non-reference types + only makes sense when the function being called is supposed to change the referenced value without returning it.

Except, as Steven pointed out in his post when dealing with large lvalue structs.

This all seems quite complicated to "get right" when writing code. I'm sure there are compiler optimizations run on this? Or is that not possible due to the nature of difference in ref and value passing.


Anyhow, thanks for the answers! I bet it's possible to write books on this topic.. Or just mention ones that already were written : ~D.

Reply via email to