Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not allocate capacity in ready_chunks #2657

Closed
wants to merge 1 commit into from

Conversation

stepancheg
Copy link
Contributor

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
Copy link
Member

taiki-e commented Oct 22, 2022

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
Copy link
Contributor Author

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.

`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).
stepancheg added a commit to stepancheg/futures-rs that referenced this pull request Oct 30, 2022
Reserving `ready_chunks` limit every time may be too expensive when
the limit is high but only a few number of messages available.

Now rely on `Stream::size_hint` to reserve the capacity.  If
underlying stream implements `size_hint`, this will work great.
Otherwise `ready_chunk` will be somewhat less efficient.

This is better alternative to rust-lang#2657
stepancheg added a commit to stepancheg/futures-rs that referenced this pull request Oct 30, 2022
Reserving `ready_chunks` limit every time may be too expensive when
the limit is high but only a few number of messages available.

Now rely on `Stream::size_hint` to reserve the capacity.  If
underlying stream implements `size_hint`, this will work great.
Otherwise `ready_chunk` will be somewhat less efficient.

This is better alternative to rust-lang#2657
@taiki-e
Copy link
Member

taiki-e commented Nov 27, 2022

Closing in favor of #2661.

@taiki-e taiki-e closed this Nov 27, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants