mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
encoding/json: rely on reflect.Value.SetZero
v.SetZero() is faster than v.Set(reflect.Zero(v.Type())) and was recently added in Go 1.20. Benchmark numbers are largely unchanged since this mainly affects the unmarshaling of large numbers of JSON nulls, which our benchmarks do not heavily exercise. Change-Id: I464f60f63c9027e63a99fd5da92e7ab782018329 Reviewed-on: https://go-review.googlesource.com/c/go/+/471195 Reviewed-by: Johan Brandhorst-Satzkorn <johan.brandhorst@gmail.com> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Than McIntosh <thanm@google.com> Run-TryBot: Joseph Tsai <joetsai@digital-static.net> Reviewed-by: Dmitri Shuralyov <dmitshur@google.com> Auto-Submit: Joseph Tsai <joetsai@digital-static.net> Reviewed-by: Daniel Martí <mvdan@mvdan.cc>
This commit is contained in:
parent
8367e2dfc7
commit
cb3e170ae7
1 changed files with 4 additions and 6 deletions
|
|
@ -577,13 +577,11 @@ func (d *decodeState) array(v reflect.Value) error {
|
|||
|
||||
if i < v.Len() {
|
||||
if v.Kind() == reflect.Array {
|
||||
// Array. Zero the rest.
|
||||
z := reflect.Zero(v.Type().Elem())
|
||||
for ; i < v.Len(); i++ {
|
||||
v.Index(i).Set(z)
|
||||
v.Index(i).SetZero() // zero remainder of array
|
||||
}
|
||||
} else {
|
||||
v.SetLen(i)
|
||||
v.SetLen(i) // truncate the slice
|
||||
}
|
||||
}
|
||||
if i == 0 && v.Kind() == reflect.Slice {
|
||||
|
|
@ -688,7 +686,7 @@ func (d *decodeState) object(v reflect.Value) error {
|
|||
if !mapElem.IsValid() {
|
||||
mapElem = reflect.New(elemType).Elem()
|
||||
} else {
|
||||
mapElem.Set(reflect.Zero(elemType))
|
||||
mapElem.SetZero()
|
||||
}
|
||||
subv = mapElem
|
||||
} else {
|
||||
|
|
@ -902,7 +900,7 @@ func (d *decodeState) literalStore(item []byte, v reflect.Value, fromQuoted bool
|
|||
}
|
||||
switch v.Kind() {
|
||||
case reflect.Interface, reflect.Pointer, reflect.Map, reflect.Slice:
|
||||
v.Set(reflect.Zero(v.Type()))
|
||||
v.SetZero()
|
||||
// otherwise, ignore null for primitives/string
|
||||
}
|
||||
case 't', 'f': // true, false
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue