From: Daniel Martí Date: Sun, 22 Jul 2018 11:36:15 +0000 (+0100) Subject: encoding/json: inline fieldByIndex X-Git-Tag: go1.12beta1~1347 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=75e7e05aee4c0588a11f79bb5c46290ca753bf20;p=gostls13.git encoding/json: inline fieldByIndex This function was only used in a single place - in the field encoding loop within the struct encoder. Inlining the function call manually lets us get rid of the call overhead. But most importantly, it lets us simplify the logic afterward. We no longer need to use reflect.Value{} and !fv.IsValid(), as we can skip the field immediately. The two factors combined (mostly just the latter) give a moderate speed improvement to this hot loop. name old time/op new time/op delta CodeEncoder-4 6.01ms ± 1% 5.91ms ± 1% -1.66% (p=0.002 n=6+6) name old speed new speed delta CodeEncoder-4 323MB/s ± 1% 328MB/s ± 1% +1.69% (p=0.002 n=6+6) Updates #5683. Change-Id: I12757c325a68abb2856026cf719c122612a1f38e Reviewed-on: https://go-review.googlesource.com/125417 Run-TryBot: Daniel Martí Reviewed-by: Brad Fitzpatrick TryBot-Result: Gobot Gobot --- diff --git a/src/encoding/json/encode.go b/src/encoding/json/encode.go index 40bc060644..bb4c54e8d6 100644 --- a/src/encoding/json/encode.go +++ b/src/encoding/json/encode.go @@ -632,8 +632,21 @@ func (se structEncoder) encode(e *encodeState, v reflect.Value, opts encOpts) { first := true for i := range se.fields { f := &se.fields[i] - fv := fieldByIndex(v, f.index) - if !fv.IsValid() || f.omitEmpty && isEmptyValue(fv) { + + // Find the nested struct field by following f.index. + fv := v + FieldLoop: + for _, i := range f.index { + if fv.Kind() == reflect.Ptr { + if fv.IsNil() { + continue FieldLoop + } + fv = fv.Elem() + } + fv = fv.Field(i) + } + + if f.omitEmpty && isEmptyValue(fv) { continue } if first { @@ -835,19 +848,6 @@ func isValidTag(s string) bool { return true } -func fieldByIndex(v reflect.Value, index []int) reflect.Value { - for _, i := range index { - if v.Kind() == reflect.Ptr { - if v.IsNil() { - return reflect.Value{} - } - v = v.Elem() - } - v = v.Field(i) - } - return v -} - func typeByIndex(t reflect.Type, index []int) reflect.Type { for _, i := range index { if t.Kind() == reflect.Ptr {