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

cmd/compile: optimize functions into boolean expressions #17622

dsnet opened this issue Oct 26, 2016 · 0 comments

cmd/compile: optimize functions into boolean expressions #17622

dsnet opened this issue Oct 26, 2016 · 0 comments


Copy link

@dsnet dsnet commented Oct 26, 2016

Using 587b803

In, I optimized a switch-based implementation to be a boolean expression. The two implementations below are semantically equivalent:

func ValidRune1(r rune) bool {
    switch {
    case r < 0:
        return false
    case surrogateMin <= r && r <= surrogateMax:
        return false
    case r > MaxRune:
        return false
    return true

func ValidRune2(r rune) bool {
    return (0 <= r && r < surrogateMin) || (surrogateMax < r && r <= MaxRune)

Testable benchmark:
The benchmark results:

benchmark        old ns/op     new ns/op     delta
BenchmarkA-4     16.3          11.9          -26.99%

We could consider having the compiler recognize the pattern of switch (with boolean expression as cases) and all return values are either boolean literals or expressions and have the compiler automatically optimize that into a single larger boolean expression.

\cc @bradfitz @minux @martisch

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
1 participant
You can’t perform that action at this time.