I am trying to test whether a function I am using in my plug-in is more efficient when using pass-by-const-reference as opposed to pass-by-value. Is there an application you know of that I can objectively measure the memory requirements of my plug-in? Thank you.
You can use valgrind
When in doubt, though, I would just go with const-ref unless there’s a reason not to
Gotcha. Thank you!
Since a const reference is synthactic sugar for a pointer, as a general rule of thumb passing trivially copyable small types that are sized up to
sizeof (void*) by value is more efficient then passing by const reference, everything other is likely more efficient to pass by const ref.
A totally different approach would be inspecting the generated assembly of either version, e.g. on www.godbolt.org. You don‘t need to be an assembly expert to get a rough idea how your code translates to CPU instructions.
So if I understand you correctly, ints floats doubles pass by value, everything bigger pass by const-reference?
This is a good rule of thumb. Other examples of types that can be better passed by value then by const reference are e.g. a
juce::Colour, which is basically a struct containing four 8 bit variables → 4 byte trivially copyable and therefore is even lighter to pass by value in theory (as
sizeof (void*) is 8 byte on 64 bit platforms).
If this makes a real performance difference in real-world applications is questionable though.
By the way, the JUCE containers use this helper struct to figure out what type to pass by value or by const reference. You see that it defaults to const reference, except for the specializations which are only primitive types, non const references or pointers:
Good to know! Thank you.