Description
Rust's implementation of #[inline]
functions is far from ideal. It's a major contributor to both slow compile times and bloated binaries.
There are an enormous number of #[inline]
functions in the standard libraries, and most have far more than one layer of inner #[inline]
function calls. The work to convert these functions to LLVM IR, optimize and translate to machine code is duplicated for every single crate. The inline
pass is not used at --opt-level=0
and --opt-level=1
, so the result is wasted time and duplicated code without any benefits.
It is possible to implement #[inline]
functions without duplicating the work of converting to LLVM IR and optimizing. The compiler can also entirely avoid any duplicated function bodies when the optimization level is not high enough for inline
, the function is used as a function pointer or it is above the threshold.
Before compiling all of the functions in a library crate, Rust should create an LLVM module with all of the externally reachable inline functions in the crate. It will run the optimization passes on this LLVM module before continuing to compile, and it end up stored as metadata in the rlib
or dynamic library in the bytecode format.
The compiler will then continue on with the compilation of the other functions in the crate. The work to generate optimized LLVM IR from the externally reachable #[inline]
is already complete and can be reused. These functions will not be marked internal, because other crates will be able to call through to these.
Now, when Rust is compiling another crate, it can start by fetching the LLVM bytecode for the required inline functions. These functions will be marked available_externally
and use the original symbol from the source library, so that if inlining does not occur there will be no duplicate code. At --opt-level=0
and --opt-level=1
, it can simply generate an external call immediately and ignore the bytecode blob.
It would also be possible to leverage this for instantiations of generic functions, by making the instantiations already done by the library available externally as LLVM bytecode blobs in the metadata.