runtime: add fast paths for common select
patterns
#64900
Labels
compiler/runtime
Issues related to the Go compiler and/or runtime.
NeedsInvestigation
Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Performance
Milestone
This select:
should, ideally, be as fast as this one:
This is currently not the case though, with the latter being over 2 orders of magnitude faster under contention, and almost 1 with no contention.
In
selectgo
there is a (pretty old) comment that claims that the code in the first example is "rare enough" that it is not worth optimizing for. I would argue that at least in code I write and maintain, a two-casesselect
with one of the cases being<-ctx.Done()
is pretty common. This would be especially beneficial in libraries, where the library normally has no control over whichContext
it is passed by the host application, and where the compiler would likely be unable to statically determine that the channel is nil.AFAICT, to address this,
selectgo
could - after counting how many non-nil cases exist - dispatch directly tochansend
/chanrecv
if there is only one non-nil case. This would be somewhat similar to what the compiler does for simple cases, but done dynamically instead of statically.update: I actually tried it out, and to achieve the speedup it is also going to require extending the fast paths in
chanrecv
/chansend
, but the results are promising:The text was updated successfully, but these errors were encountered: