mirror of
https://github.com/golang/go
synced 2024-11-15 03:50:33 -07:00
c5ba9d2232
This patch optimizes reflectdata.geneq to pick apart structs in array
equality and prioritize non-CALL comparisons over those which involve
a runtime function call. This is similar to how arrays of strings
operate currently. Instead of looping over the entire array of structs
once, if there are any comparisons which involve a runtime function
call we instead loop twice. The first loop is all simple, quick
comparisons. If no inequality is found in the first loop the second loop
calls runtime functions for larger memory comparison, which is more
expensive.
For the benchmarks added in this change:
Old:
```
goos: linux
goarch: amd64
pkg: cmd/compile/internal/reflectdata
cpu: AMD Ryzen 9 3950X 16-Core Processor
BenchmarkEqArrayOfStructsEq
BenchmarkEqArrayOfStructsEq-32 797196 1497 ns/op
BenchmarkEqArrayOfStructsEq-32 758332 1581 ns/op
BenchmarkEqArrayOfStructsEq-32 764871 1599 ns/op
BenchmarkEqArrayOfStructsEq-32 760706 1558 ns/op
BenchmarkEqArrayOfStructsEq-32 763112 1476 ns/op
BenchmarkEqArrayOfStructsEq-32 747696 1547 ns/op
BenchmarkEqArrayOfStructsEq-32 756526 1562 ns/op
BenchmarkEqArrayOfStructsEq-32 768829 1486 ns/op
BenchmarkEqArrayOfStructsEq-32 764248 1477 ns/op
BenchmarkEqArrayOfStructsEq-32 752767 1545 ns/op
BenchmarkEqArrayOfStructsNotEq
BenchmarkEqArrayOfStructsNotEq-32 757194 1542 ns/op
BenchmarkEqArrayOfStructsNotEq-32 748942 1552 ns/op
BenchmarkEqArrayOfStructsNotEq-32 766687 1554 ns/op
BenchmarkEqArrayOfStructsNotEq-32 732069 1541 ns/op
BenchmarkEqArrayOfStructsNotEq-32 759163 1576 ns/op
BenchmarkEqArrayOfStructsNotEq-32 796402 1629 ns/op
BenchmarkEqArrayOfStructsNotEq-32 726610 1570 ns/op
BenchmarkEqArrayOfStructsNotEq-32 735770 1584 ns/op
BenchmarkEqArrayOfStructsNotEq-32 745255 1610 ns/op
BenchmarkEqArrayOfStructsNotEq-32 743872 1591 ns/op
PASS
ok cmd/compile/internal/reflectdata 35.446s
```
New:
```
goos: linux
goarch: amd64
pkg: cmd/compile/internal/reflectdata
cpu: AMD Ryzen 9 3950X 16-Core Processor
BenchmarkEqArrayOfStructsEq
BenchmarkEqArrayOfStructsEq-32 618379 1827 ns/op
BenchmarkEqArrayOfStructsEq-32 619368 1922 ns/op
BenchmarkEqArrayOfStructsEq-32 616023 1910 ns/op
BenchmarkEqArrayOfStructsEq-32 617575 1905 ns/op
BenchmarkEqArrayOfStructsEq-32 610399 1889 ns/op
BenchmarkEqArrayOfStructsEq-32 615378 1823 ns/op
BenchmarkEqArrayOfStructsEq-32 613732 1883 ns/op
BenchmarkEqArrayOfStructsEq-32 613924 1894 ns/op
BenchmarkEqArrayOfStructsEq-32 657799 1876 ns/op
BenchmarkEqArrayOfStructsEq-32 665580 1873 ns/op
BenchmarkEqArrayOfStructsNotEq
BenchmarkEqArrayOfStructsNotEq-32 1834915 627.4 ns/op
BenchmarkEqArrayOfStructsNotEq-32 1806370 660.5 ns/op
BenchmarkEqArrayOfStructsNotEq-32 1828075 625.5 ns/op
BenchmarkEqArrayOfStructsNotEq-32 1819741 641.6 ns/op
BenchmarkEqArrayOfStructsNotEq-32 1813128 632.3 ns/op
BenchmarkEqArrayOfStructsNotEq-32 1865250 643.7 ns/op
BenchmarkEqArrayOfStructsNotEq-32 1828617 632.8 ns/op
BenchmarkEqArrayOfStructsNotEq-32 1862748 633.6 ns/op
BenchmarkEqArrayOfStructsNotEq-32 1825432 638.7 ns/op
BenchmarkEqArrayOfStructsNotEq-32 1804382 628.8 ns/op
PASS
ok cmd/compile/internal/reflectdata 36.571s
```
Benchstat comparison:
```
name old time/op new time/op delta
EqArrayOfStructsEq-32 1.53µs ± 4% 1.88µs ± 3% +22.66% (p=0.000 n=10+10)
EqArrayOfStructsNotEq-32 1.57µs ± 3% 0.64µs ± 4% -59.59% (p=0.000 n=10+10)
```
So, the equal case is a bit slower (unrolling the loop helps with that),
but the non-equal case is now much faster.
Change-Id: I05d776456c79c48a3d6d74b18c45246e58ffbea6
GitHub-Last-Rev: f57ee07d05
GitHub-Pull-Request: golang/go#59409
Reviewed-on: https://go-review.googlesource.com/c/go/+/481895
Auto-Submit: Dmitri Shuralyov <dmitshur@golang.org>
Reviewed-by: Keith Randall <khr@google.com>
Reviewed-by: Keith Randall <khr@golang.org>
Run-TryBot: Dmitri Shuralyov <dmitshur@golang.org>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Heschi Kreinick <heschi@google.com>
107 lines
2.3 KiB
Go
107 lines
2.3 KiB
Go
// run
|
|
|
|
// Copyright 2020 The Go Authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
// Check to make sure that we compare fields in order. See issue 8606.
|
|
|
|
package main
|
|
|
|
import "fmt"
|
|
|
|
func main() {
|
|
type A [2]interface{}
|
|
type A2 [6]interface{}
|
|
type S struct{ x, y interface{} }
|
|
type S2 struct{ x, y, z, a, b, c interface{} }
|
|
type T1 struct {
|
|
i interface{}
|
|
a int64
|
|
j interface{}
|
|
}
|
|
type T2 struct {
|
|
i interface{}
|
|
a, b, c int64
|
|
j interface{}
|
|
}
|
|
type T3 struct {
|
|
i interface{}
|
|
s string
|
|
j interface{}
|
|
}
|
|
type S3 struct {
|
|
f any
|
|
i int
|
|
}
|
|
type S4 struct {
|
|
a [1000]byte
|
|
b any
|
|
}
|
|
b := []byte{1}
|
|
s1 := S3{func() {}, 0}
|
|
s2 := S3{func() {}, 1}
|
|
|
|
for _, test := range []struct {
|
|
panic bool
|
|
a, b interface{}
|
|
}{
|
|
{false, A{1, b}, A{2, b}},
|
|
{true, A{b, 1}, A{b, 2}},
|
|
{false, A{1, b}, A{"2", b}},
|
|
{true, A{b, 1}, A{b, "2"}},
|
|
|
|
{false, A2{1, b}, A2{2, b}},
|
|
{true, A2{b, 1}, A2{b, 2}},
|
|
{false, A2{1, b}, A2{"2", b}},
|
|
{true, A2{b, 1}, A2{b, "2"}},
|
|
|
|
{false, S{1, b}, S{2, b}},
|
|
{true, S{b, 1}, S{b, 2}},
|
|
{false, S{1, b}, S{"2", b}},
|
|
{true, S{b, 1}, S{b, "2"}},
|
|
|
|
{false, S2{x: 1, y: b}, S2{x: 2, y: b}},
|
|
{true, S2{x: b, y: 1}, S2{x: b, y: 2}},
|
|
{false, S2{x: 1, y: b}, S2{x: "2", y: b}},
|
|
{true, S2{x: b, y: 1}, S2{x: b, y: "2"}},
|
|
|
|
{true, T1{i: b, a: 1}, T1{i: b, a: 2}},
|
|
{false, T1{a: 1, j: b}, T1{a: 2, j: b}},
|
|
{true, T2{i: b, a: 1}, T2{i: b, a: 2}},
|
|
{false, T2{a: 1, j: b}, T2{a: 2, j: b}},
|
|
{true, T3{i: b, s: "foo"}, T3{i: b, s: "bar"}},
|
|
{false, T3{s: "foo", j: b}, T3{s: "bar", j: b}},
|
|
{true, T3{i: b, s: "fooz"}, T3{i: b, s: "bar"}},
|
|
{false, T3{s: "fooz", j: b}, T3{s: "bar", j: b}},
|
|
{true, A{s1, s2}, A{s2, s1}},
|
|
{true, s1, s2},
|
|
{false, S4{[1000]byte{0}, func() {}}, S4{[1000]byte{1}, func() {}}},
|
|
} {
|
|
f := func() {
|
|
defer func() {
|
|
if recover() != nil {
|
|
panic(fmt.Sprintf("comparing %#v and %#v panicked", test.a, test.b))
|
|
}
|
|
}()
|
|
if test.a == test.b {
|
|
panic(fmt.Sprintf("values %#v and %#v should not be equal", test.a, test.b))
|
|
}
|
|
}
|
|
if test.panic {
|
|
shouldPanic(fmt.Sprintf("comparing %#v and %#v did not panic", test.a, test.b), f)
|
|
} else {
|
|
f() // should not panic
|
|
}
|
|
}
|
|
}
|
|
|
|
func shouldPanic(name string, f func()) {
|
|
defer func() {
|
|
if recover() == nil {
|
|
panic(name)
|
|
}
|
|
}()
|
|
f()
|
|
}
|