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

Poll Select futures without moving them #2704

Merged
merged 1 commit into from
Feb 3, 2023

Conversation

EFanZh
Copy link
Contributor

@EFanZh EFanZh commented Feb 3, 2023

The current implementation of Select moves the child futures out of the parent future before polling them, which maybe too costly if the sizes of child futures are too big or the future needs multiple polls before finally ready.

This PR changes the behavior to only move child futures once before ready, which could help improve performance. Here is a benchmark I wrote: https://gist.github.com/EFanZh/099bb8f6113deab71ed927674005abe7. With two 800 bytes future (on 64-bit platform) that each requires 100 polls before ready. I got more than 2x speed improvement with the new implementation.

Here is a link for comparing the assembly code of two implementations: https://godbolt.org/z/hhK1fGE5e.

This PR adds two unwraps()s, but it seems the compiler can optimize them out, or do I need to change them to unwrap_unchecked() to make sure no extra cost is introduced?

@EFanZh EFanZh requested a review from taiki-e as a code owner February 3, 2023 16:42
Copy link
Member

@taiki-e taiki-e left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@taiki-e taiki-e merged commit aa1f5c7 into rust-lang:master Feb 3, 2023
@taiki-e taiki-e added A-future Area: futures::future 0.3-backport: pending The maintainer accepted to backport this to the 0.3 branch, but backport has not been done yet. labels Feb 3, 2023
@EFanZh EFanZh deleted the optimize-select branch February 4, 2023 01:15
taiki-e pushed a commit that referenced this pull request Mar 11, 2023
@taiki-e taiki-e added 0.3-backport: completed and removed 0.3-backport: pending The maintainer accepted to backport this to the 0.3 branch, but backport has not been done yet. labels Mar 11, 2023
taiki-e pushed a commit that referenced this pull request Mar 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants