-
Notifications
You must be signed in to change notification settings - Fork 17.7k
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
cmd/compile: if x == a || x == b || x == c generates slower code than case a, b, c #19791
Comments
I seem to recall a bug open about this already, or something similar. @randall77 probably knows. |
I don't know of any related issue. |
We could do something during walk, where we look at disjunctions of equality checks, look for constants on one side and samesafeexprs on the other, and rewrite them into switch statements. I'm not convinced it's worth the code, since the benefit only kicks in when there are several disjuncts, and the author can simply use a switch statement instead. |
There is #17622 about compiling switches into boolean functions. |
There is #5496 about compiling dense integer switches into jump tables. But that doesn't touch the fact that the if is slower than the switch. I have a lexer/parser that has a lot of branching depending on runes/bytes, so I'm interested in this. I have come up with some benchmarks, also including a hand-made table: https://play.golang.org/p/QHwxgi0pBLn
Like in this issue, I see the switch being noticeably faster than the if. Perhaps unsurprisingly, the table is in turn noticeably faster than the switch. The fun part, however, is that swapping a |
Your speed is probably very dependent on the order of comparisons and the likely input values. If your first Without information about the distribution of the input values, it is hard to make an always-correct decision in the optimizer. (That said, a jump table is likely a good idea in most cases.) |
Thank you - this makes sense. Seems like I should pay closer attention to the jump table issue rather than this one. |
Let's close this in favor of the jump table issue (#5496). |
go version devel +214be5b302 Sun Mar 26 04:40:20 2017 +0000 linux/amd64
While investigating issue 19789 I came across code that rans 25% slower than I expected. Context with runnable benchmark: https://gist.github.com/gaal/497361737dd55125cf2de998b49d948b
Please compare:
It looks like the compiler could figure out that the expression is equivalent and could be optimized the same way. The speedup probably depends greatly on the number and kinds of arguments in the disjunction.
The text was updated successfully, but these errors were encountered: