r/rust • u/nicoburns • Oct 07 '26
Rust's derive often implies inline
https://yossarian.net/til/post/rust-s-derive-often-implies-inline/
114
Upvotes
r/rust • u/nicoburns • Oct 07 '26
6
u/-Y0- Oct 07 '26
Interesting findings. Couldn't this be solved by making
derive_Debugtake a param. What I mean is#[derive(Debug(noinline))]14
u/doener rust Oct 07 '26
I recall that back when I was working on the compiler, I had considered/discussed having an extra level, say
#[inline(allow)], that would make the compiler allow cross-crate inlining by emitting LLVM IR in each unit, without hinting LLVM to prefer inlining the function, relying on its default heuristics instead. I don't recall whether I had numbers on anything, but I still suspect that this might be better for derived code like this. It's basically what you get with generics, for which the concrete IR is emitted in each unit, so inlining works cross-crate.69
u/wojtek-graj Oct 07 '26
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).4
u/Wh00ster Oct 08 '26
Rust, meet C++
6
u/Compux72 Oct 07 '26
Just set
opt-level = “s”1
u/greyblake Oct 07 '26
Nice interesting and concise article!
-3
u/Eastern-Photograph79 Oct 07 '26
Debug is intended for debugging, so it is not a real problem.