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

Open
dsnet opened this issue Oct 26, 2016 · 0 comments
Open

cmd/compile: optimize functions into boolean expressions #17622

dsnet opened this issue Oct 26, 2016 · 0 comments
Labels
compiler/runtime Issues related to the Go compiler and/or runtime. Performance
Milestone

Comments

@dsnet
Copy link
Member

dsnet commented Oct 26, 2016

Using 587b803

In http://golang.org/cl/32122, 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: https://play.golang.org/p/KMlv43EVQn
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

@dsnet dsnet added this to the Unplanned milestone Oct 26, 2016
@gopherbot gopherbot added the compiler/runtime Issues related to the Go compiler and/or runtime. label Jul 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler/runtime Issues related to the Go compiler and/or runtime. Performance
Projects
None yet
Development

No branches or pull requests

2 participants