Skip to content

Commit

Permalink
encoding/json: avoid some more pointer receivers
Browse files Browse the repository at this point in the history
A few encoder struct types, such as map and slice, only encapsulate
other prepared encoder funcs. Using pointer receivers has no advantage,
and makes calling these methods slightly more expensive.

Not a huge performance win, but certainly an easy one. The struct types
used in the benchmark below contain one slice field and one pointer
field.

name           old time/op    new time/op    delta
CodeEncoder-4    5.48ms ± 0%    5.39ms ± 0%  -1.66%  (p=0.010 n=6+4)

name           old speed      new speed      delta
CodeEncoder-4   354MB/s ± 0%   360MB/s ± 0%  +1.69%  (p=0.010 n=6+4)

Updates #5683.

Change-Id: I9f78dbe07fcc6fbf19a6d96c22f5d6970db9eca4
Reviewed-on: https://go-review.googlesource.com/131400
Run-TryBot: Daniel Martí <mvdan@mvdan.cc>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
  • Loading branch information
mvdan committed Aug 25, 2018
1 parent f7e4d91 commit b659283
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -674,7 +674,7 @@ type mapEncoder struct {
elemEnc encoderFunc
}

func (me *mapEncoder) encode(e *encodeState, v reflect.Value, opts encOpts) {
func (me mapEncoder) encode(e *encodeState, v reflect.Value, opts encOpts) {
if v.IsNil() {
e.WriteString("null")
return
Expand Down Expand Up @@ -713,7 +713,7 @@ func newMapEncoder(t reflect.Type) encoderFunc {
return unsupportedTypeEncoder
}
}
me := &mapEncoder{typeEncoder(t.Elem())}
me := mapEncoder{typeEncoder(t.Elem())}
return me.encode
}

Expand Down Expand Up @@ -752,7 +752,7 @@ type sliceEncoder struct {
arrayEnc encoderFunc
}

func (se *sliceEncoder) encode(e *encodeState, v reflect.Value, opts encOpts) {
func (se sliceEncoder) encode(e *encodeState, v reflect.Value, opts encOpts) {
if v.IsNil() {
e.WriteString("null")
return
Expand All @@ -768,15 +768,15 @@ func newSliceEncoder(t reflect.Type) encoderFunc {
return encodeByteSlice
}
}
enc := &sliceEncoder{newArrayEncoder(t)}
enc := sliceEncoder{newArrayEncoder(t)}
return enc.encode
}

type arrayEncoder struct {
elemEnc encoderFunc
}

func (ae *arrayEncoder) encode(e *encodeState, v reflect.Value, opts encOpts) {
func (ae arrayEncoder) encode(e *encodeState, v reflect.Value, opts encOpts) {
e.WriteByte('[')
n := v.Len()
for i := 0; i < n; i++ {
Expand All @@ -789,15 +789,15 @@ func (ae *arrayEncoder) encode(e *encodeState, v reflect.Value, opts encOpts) {
}

func newArrayEncoder(t reflect.Type) encoderFunc {
enc := &arrayEncoder{typeEncoder(t.Elem())}
enc := arrayEncoder{typeEncoder(t.Elem())}
return enc.encode
}

type ptrEncoder struct {
elemEnc encoderFunc
}

func (pe *ptrEncoder) encode(e *encodeState, v reflect.Value, opts encOpts) {
func (pe ptrEncoder) encode(e *encodeState, v reflect.Value, opts encOpts) {
if v.IsNil() {
e.WriteString("null")
return
Expand All @@ -806,15 +806,15 @@ func (pe *ptrEncoder) encode(e *encodeState, v reflect.Value, opts encOpts) {
}

func newPtrEncoder(t reflect.Type) encoderFunc {
enc := &ptrEncoder{typeEncoder(t.Elem())}
enc := ptrEncoder{typeEncoder(t.Elem())}
return enc.encode
}

type condAddrEncoder struct {
canAddrEnc, elseEnc encoderFunc
}

func (ce *condAddrEncoder) encode(e *encodeState, v reflect.Value, opts encOpts) {
func (ce condAddrEncoder) encode(e *encodeState, v reflect.Value, opts encOpts) {
if v.CanAddr() {
ce.canAddrEnc(e, v, opts)
} else {
Expand All @@ -825,7 +825,7 @@ func (ce *condAddrEncoder) encode(e *encodeState, v reflect.Value, opts encOpts)
// newCondAddrEncoder returns an encoder that checks whether its value
// CanAddr and delegates to canAddrEnc if so, else to elseEnc.
func newCondAddrEncoder(canAddrEnc, elseEnc encoderFunc) encoderFunc {
enc := &condAddrEncoder{canAddrEnc: canAddrEnc, elseEnc: elseEnc}
enc := condAddrEncoder{canAddrEnc: canAddrEnc, elseEnc: elseEnc}
return enc.encode
}

Expand Down

0 comments on commit b659283

Please sign in to comment.