-
Notifications
You must be signed in to change notification settings - Fork 407
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
Go Optimizations 101 #214
Comments
It is true for Go toolchain 1.17, the reason might be But here, the example is intended to explain value copy costs, so I don't want to make it more complicated. |
It looks there are some weirdness in the code inline module of the standard Go compiler v1.18. (for 1.17, with
(for 1.17, without
(for 1.18 beta 2, with
(for 1.18 beta 2, without
The results for 1.17 are expected. |
The simplified benchmark code: package copycost
import "testing"
const N = 1024
func Sum_RangeSliceIdx_Inline(a []int) (r int) {
for i := range a {
r += a[i]
}
return
}
//go:noinline
func Sum_RangeSliceIdx_NoInline(a []int) (r int) {
for i := range a {
r += a[i]
}
return
}
func buildArray() [N]int {
var a [N]int
for i := 0; i < N; i++ {
a[i] = (N - i) & i
}
return a
}
var r [128]int
func Benchmark_Sum_RangeSliceIdx_Inline(b *testing.B) {
var a = buildArray()
b.ResetTimer()
for i := 0; i < b.N; i++ {
r[i&127] = Sum_RangeSliceIdx_Inline(a[:])
}
}
func Benchmark_Sum_RangeSliceIdx_NoInline(b *testing.B) {
var a = buildArray()
b.ResetTimer()
for i := 0; i < b.N; i++ {
r[i&127] = Sum_RangeSliceIdx_NoInline(a[:])
}
} The benchmark results:
|
I submitted an issue here: golang/go#51028 |
2.7 Value copy scenarios
Example 1
This example is more efficient
func Sum_RangeSliceIdx(a []int) (r int) { for i := range a { r += a[i] } return }
The text was updated successfully, but these errors were encountered: