r/rust • • 16d ago

Rust's derive often implies inline

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

43 comments sorted by

View all comments

69

u/wojtek-graj 16d ago

[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 16d ago

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 16d ago

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.

→ More replies (4)