Async Rust generates state machines under the hood, which can cause unexpected bloat in RAM usage, code size, and CPU overhead. Several practical techniques help reduce this bloat: avoid marking functions async when they don't need to be (use `std::future::ready` instead), eliminate async pass-through wrappers by returning `impl Future` directly, use `FutureExt::map` from the futures crate to handle postambles without extra state machines, refactor code to share await points and reduce duplicate states, and pass references to large variables rather than moving them into futures. The post also includes assembly-level analysis showing how await points multiply states, and notes that async Rust's compiler implementation never fully left MVP status.
Table of contents
What is async bloat not?Async, but no awaitsAsync pass-throughSmaller statemachines are better statemachinesWhat variables are part of the statemachine anyways?Wrap-upNeed help fixing bloat?2 Comments
Sort: