Re:Rust 1.100.0
by dxxvi on Thursday November 12, 2026 @07:15PM
(#50476386)
Is x = !x faster than x.toggle() because from my naked eyes no function is called? Or does the compiler know that the function toggle should be replace with `!=`? If the compiler does know that, how is that knowledge implemented? Is there a table of special functions that should be replaced with something else? Or some more clever way?
[ Parent
| on HN ]
Re:Rust 1.100.0
by lern_too_spel on Thursday November 12, 2026 @07:24PM
(#50476454)
It has the #[inline] attribute.
In this case the inline attribute probably isn't strictly necessary, because rustc heuristically inlines any functions that it deems trivial enough.
Re:Rust 1.100.0
by tialaramex on Thursday November 12, 2026 @08:53PM
(#50477088)
Thanks to Matt Godbolt, we can ask Compiler Explorer!
https://rust.godbolt.org/z/dPn3Y4r59
The optimizer gives the exact same machine code for both functions, and so it just outputs a single function and gives it an alias.
The compiler has a technique called "inlining" https://en.wikipedia.org/wiki/Inline_expansion
The LLVM backend used by Rust is jokingly said to have a inlining heuristic of "yes" ie it will always try to inline everything because that's usually faster.
Unfortunately it's not inlined in debug/no optimization mode so that's rough.
I'll go out on a limb and hypothesize that there are going to be much larger difference between debug and release mode for any non-trivial code that the overhead of `toggle` in debug mode will quite literally never end up mattering to anyone in practice.
Re:Rust 1.100.0
by lanyard-textile on Friday November 13, 2026 @03:45AM
(#50480291)
Why should it be? That's a feature of debug.