Reputation: 419
If I have the following functions:
fn a() -> ! {
b()
}
fn b() -> ! {
loop {}
}
Does Rust optimize such calls to simple jumps?
I have looked at a Playground to show the assembly, but a
seems to just callq
the b
function.
Is this always the case?
Upvotes: 1
Views: 355
Reputation: 1167
First, you can tell the compiler to inline a function using #[inline]
: Technically, this does not guarantee inlining, but it should work in reasonable situations. I would recommend using this.
Second, as noted in the comments, you have to do an optimised build to see such inlining. Maybe a better tool for this would be Compiler Explorer. Here is a slight modification of your example (notice the -O
flag in compiler options). As you can see, everything is inlined into one infinite loop, and the compiler can even eliminate some work done in the loop if it can prove that it is useless.
Overall, unless you use #[inline]
, the result will depend on the contents of a
and b
. So I would advise to create some kind of minimal-viable-implementation of what you are trying to achieve with a
and b
, and then test it in Compiler Explorer.
Upvotes: 0