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

Change to dynamic select #4

Merged
merged 1 commit into from
Mar 22, 2022
Merged

Change to dynamic select #4

merged 1 commit into from
Mar 22, 2022

Conversation

ghjm
Copy link
Contributor

@ghjm ghjm commented Mar 21, 2022

Description

This PR changes the implementation of Distribute() to use a dynamically generated select via the reflect package, instead of randomly choosing a single channel to send to.

In the case where all "out" channels are available to receive a message, the behavior is the same: the channel is selected pseudo-randomly. However, the pseudo-random choice is made inside the Go library's select implementation, which is presumably highly optimized for this purpose. Benchmark_Distribute-8 performance is roughly doubled on my system (831 ns/op after the change vs. 1582 ns/op before).

In the case where some of the "out" channels are not able to receive a message right now, the select call will immediately choose one of the available channels. If Distribute() is being used to send units of work to a pool of workers, we avoid the case where some workers sit idle because we happened to choose a busy worker to receive the next unit.

No changes are made to the library API and this should be fully backwards compatible with all library users, unless they rely on the exact details of the channel selection (and it's hard to see how one would do this).

@benjivesterby benjivesterby self-assigned this Mar 21, 2022
@codecov
Copy link

codecov bot commented Mar 21, 2022

Codecov Report

Merging #4 (31dc36e) into main (5bb97f1) will not change coverage.
The diff coverage is 100.00%.

@@            Coverage Diff            @@
##              main        #4   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files            3         3           
  Lines          206       207    +1     
=========================================
+ Hits           206       207    +1     
Impacted Files Coverage Δ
stream.go 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 5bb97f1...31dc36e. Read the comment docs.

Copy link
Member

@benjivesterby benjivesterby left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ghjm Thank you so much for this! I didn't know about this method before.

@benjivesterby benjivesterby merged commit f46eee3 into devnw:main Mar 22, 2022
@benjivesterby
Copy link
Member

Benchmark update from my local test:

name            old time/op  new time/op  delta
_Distribute-16  2.86µs ± 2%  1.71µs ± 0%   ~     (p=0.400 n=4+1)

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