When should inline be used in Rust?
#[inline]has non-trivial compilation time costs (a function with
#[inline]is recompiled in every crate that uses it, like a generic function), and, more importantly, it can easily be a pessimisation, as it is giving an inline-hint to override LLVM’s better judgement by increasing the cost threshold under which the function will be inlined.
If you really want the fastest binary the compiler can make, you can/should use link-time optimisation (
rustc -C lto) which has access to all of the crate’s Rust dependencies, in inlinable form (including things without
#[inline]attributes). This is very slow to compile, but it’s isomorphic to adding
#[inline]to everything and I’d guess that the attributes on everything is even slower than
#[inline]should be preferred to be used only on performance-critical things; e.g. putting
#[inline]on most functions doing IO will be absolutely pointless for run-time performance (and just drag compile-time performance down the drain).