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

Comments

Projects
None yet
1 participant
@dsnet
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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment