rust-lang / futures-rs

Zero-cost asynchronous programming in Rust
https://rust-lang.github.io/futures-rs/
Apache License 2.0
5.39k stars 625 forks source link

code size #1222

Closed aep closed 6 years ago

aep commented 6 years ago

why is futures so large? It's even bigger than std.

this is with lto, in release mode.

$ cargo bloat --release --crates -w -n 0 --full-fn
    Finished release [optimized] target(s) in 0.13s
   Analyzing target/release/carrier

 File  .text     Size Name
 9.6%  20.9% 606.3KiB futures
 9.2%  20.1% 584.0KiB std
...
 0.3%   0.8% 22.0KiB                futures <futures::stream::fold::Fold<S, F, Fut, T> as futures::future::Future>::poll::h3c2b7ae24026580c

 0.3%   0.6% 17.8KiB                futures <futures::future::or_else::OrElse<A, B, F> as futures::future::Future>::poll::hf6b5022846370fb2

 0.3%   0.6% 16.8KiB                futures <futures::future::into_stream::IntoStream<F> as futures::stream::Stream>::poll::h01f8c585ae211b25
cramertj commented 6 years ago

All of the functions in futures are monomorphized for your particular usage of them, so any code that you wrote using futures combinators will show up as monomorphized futures code. Your cargo bloat invocation above is likely showing the size of all the futures code that is generated as a result of compiling your application, as well as any of your non-futures code that is inlined into the futures functions.

aep commented 6 years ago

So do I understand correctly that's this cannot be fixed because it's an inherit property of the typing system?

cramertj commented 6 years ago

@aep The fix here is to make your application monomorphize and inline less. If you're using futures 0.1, you can consider using Box::new(your_fut) as Box<Future<Item = ..., Error = ...>> calls to reduce monomorphization. If you're using futures-preview 0.3, you can use FutureObj::new(Box::new(...)). More generally, flags such as opt-level=s, opt-level=z, and panic=abort will tell rustc/LLVM to make optimization decisions that will result in smaller codesize,