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

chore: Avoid allocation if PollSemaphore is unused #3634

Merged
merged 6 commits into from
Mar 22, 2021

Conversation

NeoLegends
Copy link
Contributor

@NeoLegends NeoLegends commented Mar 22, 2021

Motivation

Cloning PollSemaphore allocates, even if it ends up unused (e. g. if .try_acquire_owned is used as a fast-path). This is due to the underlying ReusableBoxFuture which is always initialized and holding a future.

Solution

This turns the ReusableBoxFuture optional, and only initializes it on the first call to .poll_acquire, when it's actually needed.

The size of the semaphore doesn't change, rustc is able to exploit the niche in NonNull inside of ReusableBoxFuture, but this does introduce an additional branch, so not sure this optimization is worth it in the end.

If desired I can file a similar PR for PollSender.

@Darksonn Darksonn added A-tokio-util Area: The tokio-util crate M-sync Module: tokio/sync labels Mar 22, 2021
Copy link
Contributor

@Darksonn Darksonn left a comment

Choose a reason for hiding this comment

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

Thank you for the PR.

tokio-util/src/sync/poll_semaphore.rs Outdated Show resolved Hide resolved
permit_fut: ReusableBoxFuture<Result<OwnedSemaphorePermit, AcquireError>>,
permit_fut: Option<ReusableBoxFuture<Result<OwnedSemaphorePermit, AcquireError>>>,
Copy link
Contributor

Choose a reason for hiding this comment

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

Another alternative that sometimes works is to initialize the ReusableBoxFuture with a zero-sized future, which also would not allocate. But it probably doesn't help much in this specific case.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yup! But that wouldn't let us fast-path .try_acquire anymore without an explicit flag, would it?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, I don't think it would help here, but it could be done for PollSender, and maybe also the watch/broadcast wrappers in tokio-stream.

@NeoLegends
Copy link
Contributor Author

Thank you for the feedback ❤️

I added another commit adding a fast-path for when a permit is immediately available.

Co-authored-by: Alice Ryhl <alice@ryhl.io>
@Darksonn
Copy link
Contributor

Thank you for the PR. 😃

@Darksonn
Copy link
Contributor

If desired I can file a similar PR for PollSender.

Such a PR would be accepted. The same is also the case for the broadcast and watch wrappers in tokio-stream.

@Darksonn Darksonn merged commit 227b3e0 into tokio-rs:master Mar 22, 2021
@NeoLegends
Copy link
Contributor Author

🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-tokio-util Area: The tokio-util crate M-sync Module: tokio/sync
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants