What can cause pessimization like std::hint::black_box for pass-by-value?
⚓ Rust 📅 2026-04-28 👤 surdeus 👁️ 1I was doing some benchmarks on when it is faster to pass by value vs by reference for Copy types.
Basically when the compiler is able to optimize, it doesn't seem to make a difference even for fairly large types (a function that takes four Mat4 benchmarks the same by-value and by-reference).
However when wrapping the inputs with std::hint::black_box and calling the function, the by-value version of the above function is about 70% slower than the the by-reference version.
Are there situations that could trigger this kind of pessimization like black_box?
It's a little bit more ergonomic to write my functions as pass-by-value, but then I'm worried that in some cases they will generate slower code.
3 posts - 2 participants
🏷️ Rust_feed