tokio icon indicating copy to clipboard operation
tokio copied to clipboard

Add tokio::sync::mpsc::UnboundedSender::send_many to improve the case of sending many values to an unbounded channel at once.

Open FSMaxB opened this issue 2 years ago • 2 comments

Currently mpsc::UnboundedSender don't allow sending more than one value at once, so when sending multiple values this needs to be done manually by calling it multiple times.

Wouldn't it be possible to add a method that takes an Iterator and performs the internal atomic operations to reserve space only once using the size_hint when available (at least if no new blocks need to be added and the size hint is reliable).

fn send_many<Iter>(&self, messages: Iter) -> Result<(), SendError<T>>
where
	Iter: Iterator<Item = T>
{
}

Of course this would need to be careful because the size_hint may not be trustworthy.

I'm not sure if that feature would make sense for bounded channels as well since the potentially empty slots from allocating to much in advance would reduce the channels capacity temporarily.

FSMaxB avatar Apr 28 '22 11:04 FSMaxB

The bound could also require ExactSizeIterator.

FSMaxB avatar Apr 28 '22 11:04 FSMaxB

It's probably possible to implement this. Question is if we want to. Thoughts @carllerche?

Darksonn avatar Apr 28 '22 13:04 Darksonn