tokio-rs / tokio

A runtime for writing reliable asynchronous applications with Rust. Provides I/O, networking, scheduling, timers, ...
https://tokio.rs
MIT License
25.44k stars 2.3k forks source link

mpsc::Receiver::try_recv_many #6670

Closed barafael closed 2 days ago

barafael commented 6 days ago

Problem

I want to receive data from an mpsc within the sync world. I want to be able to receive multiple items if available for faster processing and less context switching.

Solution

mpsc::Receiver::try_recv bridges the async-sync gap. It would be great if it had a variant similar to async recv_many.

Alternatives

It would be possible to keep calling try_recv in a loop until it fails and push the data into a vector manually. This would at least incur the cost of possibly growing that vector.

wathenjiang commented 4 days ago

It seems that unless recv_many is optimized internally, the performance difference between providing this try_recv_many and calling try_recv in a loop will not be particularly large.

Please see:

barafael commented 2 days ago

thanks for clarifying!