Skip to content

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.

pub struct PollSemaphore {
semaphore: Arc<Semaphore>,
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 NeoLegends force-pushed the feat/optimize-poll-allocation branch from c99fd36 to 4cd3c57 Compare March 22, 2021 14:44
@NeoLegends NeoLegends requested a review from Darksonn March 22, 2021 14:44
@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.

@NeoLegends NeoLegends requested a review from Darksonn March 22, 2021 15:19
@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.

2 participants