Taking arguments by value or by reference

Anonymouse zorael at gmail.com
Sun Oct 4 14:26:43 UTC 2020


On Saturday, 3 October 2020 at 23:47:32 UTC, Max Haughton wrote:
> The guiding principle to your function parameters should be 
> correctness - if I am passing a big struct around, if I want to 
> take ownership of it I probably want to take it by value but if 
> I want to modify it I should take it by reference (or by 
> pointer but don't overcomplicate, notice in the previous 
> example they lower to the same thing). If I just want to look 
> at it, it should be taken by const ref if possible (D const 
> isn't the same as C++ const, this may catch you out).
>
> Const-correctness is a rule to live by especially with an big 
> unwieldy struct.
>
> I would avoid the new in for now, but I would go with const ref 
> from what you've described so far.

I mostly really only want a read-only view of the struct, and 
whether a copy was done or not is academic. However, profiling 
showed (what I interpret as) a lot of copying being done in 
release builds specifically.

https://i.imgur.com/JJzh4Zc.jpg

Naturally a situation where I need ref I'd use ref, and in the 
rare cases where it actually helps to have a mutable copy 
directly I take it mutable. But if I understand what you're 
saying, and ignoring --preview=in, you'd recommend I use const 
ref where I would otherwise use const?

Is there some criteria I can go by when making this decision, or 
does it always reduce to looking at the disassembly?


More information about the Digitalmars-d-learn mailing list