-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
sync: Add async mpsc::Sender::shared_send method #2041
Conversation
This send variant creates a new permit on-the-fly to avoid taking a unique reference. It's useful for instances where the Sender is embedded into a larger struct which itself is shared across an application. The alternative is to clone the entire sender, which would involve cloning the inner `Arc<Char<T, S>>` nedlessly. Using `shared_send` instead does the minimal amount of work possible with the current algorithm.
The overhead should be the same imo, the only difference is that the new permit for each call to |
Creating a permit has low, but not zero cost. Struct local permit is the most efficient when we have mutable access to the sender. Cloning the inner |
IMO, in its current implementation, it's pretty much zero cost, and the overhead is negligible. The compiled assembly code reflects this: https://godbolt.org/z/-X8vSP .
How so? It has the same efficiency, with or without mutable access to the sender (In case of a
Yes, that's exactly what I am proposing, by actually using a |
I'm not following. The permit is currently struct local (as in the |
@udoprog Ups, sorry. I meant method local permit. (I've fixed my comment). |
Closing due to inactivity. |
Add async mpsc::Sender::shared_send method which takes a shared reference instead of a mutable reference.
Motivation
I've frequently found a need to use a Sender through a shared reference, and I decided to try and grok the code to find out why that is the case.
So we require unique access because the
Sender
embeds the permit used. And with the current Semaphore API, we need to provide a mutable permit for it to work with.Solution
This send variant creates a new permit on-the-fly to avoid taking a unique reference.
The alternative is to clone the entire sender, which would involve cloning the inner
Arc<Char<T, S>>
nedlessly. Usingshared_send
instead does the minimal amount of work possible with the current algorithm.There's some unfortunate leakage in
Chat
that had to be introducedpub(crate)
. But I'm not sure how to do this in a cleaner fashion. Suggestions are welcome, and feel free to bikeshed the name.