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: const cause the speed down #11228

Closed
chai2010 opened this issue Jun 16, 2015 · 4 comments
Closed

cmd/compile: const cause the speed down #11228

chai2010 opened this issue Jun 16, 2015 · 4 comments

Comments

@chai2010
Copy link
Contributor

go version devel +f74ea6c Tue Jun 16 01:04:10 2015 +0000 windows/amd64
func funA(a0, a1, b0, b1 uint32) uint32 {
    return ((a0&0x12345678)>>2 + (a1&0x12345678)>>3 + (b0&0x12345678)>>4 + (b1&0x12345678)>>5) +
        ((((a0 & 0x87654321) + (a1 & 0x87654321) + (b0 & 0x87654321) + (b1 & 0x87654321) + 0) >> 7) & 0x87654321)
}

func funB(a0, a1, b0, b1 uint32) uint32 {
    const mask0, mask1 = 0x12345678, 0x87654321
    return ((a0&mask0)>>2 + (a1&mask0)>>3 + (b0&mask0)>>4 + (b1&mask0)>>5) +
        ((((a0 & mask1) + (a1 & mask1) + (b0 & mask1) + (b1 & mask1) + 0) >> 7) & mask1)
}

func BenchmarkA(b *testing.B) {
    for i := 0; i < b.N; i++ {
        for i := 0; i < (1 << 20); i++ {
            funA(1, 2, 3, 4)
        }
    }
}

func BenchmarkB(b *testing.B) {
    for i := 0; i < b.N; i++ {
        for i := 0; i < (1 << 20); i++ {
            funB(1, 2, 3, 4)
        }
    }
}
BenchmarkA-4         500           3828382 ns/op
BenchmarkB-4         300           5467213 ns/op
@josharian
Copy link
Contributor

Possible dup of #7655. If you run with -gcflags="-l", does the performance difference go away?

@chai2010
Copy link
Contributor Author

go test -bench=. -gcflags="-l"
BenchmarkA-4         300           5407207 ns/op
BenchmarkB-4         300           5417208 ns/op

@randall77
Copy link
Contributor

Yes, if you look at the disassembly funA gets inlined into BenchmarkA and funB does not get inlined into BenchmarkB. Our inlining heuristic doesn't weigh these two bodies equally, unfortunately.

@josharian
Copy link
Contributor

Thanks. Closing as duplicate.

@mikioh mikioh changed the title gc: const cause the speed down cmd/compile: const cause the speed down Jun 16, 2015
@golang golang locked and limited conversation to collaborators Jun 25, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants