Skip to content

Commit cdb3d46

Browse files
apocelipesgopherbot
authored andcommitted
encoding/gob: make use of reflect.TypeAssert
Use "reflect.TypeAssert" to simplify the code. There are also some performance gains: goarch: arm64 pkg: encoding/gob cpu: Apple M4 │ old │ new │ │ sec/op │ sec/op vs base │ EncodeComplex128Slice-10 1.048µ ± 3% 1.048µ ± 1% ~ (p=0.986 n=10) EncodeFloat64Slice-10 481.5n ± 0% 538.1n ± 0% +11.75% (p=0.000 n=10) EncodeInt32Slice-10 560.0n ± 1% 562.2n ± 1% ~ (p=0.239 n=10) EncodeStringSlice-10 713.1n ± 2% 690.1n ± 1% -3.24% (p=0.000 n=10) EncodeInterfaceSlice-10 16.10µ ± 3% 16.89µ ± 3% +4.94% (p=0.004 n=10) DecodeComplex128Slice-10 5.507µ ± 2% 5.488µ ± 2% ~ (p=0.617 n=10) DecodeFloat64Slice-10 3.359µ ± 1% 3.365µ ± 1% ~ (p=0.403 n=10) DecodeInt32Slice-10 3.296µ ± 1% 3.290µ ± 2% ~ (p=0.926 n=10) DecodeStringSlice-10 8.397µ ± 2% 8.459µ ± 1% ~ (p=0.796 n=10) DecodeStringsSlice-10 18.47µ ± 1% 11.14µ ± 1% -39.69% (p=0.000 n=10) DecodeBytesSlice-10 5.038µ ± 1% 5.039µ ± 1% ~ (p=0.956 n=10) DecodeInterfaceSlice-10 40.14µ ± 1% 40.60µ ± 1% +1.16% (p=0.001 n=10) DecodeMap-10 43.43µ ± 1% 44.09µ ± 1% +1.51% (p=0.000 n=10) geomean 4.451µ 4.335µ -2.62% │ old │ new │ │ B/op │ B/op vs base │ EncodeComplex128Slice-10 1.0000 ± ? 0.5000 ± ? ~ (p=0.350 n=10) EncodeFloat64Slice-10 0.000 ± 0% 0.000 ± 0% ~ (p=1.000 n=10) ¹ EncodeInt32Slice-10 0.000 ± 0% 0.000 ± 0% ~ (p=1.000 n=10) ¹ EncodeStringSlice-10 0.000 ± 0% 0.000 ± 0% ~ (p=1.000 n=10) ¹ EncodeInterfaceSlice-10 19.00 ± 5% 20.00 ± 5% +5.26% (p=0.002 n=10) DecodeComplex128Slice-10 24.55Ki ± 0% 24.53Ki ± 0% -0.10% (p=0.000 n=10) DecodeFloat64Slice-10 10.56Ki ± 0% 10.54Ki ± 0% -0.22% (p=0.000 n=10) DecodeInt32Slice-10 9.539Ki ± 0% 9.516Ki ± 0% -0.25% (p=0.000 n=10) DecodeStringSlice-10 38.18Ki ± 0% 38.16Ki ± 0% -0.06% (p=0.000 n=10) DecodeStringsSlice-10 63.96Ki ± 0% 40.51Ki ± 0% -36.65% (p=0.000 n=10) DecodeBytesSlice-10 22.58Ki ± 0% 22.58Ki ± 0% ~ (p=1.000 n=10) DecodeInterfaceSlice-10 80.47Ki ± 0% 80.47Ki ± 0% ~ (p=1.000 n=10) DecodeMap-10 48.81Ki ± 0% 48.81Ki ± 0% ~ (p=1.000 n=10) ¹ geomean ² -8.15% ² ¹ all samples are equal ² summaries must be >0 to compute geomean │ old │ new │ │ allocs/op │ allocs/op vs base │ EncodeComplex128Slice-10 0.000 ± 0% 0.000 ± 0% ~ (p=1.000 n=10) ¹ EncodeFloat64Slice-10 0.000 ± 0% 0.000 ± 0% ~ (p=1.000 n=10) ¹ EncodeInt32Slice-10 0.000 ± 0% 0.000 ± 0% ~ (p=1.000 n=10) ¹ EncodeStringSlice-10 0.000 ± 0% 0.000 ± 0% ~ (p=1.000 n=10) ¹ EncodeInterfaceSlice-10 0.000 ± 0% 0.000 ± 0% ~ (p=1.000 n=10) ¹ DecodeComplex128Slice-10 149.0 ± 0% 148.0 ± 0% -0.67% (p=0.000 n=10) DecodeFloat64Slice-10 150.0 ± 0% 149.0 ± 0% -0.67% (p=0.000 n=10) DecodeInt32Slice-10 149.0 ± 0% 148.0 ± 0% -0.67% (p=0.000 n=10) DecodeStringSlice-10 1.150k ± 0% 1.149k ± 0% -0.09% (p=0.000 n=10) DecodeStringsSlice-10 2.158k ± 0% 1.158k ± 0% -46.34% (p=0.000 n=10) DecodeBytesSlice-10 149.0 ± 0% 149.0 ± 0% ~ (p=1.000 n=10) ¹ DecodeInterfaceSlice-10 3.158k ± 0% 3.158k ± 0% ~ (p=1.000 n=10) ¹ DecodeMap-10 160.0 ± 0% 160.0 ± 0% ~ (p=1.000 n=10) ¹ geomean ² -4.83% ² ¹ all samples are equal ² summaries must be >0 to compute geomean Updates golang#62121 Change-Id: I1d3534e5001ca185bc8ba5a7ed4ddbc00f85a17e GitHub-Last-Rev: c0209f8 GitHub-Pull-Request: golang#75409 Reviewed-on: https://go-review.googlesource.com/c/go/+/702735 Reviewed-by: Mark Freeman <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Auto-Submit: Michael Pratt <[email protected]> Reviewed-by: Daniel Martí <[email protected]> Reviewed-by: Michael Pratt <[email protected]>
1 parent fef3609 commit cdb3d46

File tree

6 files changed

+46
-40
lines changed

6 files changed

+46
-40
lines changed

src/encoding/gob/dec_helpers.go

Lines changed: 16 additions & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/encoding/gob/decgen.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ func dec%[2]sArray(state *decoderState, v reflect.Value, length int, ovfl error)
231231

232232
const sliceHelper = `
233233
func dec%[2]sSlice(state *decoderState, v reflect.Value, length int, ovfl error) bool {
234-
slice, ok := v.Interface().([]%[1]s)
234+
slice, ok := reflect.TypeAssert[[]%[1]s](v)
235235
if !ok {
236236
// It is kind %[1]s but not type %[1]s. TODO: We can handle this unsafely.
237237
return false

src/encoding/gob/decode.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -768,11 +768,14 @@ func (dec *Decoder) decodeGobDecoder(ut *userTypeInfo, state *decoderState, valu
768768
// We know it's one of these.
769769
switch ut.externalDec {
770770
case xGob:
771-
err = value.Interface().(GobDecoder).GobDecode(b)
771+
gobDecoder, _ := reflect.TypeAssert[GobDecoder](value)
772+
err = gobDecoder.GobDecode(b)
772773
case xBinary:
773-
err = value.Interface().(encoding.BinaryUnmarshaler).UnmarshalBinary(b)
774+
binaryUnmarshaler, _ := reflect.TypeAssert[encoding.BinaryUnmarshaler](value)
775+
err = binaryUnmarshaler.UnmarshalBinary(b)
774776
case xText:
775-
err = value.Interface().(encoding.TextUnmarshaler).UnmarshalText(b)
777+
textUnmarshaler, _ := reflect.TypeAssert[encoding.TextUnmarshaler](value)
778+
err = textUnmarshaler.UnmarshalText(b)
776779
}
777780
if err != nil {
778781
error_(err)

src/encoding/gob/enc_helpers.go

Lines changed: 16 additions & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/encoding/gob/encgen.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ func enc%[2]sArray(state *encoderState, v reflect.Value) bool {
208208

209209
const sliceHelper = `
210210
func enc%[2]sSlice(state *encoderState, v reflect.Value) bool {
211-
slice, ok := v.Interface().([]%[1]s)
211+
slice, ok := reflect.TypeAssert[[]%[1]s](v)
212212
if !ok {
213213
// It is kind %[1]s but not type %[1]s. TODO: We can handle this unsafely.
214214
return false

src/encoding/gob/encode.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -440,11 +440,14 @@ func (enc *Encoder) encodeGobEncoder(b *encBuffer, ut *userTypeInfo, v reflect.V
440440
// We know it's one of these.
441441
switch ut.externalEnc {
442442
case xGob:
443-
data, err = v.Interface().(GobEncoder).GobEncode()
443+
gobEncoder, _ := reflect.TypeAssert[GobEncoder](v)
444+
data, err = gobEncoder.GobEncode()
444445
case xBinary:
445-
data, err = v.Interface().(encoding.BinaryMarshaler).MarshalBinary()
446+
binaryMarshaler, _ := reflect.TypeAssert[encoding.BinaryMarshaler](v)
447+
data, err = binaryMarshaler.MarshalBinary()
446448
case xText:
447-
data, err = v.Interface().(encoding.TextMarshaler).MarshalText()
449+
textMarshaler, _ := reflect.TypeAssert[encoding.TextMarshaler](v)
450+
data, err = textMarshaler.MarshalText()
448451
}
449452
if err != nil {
450453
error_(err)

0 commit comments

Comments
 (0)