2
0
Fork 0
mirror of https://git.asonix.dog/asonix/pict-rs synced 2024-12-22 11:21:24 +00:00

Improve cooperation from from_iterator to prevent task starvation

This commit is contained in:
asonix 2024-01-05 19:50:10 -06:00
parent 4145637a33
commit bcf73eb4e4

View file

@ -76,10 +76,22 @@ where
streem::from_fn(|yielder| async move {
let mut stream = rx.into_stream().into_streamer();
let yield_count = buffer.max(8);
let mut count = 0;
while let Some(res) = stream.next().await {
tracing::trace!("from_iterator: looping");
count += 1;
count = count % yield_count;
yielder.yield_(res).await;
// every 8 (or buffer-size) items, yield to executor before looping
// improves cooperation
if count == 0 {
tokio::task::yield_now().await;
}
}
let _ = handle.await;