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: combine contiguous multi-byte comparisons to a constant #17106

Open
josharian opened this issue Sep 14, 2016 · 0 comments
Open

cmd/compile: combine contiguous multi-byte comparisons to a constant #17106

josharian opened this issue Sep 14, 2016 · 0 comments
Labels
compiler/runtime Issues related to the Go compiler and/or runtime. Performance
Milestone

Comments

@josharian
Copy link
Contributor

On architectures that allow unaligned loads, we should rewrite b[0] == c1 && b[1] == c2 to *(*uint16)(&b[0]) == (c1 << 8) + c2. And do all architectures for cases in which we know b[0] is appropriately aligned. Also uint32 and uint64 (as appropriate).

See CL 26758 for more discussion; that CL will cause many of these to be generated.

We might also independently want to update the front end (near OCMPSTR) to generate the larger comparisons directly.

package p

import "testing"

var (
    sink   bool
    b1, b2 byte
)

func Benchmark8(b *testing.B) {
    for i := 0; i < b.N; i++ {
        sink = b1 == 5 && b2 == 9
    }
}

func Benchmark16(b *testing.B) {
    u16 := uint16(b1<<8) + uint16(b2)
    const c16 = (5 << 8) + 9
    for i := 0; i < b.N; i++ {
        sink = u16 == c16
    }
}
Benchmark8-8                2000000000           0.99 ns/op
Benchmark16-8               2000000000           0.68 ns/op
@josharian josharian added this to the Unplanned milestone Sep 14, 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