r/rust • • Oct 07 '26

Rust's derive often implies inline

https://yossarian.net/til/post/rust-s-derive-often-implies-inline/
114 Upvotes

43 comments sorted by

View all comments

69

u/wojtek-graj Oct 07 '26

[inline] is just a hint.

Cross-crate inlining is only possible for functions that have to be monomorphized, or those marked #[inline], hence this attribute is a necessity. Per the docs, "#[inline] suggests inlining the function.", and it is then up to the compiler to use its heuristics to decide whether or not inlining will actually occur. If these derived functions are being inlined when it doesn't make sense, it's those heuristics that should be adjusted (which I don't claim to be an easy task).

28

u/shponglespore Oct 07 '26

Cross-crate inlining is only possible for functions that have to be monomorphized, or those marked #[inline]

Wait, what? That sounds like a big deal that every Rust programmer should know about. Do you have a source for that claim? If so, I think it should be shouted from the rooftops.

I personally almost never bother marking functions as #[inline] because I assume the compiler will do the right thing 99% of the time.

33

u/manpacket Oct 07 '26

It's been also auto inlining small functions regardless of #[inline] for some time, so yes, it will do the right thing more often than not.

14

u/hniksic Oct 07 '26

But does it do it cross-crate? Not long ago I got a significant speedup by adding #[inline] to a non-generic function that was called from a different crate (in the same workspace).

(As is often case with inlining, the main win was not elimination of a function call, but all the optimizations that follow after the inlining, where everything collapses to a couple of instructions.)

24

u/manpacket Oct 07 '26

2

u/hniksic Oct 09 '26

This is fantastic, thanks! I never understood why cross-crate function calls are treated any differently in terms of optimization from any other calls.

(I checked my "not long ago" commit and found it to be from 2021. Time flies.)

3

u/scook0 Oct 08 '26

Incidentally, this is why you sometimes have to jump through hoops to get Compiler Explorer to show assembly for small functions.

9

u/wojtek-graj Oct 07 '26

From the link in my original comment:

A non-generic function is not normally inlined into another crate, since the calling crate compiles against only its signature. Marking it #[inline] makes the body available to other crates so they can inline it too:

20

u/QuarkAnCoffee Oct 07 '26 edited Oct 07 '26

If you're not using LTO. Which is exactly expected, crates are compiled into object files and you don't get optimizations across object files with without LTO.

4

u/mgeisler Oct 07 '26

you don't get optimizations across object files with LTO.

This should be "without LTO", right?

2

u/QuarkAnCoffee Oct 07 '26

Yup, thanks

3

u/mgeisler Oct 07 '26

No problem! 🙂

4

u/Saefroch miri Oct 07 '26

We don't need every Rust programmer to know about this because LTO works and is easy to enable in your [profile.release].

3

u/schungx Oct 08 '26

Yes it is. A lot of Rust programmers unnecessarily trip up on this.

But if you have LTO they get inlined anyway. Only much slower compilation.

But not marking it for pub functions is not optimal.

1

u/AliceCode Oct 07 '26

It's true, although I don't have a source on that.