reflect: optimize Value.IsZero for struct types

For some types where the zero value is a value where all bits of this type are 0 optimize it.

goos: windows
goarch: amd64
pkg: reflect
cpu: AMD Ryzen 7 7840HS w/ Radeon 780M Graphics
                         │   old.txt    │               new.txt               │
                         │    sec/op    │   sec/op     vs base                │
IsZero/StructInt_512-16   109.75n ± 0%   72.61n ± 1%  -33.84% (p=0.000 n=12)

Change-Id: I56de8b95f4d4482068960d6f38938763fa1caa90
GitHub-Last-Rev: c143f0cd76
GitHub-Pull-Request: golang/go#64220
Reviewed-on: https://go-review.googlesource.com/c/go/+/543355
Reviewed-by: David Chase <drchase@google.com>
Reviewed-by: Cherry Mui <cherryyz@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Auto-Submit: Keith Randall <khr@golang.org>
Reviewed-by: Keith Randall <khr@golang.org>
This commit is contained in:
qiulaidongfeng 2023-11-19 11:22:05 +00:00 committed by Gopher Robot
parent 2551fffd2c
commit ada5c2edb4
3 changed files with 39 additions and 12 deletions

View file

@ -1500,6 +1500,12 @@ func TestIsZero(t *testing.T) {
{setField(struct{ _, a, _ func() }{}, 0*unsafe.Sizeof((func())(nil)), func() {}), true},
{setField(struct{ _, a, _ func() }{}, 1*unsafe.Sizeof((func())(nil)), func() {}), false},
{setField(struct{ _, a, _ func() }{}, 2*unsafe.Sizeof((func())(nil)), func() {}), true},
{struct{ a [256]S }{}, true},
{struct{ a [256]S }{a: [256]S{2: {i1: 1}}}, false},
{struct{ a [256]float32 }{}, true},
{struct{ a [256]float32 }{a: [256]float32{2: 1.0}}, false},
{struct{ _, a [256]S }{}, true},
{setField(struct{ _, a [256]S }{}, 0*unsafe.Sizeof(int64(0)), int64(1)), true},
// UnsafePointer
{(unsafe.Pointer)(nil), true},
{(unsafe.Pointer)(new(int)), false},
@ -1541,7 +1547,7 @@ func TestIsZero(t *testing.T) {
func TestInternalIsZero(t *testing.T) {
b := make([]byte, 512)
for a := 0; a < 8; a++ {
for i := 256 + 7; i <= 512-a; i++ {
for i := 1; i <= 512-a; i++ {
InternalIsZero(b[a : a+i])
}
}