Skip to content

Commit 8925290

Browse files
committed
reflect: use zero buffer to back the Value returned by Zero
In the common case (<1KB types), no allocation is required by reflect.Zero. Also use memclr instead of memmove in Set when the source is known to be zero. Fixes #33136 Change-Id: Ic66871930fbb53328032e587153ebd12995ccf55 Reviewed-on: https://go-review.googlesource.com/c/go/+/192331 Trust: Keith Randall <[email protected]> Run-TryBot: Keith Randall <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Martin Möhrmann <[email protected]>
1 parent 7e54aa2 commit 8925290

File tree

3 files changed

+48
-3
lines changed

3 files changed

+48
-3
lines changed

src/reflect/all_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6006,6 +6006,14 @@ func TestReflectMethodTraceback(t *testing.T) {
60066006
}
60076007
}
60086008

6009+
func TestSmallZero(t *testing.T) {
6010+
type T [10]byte
6011+
typ := TypeOf(T{})
6012+
if allocs := testing.AllocsPerRun(100, func() { Zero(typ) }); allocs > 0 {
6013+
t.Errorf("Creating small zero values caused %f allocs, want 0", allocs)
6014+
}
6015+
}
6016+
60096017
func TestBigZero(t *testing.T) {
60106018
const size = 1 << 10
60116019
var v [size]byte
@@ -6017,6 +6025,27 @@ func TestBigZero(t *testing.T) {
60176025
}
60186026
}
60196027

6028+
func TestZeroSet(t *testing.T) {
6029+
type T [16]byte
6030+
type S struct {
6031+
a uint64
6032+
T T
6033+
b uint64
6034+
}
6035+
v := S{
6036+
a: 0xaaaaaaaaaaaaaaaa,
6037+
T: T{9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9},
6038+
b: 0xbbbbbbbbbbbbbbbb,
6039+
}
6040+
ValueOf(&v).Elem().Field(1).Set(Zero(TypeOf(T{})))
6041+
if v != (S{
6042+
a: 0xaaaaaaaaaaaaaaaa,
6043+
b: 0xbbbbbbbbbbbbbbbb,
6044+
}) {
6045+
t.Fatalf("Setting a field to a Zero value didn't work")
6046+
}
6047+
}
6048+
60206049
func TestFieldByIndexNil(t *testing.T) {
60216050
type P struct {
60226051
F int

src/reflect/value.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1553,7 +1553,11 @@ func (v Value) Set(x Value) {
15531553
}
15541554
x = x.assignTo("reflect.Set", v.typ, target)
15551555
if x.flag&flagIndir != 0 {
1556-
typedmemmove(v.typ, v.ptr, x.ptr)
1556+
if x.ptr == unsafe.Pointer(&zeroVal[0]) {
1557+
typedmemclr(v.typ, v.ptr)
1558+
} else {
1559+
typedmemmove(v.typ, v.ptr, x.ptr)
1560+
}
15571561
} else {
15581562
*(*unsafe.Pointer)(v.ptr) = x.ptr
15591563
}
@@ -2360,11 +2364,23 @@ func Zero(typ Type) Value {
23602364
t := typ.(*rtype)
23612365
fl := flag(t.Kind())
23622366
if ifaceIndir(t) {
2363-
return Value{t, unsafe_New(t), fl | flagIndir}
2367+
var p unsafe.Pointer
2368+
if t.size <= maxZero {
2369+
p = unsafe.Pointer(&zeroVal[0])
2370+
} else {
2371+
p = unsafe_New(t)
2372+
}
2373+
return Value{t, p, fl | flagIndir}
23642374
}
23652375
return Value{t, nil, fl}
23662376
}
23672377

2378+
// must match declarations in runtime/map.go.
2379+
const maxZero = 1024
2380+
2381+
//go:linkname zeroVal runtime.zeroVal
2382+
var zeroVal [maxZero]byte
2383+
23682384
// New returns a Value representing a pointer to a new zero value
23692385
// for the specified type. That is, the returned Value's Type is PtrTo(typ).
23702386
func New(typ Type) Value {

src/runtime/map.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1380,5 +1380,5 @@ func reflectlite_maplen(h *hmap) int {
13801380
return h.count
13811381
}
13821382

1383-
const maxZero = 1024 // must match value in cmd/compile/internal/gc/walk.go:zeroValSize
1383+
const maxZero = 1024 // must match value in reflect/value.go:maxZero cmd/compile/internal/gc/walk.go:zeroValSize
13841384
var zeroVal [maxZero]byte

0 commit comments

Comments
 (0)