▲ | SuperV1234 9 days ago | |||||||
Note that taking a 'const' by-value parameter is very sensible in some cases, so it is not something that could be detected as a typo by the C++ compiler in general. | ||||||||
▲ | Animats 5 days ago | parent | next [-] | |||||||
Right. Copying is very fast on modern CPUs, at least up to the size of a cache line. Especially if the data being copied was just created and is in the L1 cache. If something is const, whether to pass it by reference or value is a decision the compiler should make. There's a size threshold, and it varies with the target hardware. It might be 2 bytes on an Arduino and 16 bytes on a machine with 128-bit arithmetic. Or even as big as a cache line. That optimization is reportedly made by the Rust compiler. It's an old optimization, first seen in Modula 1, which had strict enough semantics to make it work. Rust can do this because the strict affine type model prohibits aliasing. So the program can't tell if it got the original or a copy for types that are Copy. C++ does not have strong enough assurances to make that a safe optimization. "-fstrict-aliasing" enables such optimizations, but the language does not actually validate that there is no aliasing. If you are worried about this, you have either used a profiler to determine that there is a performance problem in a very heavily used inner loop, or you are wasting your time. | ||||||||
▲ | spacechild1 5 days ago | parent | prev | next [-] | |||||||
Yes. For example, if an argument fits into the size of a register, it's better to pass by value to avoid the extra indirection. | ||||||||
| ||||||||
▲ | Rubberducky1324 5 days ago | parent | prev [-] | |||||||
clang-tidy can often detect these. If the body of the function doesn't modify the value, for example. But it needs to be conservative of course, in general you can't do this. |