diff options
| author | apocelipes <seve3r@outlook.com> | 2025-09-11 09:40:02 +0000 |
|---|---|---|
| committer | Gopher Robot <gobot@golang.org> | 2025-09-11 10:56:37 -0700 |
| commit | cdb3d467fa2df80611cc7576ce3b3806aa715fa3 (patch) | |
| tree | 9d186dd11aa25293b04aaecfabcf322a3f8d9539 /src/encoding/gob/encode.go | |
| parent | fef360964c85d47406bdfbf7333591686720663b (diff) | |
| download | go-cdb3d467fa2df80611cc7576ce3b3806aa715fa3.tar.xz | |
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 #62121
Change-Id: I1d3534e5001ca185bc8ba5a7ed4ddbc00f85a17e
GitHub-Last-Rev: c0209f8c50e14477069592bfeb161011e7f9e80a
GitHub-Pull-Request: golang/go#75409
Reviewed-on: https://go-review.googlesource.com/c/go/+/702735
Reviewed-by: Mark Freeman <markfreeman@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Auto-Submit: Michael Pratt <mpratt@google.com>
Reviewed-by: Daniel Martí <mvdan@mvdan.cc>
Reviewed-by: Michael Pratt <mpratt@google.com>
Diffstat (limited to 'src/encoding/gob/encode.go')
| -rw-r--r-- | src/encoding/gob/encode.go | 9 |
1 files changed, 6 insertions, 3 deletions
diff --git a/src/encoding/gob/encode.go b/src/encoding/gob/encode.go index ed3494218c..15932aabe0 100644 --- a/src/encoding/gob/encode.go +++ b/src/encoding/gob/encode.go @@ -440,11 +440,14 @@ func (enc *Encoder) encodeGobEncoder(b *encBuffer, ut *userTypeInfo, v reflect.V // We know it's one of these. switch ut.externalEnc { case xGob: - data, err = v.Interface().(GobEncoder).GobEncode() + gobEncoder, _ := reflect.TypeAssert[GobEncoder](v) + data, err = gobEncoder.GobEncode() case xBinary: - data, err = v.Interface().(encoding.BinaryMarshaler).MarshalBinary() + binaryMarshaler, _ := reflect.TypeAssert[encoding.BinaryMarshaler](v) + data, err = binaryMarshaler.MarshalBinary() case xText: - data, err = v.Interface().(encoding.TextMarshaler).MarshalText() + textMarshaler, _ := reflect.TypeAssert[encoding.TextMarshaler](v) + data, err = textMarshaler.MarshalText() } if err != nil { error_(err) |
