Cost of rebuilding futures in a loop?

⚓ Rust    📅 2026-01-23    👤 surdeus    👁️ 1      

surdeus

I'm working on an async library and I have a Connection object with a run function that advances a bunch of internal state. I need to handle a bunch of different things concurrently, so this leads me to have a big blob that kinda looks like this:

use futures_concurrency::future::Race;

loop {
    let wait_for_thing1 = async { ... };
    let wait_for_thing2 = async { ... };
    let wait_for_thing3 = async { ... };
    let wait_for_thing4 = async { ... };
    match (wait_for_thing1, wait_for_thing2, wait_for_thing3, wait_for_thing4).race() {
        // handle each thing
    }
}

All of the things use some piece of internal state (i.e. socket, channel) on the Connection and I use some helper methods on Connection for handling some of the events (in the match) to keep things less messy.

My main question is, is rebuilding futures in a loop like this problematic performance wise? Would it be worth switching to a design that uses Streams or something? Or is this not worth worrying about?

5 posts - 4 participants

Read full topic

🏷️ Rust_feed