Skip to content
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

Threads waiting for data go to the front of the queue #151

Closed
wants to merge 1 commit into from

Conversation

fulara
Copy link

@fulara fulara commented Jun 24, 2024

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.

This addresses #150

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.
@zesterer
Copy link
Owner

I'd like to think through the implications of this, I'm worried about heavily biasing toward a specific thread, starving others of items. Perhaps this is fine if that one thread is capable of servicing senders in appropriate time though. Do you happen to know whether there's prior work on this for other implementations?

@fulara
Copy link
Author

fulara commented Jun 27, 2024

Yes, someone else pointed out this issue too, that this might cause starvation of other workers.

This change is fine as long as all the workers have exactly same way to handle the calls.
I am going to experiment with it locally, but I don't think it is a good candidate to merge here.

Maybe as a separate API it would be decent, but I don't want to go that far.

@fulara fulara closed this Jun 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants