Skip to content

Conversation

@akonradi-signal
Copy link
Contributor

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::poll is one such function. Pulling the code that doesn't depend on the type T out into a separate function lets the compiler emit it once instead of once for each instantiation of Timeout.

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.
@rcoh
Copy link
Contributor

rcoh commented Aug 14, 2025

Seems reasonable. Do you have any data of the impact of a change like this on a real codebase? Is it actually measureable?

@akonradi-signal
Copy link
Contributor Author

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 cargo-bloat and filtering the output for "tokio::time::timeout.*poll" shows about 10% less code text size attributed to those functions for libsignal.

Copy link
Contributor

@Darksonn Darksonn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change sounds good to me. Just one nit.

@Darksonn Darksonn added A-tokio Area: The main tokio crate M-time Module: tokio/time labels Aug 15, 2025
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.
@akonradi-signal
Copy link
Contributor Author

I realized that the Result<Infallible, E> spelling was redundant and removed that in the last commit. If you'd rather keep it feel free to revert or drop that before merging.

@Darksonn Darksonn merged commit 925c614 into tokio-rs:master Aug 19, 2025
90 checks passed
@Darksonn Darksonn mentioned this pull request Oct 14, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-tokio Area: The main tokio crate M-time Module: tokio/time

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants