1
0
mirror of https://github.com/golang/go synced 2024-11-23 06:40:05 -07:00

reflect: make Value.IsZero not escape

With CL 408826 reflect.Value not always escape. IsZero still
escapes the Value because in some cases it passes the Value
pointer to the equal function, which is function pointer. Equal
functions are compiler generated and never escapes, but the escape
analysis doesn't know. Add noescape to help.

Change-Id: Ica397c2be77cac9e8a46d03d70bac385b0aa9e82
Reviewed-on: https://go-review.googlesource.com/c/go/+/441937
Reviewed-by: David Chase <drchase@google.com>
Run-TryBot: Cherry Mui <cherryyz@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
This commit is contained in:
Cherry Mui 2022-10-07 18:11:10 -04:00
parent be4fe08b57
commit 3e19dc2b23

View File

@ -1598,7 +1598,10 @@ func (v Value) IsZero() bool {
if v.flag&flagIndir == 0 {
return v.ptr == nil
}
return v.typ().Equal(v.ptr, unsafe.Pointer(&zeroVal[0]))
// v.ptr doesn't escape, as Equal functions are compiler generated
// and never escape. The escape analysis doesn't know, as it is a
// function pointer call.
return v.typ().Equal(noescape(v.ptr), unsafe.Pointer(&zeroVal[0]))
}
n := v.Len()
@ -1618,7 +1621,8 @@ func (v Value) IsZero() bool {
if v.flag&flagIndir == 0 {
return v.ptr == nil
}
return v.typ().Equal(v.ptr, unsafe.Pointer(&zeroVal[0]))
// See noescape justification above.
return v.typ().Equal(noescape(v.ptr), unsafe.Pointer(&zeroVal[0]))
}
n := v.NumField()