rust-lang / futures-rs

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

Do not allocate capacity in ready_chunks #2657

Closed stepancheg closed 1 year ago

stepancheg commented 2 years ago

read_chunks practically often returns items when not all capacity filled. So always allocating full capacity results in excessive allocation.

This is especially an issue when capacity parameter passed is large (to be able to handle streams efficiently under high load).

taiki-e commented 2 years ago

Hmm... Depending on the size of the item, reallocation may occur with each push, but 1024 bytes on the stack is relatively large, so it probably seems fine?

stepancheg commented 2 years ago

reallocation may occur with each push,

Snippet you pointed at is min non-zero capacity. The following line is:

let cap = cmp::max(min_non_zero_cap, cap);

where old cap is double of previous capacity. So it will be amortized constant per push. So for 8 bytes objects for example, reallocation will happen at sizes: 4, 8, 16, 32, ...

Or I didn't understand your comment.

taiki-e commented 1 year ago

Closing in favor of https://github.com/rust-lang/futures-rs/pull/2661.