Skip to content

Commit

Permalink
Threads waiting for data go to the front of the queue
Browse files Browse the repository at this point in the history
Rather than using a round-robin approach for all threads waiting on the `recv`, prioritize the threads that were most recently active to pick up an item next.

Threads that have recently done some work have a higher chance of remaining in the cache. This approach is especially useful in scenarios where there are many more threads waiting to receive data than there are CPUs. Only when the system becomes backed up will threads further down the pipeline be awoken.

Imagine a scenario where we have 10 threads receiving on the channel, but usually there is only 1 item in the queue, so a single thread is able to process it all without more items accumulating. With this patch while the system is in this semi-idle state it will be always same thread, giving a slightly higher chance of its stack being still in cache.
  • Loading branch information
fulara authored Jun 24, 2024
1 parent bb5623b commit 7496e5b
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -595,7 +595,7 @@ impl<T> Shared<T> {
Err(TryRecvTimeoutError::Disconnected).into()
} else if should_block {
let hook = make_signal();
chan.waiting.push_back(hook.clone());
chan.waiting.push_front(hook.clone());
drop(chan);

do_block(hook)
Expand Down

0 comments on commit 7496e5b

Please sign in to comment.