Reduce the generated code size of Timeout<T>::poll#7535
Reduce the generated code size of Timeout<T>::poll#7535Darksonn merged 3 commits intotokio-rs:masterfrom
Conversation
Pull out the tail portion that is invariant for T into a separate function. This prevents the compiler from duplicating it during monomorphization (at least when optimizing for the size of the generated binary) which decreases the size of dependent crates.
|
Seems reasonable. Do you have any data of the impact of a change like this on a real codebase? Is it actually measureable? |
|
It's small. From building https://github.com/signalapp/libsignal/ with Tokio 1.45.0, the changes here only reduce the Android arm64 binary size by 3KB (out of ~6MB after stripping, though most of the code isn't async). That being said, using |
Darksonn
left a comment
There was a problem hiding this comment.
This change sounds good to me. Just one nit.
Result<Infallible, E>::Ok was already not constructible, so the type was functionally equivalent to E. This just aligns the written types with that reality and, in the process, removes the confusing empty match.
|
I realized that the |
Pull out the tail portion that is invariant for T into a separate function.
Motivation
This reduces the size of the built binaries for crates that use Tokio and target minimal code size.
Solution
The Rust compiler generates a separate copy of LLVM IR for each instantiation of templated functions. That means that code in a templated function that doesn't depend on the template types can be needlessly duplicated. The tail of
Timeout::pollis one such function. Pulling the code that doesn't depend on the typeTout into a separate function lets the compiler emit it once instead of once for each instantiation ofTimeout.