aboutsummaryrefslogtreecommitdiff
path: root/src/encoding/gob/decode.go
AgeCommit message (Collapse)Author
2025-09-11encoding/gob: make use of reflect.TypeAssertapocelipes
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>
2024-09-05encoding/gob: cover missed cases when checking ignore depthRoland Shoemaker
This change makes sure that we are properly checking the ignored field recursion depth in decIgnoreOpFor consistently. This prevents stack exhaustion when attempting to decode a message that contains an extremely deeply nested struct which is ignored. Thanks to Md Sakib Anwar of The Ohio State University (anwar.40@osu.edu) for reporting this issue. Fixes #69139 Fixes CVE-2024-34156 Change-Id: Iacce06be95a5892b3064f1c40fcba2e2567862d6 Reviewed-on: https://go-internal-review.googlesource.com/c/go/+/1440 Reviewed-by: Russ Cox <rsc@google.com> Reviewed-by: Damien Neil <dneil@google.com> Reviewed-on: https://go-review.googlesource.com/c/go/+/611239 LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Dmitri Shuralyov <dmitshur@google.com> Reviewed-by: Roland Shoemaker <roland@golang.org> Auto-Submit: Dmitri Shuralyov <dmitshur@golang.org>
2023-09-07encoding/gob: fix typo in comment for decAllocJes Cok
Change-Id: I89c607ee40358d6d650ba0ea1f05ce7d1df698bd GitHub-Last-Rev: e78a37118009dbd9468a0f656ad66b989f7c5ada GitHub-Pull-Request: golang/go#62319 Reviewed-on: https://go-review.googlesource.com/c/go/+/523376 Reviewed-by: Matthew Dempsky <mdempsky@google.com> Reviewed-by: qiulaidongfeng <2645477756@qq.com> Auto-Submit: Ian Lance Taylor <iant@google.com> Reviewed-by: Ian Lance Taylor <iant@google.com> Reviewed-by: Rob Pike <r@golang.org> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
2023-09-07encoding/gob: swap 'err, i' to correct verbs in decUint8SliceJes Cok
Change-Id: I76b2dd45179f65e9ed4f1d0f597ca59e49b59a85 GitHub-Last-Rev: b4171e6b836f948ff2973d91a19f0fd58cc8faea GitHub-Pull-Request: golang/go#62374 Reviewed-on: https://go-review.googlesource.com/c/go/+/524356 Reviewed-by: Ian Lance Taylor <iant@google.com> Auto-Submit: Ian Lance Taylor <iant@google.com> Reviewed-by: Matthew Dempsky <mdempsky@google.com> Reviewed-by: Rob Pike <r@golang.org> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
2023-08-31all: rewrite internal/saferio.SliceCap using generics and add func ↵Jes Cok
SliceCapWithSize Change-Id: I265173bf2722796c4be545c968efef3a1a6f7a7d GitHub-Last-Rev: 04d95cdd615f906167545f246f707e1440c39374 GitHub-Pull-Request: golang/go#62365 Reviewed-on: https://go-review.googlesource.com/c/go/+/524257 LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Auto-Submit: Ian Lance Taylor <iant@google.com> Reviewed-by: Russ Cox <rsc@golang.org> Reviewed-by: Ian Lance Taylor <iant@google.com>
2023-08-19encoding/gob: prevent panic from index out of range in Decoder.typeStringAndy Pan
I believe this bug is introduced by CL 460543 which optimizes the allocations by changing the type of `idToType` from map to slice, but didn't update the access code in `Decoder.typeString` that is safe for map but not for slice. Fixes #62117 Change-Id: I0f2e4cc2f34c54dada1f83458ba512a6fde6dcbe Reviewed-on: https://go-review.googlesource.com/c/go/+/520757 TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@google.com> Auto-Submit: Ian Lance Taylor <iant@google.com> Run-TryBot: Andy Pan <panjf2000@gmail.com> Auto-Submit: Ian Lance Taylor <iant@golang.org> Reviewed-by: Daniel Martí <mvdan@mvdan.cc> Reviewed-by: Dmitri Shuralyov <dmitshur@google.com> Run-TryBot: Ian Lance Taylor <iant@google.com>
2023-08-01encoding/gob: use reflect.TypeFor for known typesIan Lance Taylor
This avoids several mildly confusing Elem calls. For #60088 Change-Id: If7b83d2ab10537c7e886a035b43cb272130c1669 Reviewed-on: https://go-review.googlesource.com/c/go/+/514455 Run-TryBot: Ian Lance Taylor <iant@google.com> Reviewed-by: David Chase <drchase@google.com> Reviewed-by: Rob Pike <r@golang.org> Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@google.com> Auto-Submit: Ian Lance Taylor <iant@google.com>
2023-07-27reflect: deprecate PtrToqiulaidongfeng
Replace reflect.PtrTo with reflect.PointerTo. Fixes #59599 Change-Id: I49407193e2050543ef983cd637703acc682d9f51 Change-Id: I49407193e2050543ef983cd637703acc682d9f51 GitHub-Last-Rev: 7bc9ccf1dcb4b444406430b451ad4c47c6ec1be2 GitHub-Pull-Request: golang/go#61440 Reviewed-on: https://go-review.googlesource.com/c/go/+/511035 Run-TryBot: Ian Lance Taylor <iant@golang.org> Run-TryBot: Ian Lance Taylor <iant@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Auto-Submit: Ian Lance Taylor <iant@google.com> Reviewed-by: Keith Randall <khr@google.com> Reviewed-by: Ian Lance Taylor <iant@google.com>
2023-04-04encoding/gob: use reflect.Value.GrowDaniel Martí
Growing by one is a simpler, and often cheaper, operation compared to appending one (newly created) zero value. The method was introduced in Go 1.20. growSlice in dec_helpers.go is left alone, as it grows using the builtin append instead of reflect.Append. No noticeable performance difference on any of our benchmarks, as this code only runs for slices large enough to not fit in saferio.SliceCap, and none of our benchmarks use data that large. goos: linux goarch: amd64 pkg: encoding/gob cpu: AMD Ryzen 7 PRO 5850U with Radeon Graphics │ old │ new │ │ sec/op │ sec/op vs base │ DecodeBytesSlice-8 11.37µ ± 1% 11.46µ ± 4% ~ (p=0.315 n=10) DecodeInterfaceSlice-8 96.49µ ± 1% 95.75µ ± 1% ~ (p=0.436 n=10) geomean 33.12µ 33.12µ +0.01% │ old │ new │ │ B/op │ B/op vs base │ DecodeBytesSlice-8 22.39Ki ± 0% 22.39Ki ± 0% ~ (p=1.000 n=10) DecodeInterfaceSlice-8 80.25Ki ± 0% 80.25Ki ± 0% ~ (p=0.650 n=10) geomean 42.39Ki 42.39Ki +0.00% │ old │ new │ │ allocs/op │ allocs/op vs base │ DecodeBytesSlice-8 169.0 ± 0% 169.0 ± 0% ~ (p=1.000 n=10) ¹ DecodeInterfaceSlice-8 3.178k ± 0% 3.178k ± 0% ~ (p=1.000 n=10) ¹ geomean 732.9 732.9 +0.00% Change-Id: I468aebf4ae6f197a1fd35f6fee809ca591c1788f Reviewed-on: https://go-review.googlesource.com/c/go/+/481376 Reviewed-by: Ian Lance Taylor <iant@google.com> Run-TryBot: Daniel Martí <mvdan@mvdan.cc> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Rob Pike <r@golang.org> Reviewed-by: Michael Knyszek <mknyszek@google.com>
2023-02-27encoding/gob: use reflect.Value.SetZeroDaniel Martí
Particularly helps when decoding map maps. goos: linux goarch: amd64 pkg: encoding/gob cpu: AMD Ryzen 7 PRO 5850U with Radeon Graphics │ old │ new │ │ sec/op │ sec/op vs base │ DecodeStringSlice-16 30.78µ ± 2% 30.68µ ± 1% ~ (p=0.699 n=6) DecodeStringsSlice-16 61.36µ ± 6% 60.85µ ± 4% ~ (p=0.589 n=6) DecodeBytesSlice-16 16.57µ ± 2% 16.51µ ± 4% ~ (p=0.937 n=6) DecodeInterfaceSlice-16 124.3µ ± 0% 125.4µ ± 1% ~ (p=0.065 n=6) DecodeMap-16 251.0µ ± 3% 216.2µ ± 2% -13.86% (p=0.002 n=6) geomean 62.80µ 60.87µ -3.07% │ old │ new │ │ B/op │ B/op vs base │ DecodeStringSlice-16 37.98Ki ± 0% 37.98Ki ± 0% ~ (p=1.000 n=6) DecodeStringsSlice-16 63.79Ki ± 0% 63.79Ki ± 0% ~ (p=0.675 n=6) DecodeBytesSlice-16 22.40Ki ± 0% 22.40Ki ± 0% ~ (p=0.364 n=6) DecodeInterfaceSlice-16 80.27Ki ± 0% 80.27Ki ± 0% ~ (p=0.924 n=6) DecodeMap-16 52.67Ki ± 0% 52.67Ki ± 0% ~ (p=0.145 n=6) geomean 47.01Ki 47.01Ki +0.00% │ old │ new │ │ allocs/op │ allocs/op vs base │ DecodeStringSlice-16 1.169k ± 0% 1.169k ± 0% ~ (p=1.000 n=6) ¹ DecodeStringsSlice-16 2.178k ± 0% 2.178k ± 0% ~ (p=1.000 n=6) ¹ DecodeBytesSlice-16 169.0 ± 0% 169.0 ± 0% ~ (p=1.000 n=6) ¹ DecodeInterfaceSlice-16 3.178k ± 0% 3.178k ± 0% ~ (p=1.000 n=6) ¹ DecodeMap-16 181.0 ± 0% 181.0 ± 0% ~ (p=1.000 n=6) ¹ geomean 756.3 756.3 +0.00% ¹ all samples are equal Change-Id: I5ae9268b1a86296494d8569f5158ef0e78128eb1 Reviewed-on: https://go-review.googlesource.com/c/go/+/471257 Reviewed-by: Joseph Tsai <joetsai@digital-static.net> TryBot-Result: Gopher Robot <gobot@golang.org> Run-TryBot: Daniel Martí <mvdan@mvdan.cc> Reviewed-by: Ian Lance Taylor <iant@google.com> Reviewed-by: Than McIntosh <thanm@google.com>
2023-01-20encoding/gob: shave off some init time costDaniel Martí
Avoid unnecessary allocations when calling reflect.TypeOf; we can use nil pointers, which fit into an interface without allocating. This saves about 1% of CPU time. The builtin types are limited to typeIds between 0 and firstUserId, and since firstUserId is 64, builtinIdToType does not need to be a map. We can simply use an array of length firstUserId, which is simpler. This saves about 1% of CPU time. idToType is similar to firstUserId in that it is a map keyed by typeIds. The difference is that it can grow with the user's types. However, each added type gets the next available typeId, meaning that we can use a growing slice, similar to the case above. nextId then becomes the current length of the slice. This saves about 1% of CPU time. typeInfoMap is stored globally as an atomic.Value, where each modification loads the map, makes a whole copy, adds the new element, and stores the modified copy. This is perfectly fine when the user registers types, as that can happen concurrently and at any point in the future. However, during init time, we sequentially register many types, and the overhead of copying maps adds up noticeably. During init time, use a regular global map instead, which gets replaced by the atomic.Value when our init work is done. This saves about 2% of CPU time. Finally, avoid calling checkId in bootstrapType; we have just called setTypeId, whose logic for getting nextId is simple, so the extra check doesn't gain us much. This saves about 1% of CPU time. Using benchinit, which transforms GODEBUG=inittrace=1 data into Go benchmark compatible output, results in a nice improvement: name old time/op new time/op delta EncodingGob 175µs ± 0% 162µs ± 0% -7.45% (p=0.016 n=5+4) name old alloc/op new alloc/op delta EncodingGob 39.0kB ± 0% 36.1kB ± 0% -7.35% (p=0.016 n=5+4) name old allocs/op new allocs/op delta EncodingGob 588 ± 0% 558 ± 0% -5.10% (p=0.000 n=5+4) Updates #26775. Change-Id: I28618e8b96ef440480e666ef2cd5c4a9a332ef21 Reviewed-on: https://go-review.googlesource.com/c/go/+/460543 Reviewed-by: Carlos Amedee <carlos@golang.org> Reviewed-by: Cherry Mui <cherryyz@google.com> Reviewed-by: Rob Pike <r@golang.org> Run-TryBot: Rob Pike <r@golang.org> TryBot-Result: Gopher Robot <gobot@golang.org>
2022-10-20encoding/gob: support large slices in slice decode helpersIan Lance Taylor
The slice decode helpers weren't aware of partially allocated slices. Also add large slice support for []byte. Change-Id: I5044587e917508887c7721f8059d364189831693 Reviewed-on: https://go-review.googlesource.com/c/go/+/443777 Auto-Submit: Ian Lance Taylor <iant@google.com> Run-TryBot: Ian Lance Taylor <iant@golang.org> Run-TryBot: Ian Lance Taylor <iant@google.com> Reviewed-by: David Chase <drchase@google.com> Reviewed-by: Ian Lance Taylor <iant@google.com> TryBot-Result: Gopher Robot <gobot@golang.org>
2022-10-04encoding/gob: prevent a decoder state overflowDaniel Martí
When decoding a struct, if a positive delta is large enough to overflow when added to fieldnum, we would panic due to the resulting negative index. Instead, catch this problem and produce an error like we do with negative delta integers. If fieldnum ends up being negative or smaller than state.fieldnum, the addition overflowed. While here, remove an unnecessary break after an error call, since those error functions cause a panic. Fixes #55337. Change-Id: I7a0e4f43e5c81a703e79c1597e3bb3714cc832c8 Reviewed-on: https://go-review.googlesource.com/c/go/+/432715 Reviewed-by: Rob Pike <r@golang.org> Reviewed-by: Cherry Mui <cherryyz@google.com> Reviewed-by: Bryan Mills <bcmills@google.com> Run-TryBot: Daniel Martí <mvdan@mvdan.cc> TryBot-Result: Gopher Robot <gobot@golang.org>
2022-09-25encoding/gob: use saferio.SliceCap when decoding a sliceIan Lance Taylor
This avoids allocating an overly large slice for corrupt input. Change the saferio.SliceCap function to take a pointer to the element type, so that we can handle slices of interface types. This revealed that a couple of existing calls were actually incorrect, passing the slice type rather than the element type. No test case because the problem can only happen for invalid data. Let the fuzzer find cases like this. Fixes #55338 Change-Id: I3c1724183cc275d4981379773b0b8faa01a9cbd2 Reviewed-on: https://go-review.googlesource.com/c/go/+/433296 Reviewed-by: Ian Lance Taylor <iant@google.com> Reviewed-by: Daniel Martí <mvdan@mvdan.cc> Reviewed-by: Cherry Mui <cherryyz@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Run-TryBot: Ian Lance Taylor <iant@google.com> Auto-Submit: Ian Lance Taylor <iant@google.com>
2022-08-19encoding/gob: use saferio to read large bufferIan Lance Taylor
Avoid allocating large amounts of memory for corrupt input. No test case because the problem can only happen for invalid data. Let the fuzzer find cases like this. Fixes #53369 Change-Id: I67c5e75bf181ad84988d6d6da12507df0e6df8e8 Reviewed-on: https://go-review.googlesource.com/c/go/+/413979 Reviewed-by: Joseph Tsai <joetsai@digital-static.net> Run-TryBot: Ian Lance Taylor <iant@google.com> Auto-Submit: Ian Lance Taylor <iant@google.com> Reviewed-by: Ian Lance Taylor <iant@google.com> Reviewed-by: Joedian Reid <joedian@golang.org> TryBot-Result: Gopher Robot <gobot@golang.org>
2022-08-08encoding/gob: replace runtime values with constants in init()Tomasz Jezierski
Current init() implementation in `encoding/gob/decode.go` checks int/uint/uintptr bit size with reflection in runtime. We could replace it with values available on compile stage. This should reduce time and allocations on binary start. Results from GODEBUG=inittrace=1: before: init encoding/gob @4.4 ms, 0.21 ms clock, 43496 bytes, 652 allocs after: init encoding/gob @4.4 ms, 0.15 ms clock, 41672 bytes, 643 allocs Updates #54184 Change-Id: I46dda2682fb92519da199415e29401d61edce697 Reviewed-on: https://go-review.googlesource.com/c/go/+/420455 TryBot-Result: Gopher Robot <gobot@golang.org> Run-TryBot: Rob Pike <r@golang.org> Reviewed-by: Rob Pike <r@golang.org> Reviewed-by: Ian Lance Taylor <iant@google.com> Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
2022-07-12encoding/gob: add a depth limit for ignored fieldsRoland Shoemaker
Enforce a nesting limit of 10,000 for ignored fields during decoding of messages. This prevents the possibility of triggering stack exhaustion. Fixes #53615 Fixes CVE-2022-30635 Change-Id: I05103d06dd5ca3945fcba3c1f5d3b5a645e8fb0f Reviewed-on: https://team-review.git.corp.google.com/c/golang/go-private/+/1484771 Reviewed-by: Damien Neil <dneil@google.com> Reviewed-by: Tatiana Bradley <tatianabradley@google.com> Reviewed-on: https://go-review.googlesource.com/c/go/+/417064 Reviewed-by: Heschi Kreinick <heschi@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Tatiana Bradley <tatiana@golang.org> Run-TryBot: Michael Knyszek <mknyszek@google.com>
2021-10-26all: use reflect.{Pointer,PointerTo}Cuong Manh Le
Updates #47651 Updates #48665 Change-Id: I69a87b45a5cad7a07fbd855040cd9935cf874554 Reviewed-on: https://go-review.googlesource.com/c/go/+/358454 Trust: Cuong Manh Le <cuong.manhle.vn@gmail.com> Run-TryBot: Cuong Manh Le <cuong.manhle.vn@gmail.com> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2021-09-07encoding/gob: optimize decoding of slicekorzhao
In CL 345572, we used the reflect.Value.SetLen method to avoid extra memory allocation for reflect.Value.Slice. This also applies to function decodeSlice name old time/op new time/op delta DecodeStringsSlice-12 96.5µs ±12% 63.0µs ± 8% -34.68% (p=0.000 n=9+10) name old alloc/op new alloc/op delta DecodeStringsSlice-12 89.3kB ± 0% 65.3kB ± 0% -26.89% (p=0.000 n=10+10) name old allocs/op new allocs/op delta DecodeStringsSlice-12 3.18k ± 0% 2.18k ± 0% -31.47% (p=0.000 n=10+10) Change-Id: Ifdb43716cc90a265962dec022704a5571f447fd8 Reviewed-on: https://go-review.googlesource.com/c/go/+/347533 Reviewed-by: Joe Tsai <joetsai@digital-static.net> Reviewed-by: Rob Pike <r@golang.org> Trust: Joe Tsai <joetsai@digital-static.net> Run-TryBot: Joe Tsai <joetsai@digital-static.net> TryBot-Result: Go Bot <gobot@golang.org>
2021-08-27encoding/gob: optimize decoding of []byteJoe Tsai
The reflect.Value.Slice method unfortunately allocates every time since it needs to place the slice header on the heap. This is silly since gob immediately stores the result back into slice. Instead, use the reflect.Value.SetLen method. DecodeBytesSlice 75.0µs ± 2% 35.2µs ± 6% -53.02% Change-Id: I3ca0529d01bf978f2b76e215f52d369f458951ef Reviewed-on: https://go-review.googlesource.com/c/go/+/345572 Trust: Joe Tsai <joetsai@digital-static.net> Run-TryBot: Joe Tsai <joetsai@digital-static.net> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Rob Pike <r@golang.org>
2018-03-24all: remove some unused return parametersDaniel Martí
As found by unparam. Picked the low-hanging fruit, consisting only of errors that were always nil and results that were never used. Left out those that were useful for consistency with other func signatures. Change-Id: I06b52bbd3541f8a5d66659c909bd93cb3e172018 Reviewed-on: https://go-review.googlesource.com/102418 Run-TryBot: Daniel Martí <mvdan@mvdan.cc> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2018-01-04encoding/gob: avoid race on idToTypeIan Lance Taylor
Fixes #23328 Change-Id: Ie4864d7f388d363860318fe41431d8a9719e9a75 Reviewed-on: https://go-review.googlesource.com/86075 Run-TryBot: Ian Lance Taylor <iant@golang.org> Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org> Reviewed-by: Rob Pike <r@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
2017-05-26encoding/gob: speedup decoding of maps by zeroing valuesFilip Gruszczyński
Instead of allocating a new reflect.Value object on every loop we zero it. DecodeComplex128Slice-8 13.1µs ± 7% 13.2µs ± 8% ~ (p=0.347 n=18+19) DecodeFloat64Slice-8 8.13µs ± 5% 8.00µs ± 3% ~ (p=0.168 n=20+19) DecodeInt32Slice-8 8.27µs ± 5% 8.08µs ± 5% -2.27% (p=0.001 n=19+18) DecodeStringSlice-8 17.9µs ±12% 17.8µs ±11% ~ (p=0.989 n=20+19) DecodeInterfaceSlice-8 163µs ±10% 159µs ± 4% ~ (p=0.057 n=19+19) DecodeMap-8 220µs ± 2% 183µs ± 1% -17.07% (p=0.000 n=19+18) Updates #19525 Change-Id: I27f8edd4761787f6b9928d34cefa08a34a6e25b2 Reviewed-on: https://go-review.googlesource.com/39203 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org> Reviewed-by: Rob Pike <r@golang.org> Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
2017-05-03encoding/gob: use MakeMapWithSize when decoding mapFilip Gruszczyński
This allows to pre-allocate the final size of the hashmap and avoid re-allocating as we insert entries. Furthermore for the current implementation of the hashmap it allows avoiding several rounds of evacuating hashmap entries after each re-allocation. DecodeComplex128Slice-8 51.9µs ± 1% 51.9µs ± 2% ~ (p=0.797 n=30+29) DecodeFloat64Slice-8 31.5µs ± 2% 31.6µs ± 2% ~ (p=0.050 n=28+28) DecodeInt32Slice-8 32.0µs ± 2% 31.9µs ± 3% ~ (p=0.666 n=29+28) DecodeStringSlice-8 57.7µs ± 2% 57.8µs ± 3% ~ (p=0.780 n=27+30) DecodeInterfaceSlice-8 498µs ± 2% 495µs ± 2% ~ (p=0.070 n=28+29) DecodeMap-8 300µs ± 2% 230µs ± 5% -23.31% (p=0.000 n=27+27) Updates #19525 Change-Id: Ia7233da49f05bae7a86c064d9ecebca966f5f2f7 Reviewed-on: https://go-review.googlesource.com/40113 Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2017-04-27encoding/gob: replace RWMutex usage with sync.MapBryan C. Mills
This provides a significant speedup for encoding and decoding when using many CPU cores. name old time/op new time/op delta EndToEndPipe 5.26µs ± 2% 5.38µs ± 7% ~ (p=0.121 n=8+7) EndToEndPipe-6 1.86µs ± 5% 1.80µs ±11% ~ (p=0.442 n=8+8) EndToEndPipe-48 1.39µs ± 2% 1.41µs ± 4% ~ (p=0.645 n=8+8) EndToEndByteBuffer 1.54µs ± 5% 1.57µs ± 5% ~ (p=0.130 n=8+8) EndToEndByteBuffer-6 620ns ± 6% 310ns ± 8% -50.04% (p=0.000 n=8+8) EndToEndByteBuffer-48 506ns ± 4% 110ns ± 3% -78.22% (p=0.000 n=8+8) EndToEndSliceByteBuffer 149µs ± 3% 153µs ± 5% +2.80% (p=0.021 n=8+8) EndToEndSliceByteBuffer-6 103µs ±17% 31µs ±12% -70.06% (p=0.000 n=8+8) EndToEndSliceByteBuffer-48 93.2µs ± 2% 18.0µs ± 5% -80.66% (p=0.000 n=7+8) EncodeComplex128Slice 20.6µs ± 5% 20.9µs ± 8% ~ (p=0.959 n=8+8) EncodeComplex128Slice-6 4.10µs ±10% 3.75µs ± 8% -8.58% (p=0.004 n=8+7) EncodeComplex128Slice-48 1.14µs ± 2% 0.81µs ± 2% -28.98% (p=0.000 n=8+8) EncodeFloat64Slice 10.2µs ± 7% 10.1µs ± 6% ~ (p=0.694 n=7+8) EncodeFloat64Slice-6 2.01µs ± 6% 1.80µs ±11% -10.30% (p=0.004 n=8+8) EncodeFloat64Slice-48 701ns ± 3% 408ns ± 2% -41.72% (p=0.000 n=8+8) EncodeInt32Slice 11.8µs ± 7% 11.7µs ± 6% ~ (p=0.463 n=8+7) EncodeInt32Slice-6 2.32µs ± 4% 2.06µs ± 5% -10.89% (p=0.000 n=8+8) EncodeInt32Slice-48 731ns ± 2% 445ns ± 2% -39.10% (p=0.000 n=7+8) EncodeStringSlice 9.13µs ± 9% 9.18µs ± 8% ~ (p=0.798 n=8+8) EncodeStringSlice-6 1.91µs ± 5% 1.70µs ± 5% -11.07% (p=0.000 n=8+8) EncodeStringSlice-48 679ns ± 3% 397ns ± 3% -41.50% (p=0.000 n=8+8) EncodeInterfaceSlice 449µs ±11% 461µs ± 9% ~ (p=0.328 n=8+8) EncodeInterfaceSlice-6 503µs ± 7% 88µs ± 7% -82.51% (p=0.000 n=7+8) EncodeInterfaceSlice-48 335µs ± 8% 22µs ± 1% -93.55% (p=0.000 n=8+7) DecodeComplex128Slice 67.2µs ± 4% 67.0µs ± 6% ~ (p=0.721 n=8+8) DecodeComplex128Slice-6 22.0µs ± 8% 18.9µs ± 5% -14.44% (p=0.000 n=8+8) DecodeComplex128Slice-48 46.8µs ± 3% 34.9µs ± 3% -25.48% (p=0.000 n=8+8) DecodeFloat64Slice 39.4µs ± 4% 40.3µs ± 3% ~ (p=0.105 n=8+8) DecodeFloat64Slice-6 16.1µs ± 2% 11.2µs ± 7% -30.64% (p=0.001 n=6+7) DecodeFloat64Slice-48 38.1µs ± 3% 24.0µs ± 7% -37.10% (p=0.000 n=8+8) DecodeInt32Slice 39.1µs ± 4% 40.1µs ± 5% ~ (p=0.083 n=8+8) DecodeInt32Slice-6 16.3µs ±21% 10.6µs ± 1% -35.17% (p=0.000 n=8+7) DecodeInt32Slice-48 36.5µs ± 6% 21.9µs ± 9% -39.89% (p=0.000 n=8+8) DecodeStringSlice 82.9µs ± 6% 85.5µs ± 5% ~ (p=0.121 n=8+7) DecodeStringSlice-6 32.4µs ±11% 26.8µs ±16% -17.37% (p=0.000 n=8+8) DecodeStringSlice-48 76.0µs ± 2% 57.0µs ± 5% -25.02% (p=0.000 n=8+8) DecodeInterfaceSlice 718µs ± 4% 752µs ± 5% +4.83% (p=0.038 n=8+8) DecodeInterfaceSlice-6 500µs ± 6% 165µs ± 7% -66.95% (p=0.000 n=7+8) DecodeInterfaceSlice-48 470µs ± 5% 120µs ± 6% -74.55% (p=0.000 n=8+7) DecodeMap 3.29ms ± 5% 3.34ms ± 5% ~ (p=0.279 n=8+8) DecodeMap-6 7.73ms ± 8% 7.53ms ±18% ~ (p=0.779 n=7+8) DecodeMap-48 7.46ms ± 6% 7.71ms ± 3% ~ (p=0.161 n=8+8) https://perf.golang.org/search?q=upload:20170426.4 Change-Id: I335874028ef8d7c991051004f8caadd16c92d5cc Reviewed-on: https://go-review.googlesource.com/41872 Run-TryBot: Bryan Mills <bcmills@google.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org>
2017-03-24encoding/gob: Speedup map decoding by reducing the allocations.Filip Gruszczyński
The improvementis achieved in encoding/gob/decode.go decodeMap by allocate keyInstr and elemInstr only once and pass it to decodeIntoValue, instead of allocating a new instance on every loop cycle. name old time/op new time/op delta DecodeComplex128Slice-8 64.2µs ±10% 62.2µs ± 8% ~ (p=0.686 n=4+4) DecodeFloat64Slice-8 37.1µs ± 3% 36.5µs ± 5% ~ (p=0.343 n=4+4) DecodeInt32Slice-8 33.7µs ± 3% 32.7µs ± 4% ~ (p=0.200 n=4+4) DecodeStringSlice-8 59.7µs ± 5% 57.3µs ± 1% ~ (p=0.114 n=4+4) DecodeInterfaceSlice-8 543µs ± 7% 497µs ± 3% ~ (p=0.057 n=4+4) DecodeMap-8 3.78ms ± 8% 2.66ms ± 2% -29.69% (p=0.029 n=4+4) Updates #19525 Change-Id: Iec5fa4530de76f0a70da5de8a129a567b4aa096e Reviewed-on: https://go-review.googlesource.com/38317 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org> Reviewed-by: Rob Pike <r@golang.org> Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
2017-03-18encoding/gob: speedup floats encoding and decodingAlberto Donizetti
By replacing bytes-reversing routines with bits.ReverseBytes64 calls. name old time/op new time/op delta EncodeComplex128Slice-4 35.1µs ± 1% 23.2µs ± 2% -33.94% (p=0.000 n=20+20) EncodeFloat64Slice-4 17.9µs ± 1% 11.0µs ± 1% -38.36% (p=0.000 n=17+18) name old time/op new time/op delta DecodeComplex128Slice-4 79.7µs ± 0% 69.9µs ± 1% -12.31% (p=0.000 n=20+20) DecodeFloat64Slice-4 47.3µs ± 1% 42.2µs ± 1% -10.65% (p=0.000 n=17+17) Change-Id: I91a6401c6009b5712fca6258dd1e57c6fe68ea64 Reviewed-on: https://go-review.googlesource.com/38352 Run-TryBot: Alberto Donizetti <alb.donizetti@gmail.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2017-03-06encoding/gob: remove unused ut and atyp parametersDaniel Martí
Found by github.com/mvdan/unparam. Change-Id: Ic97f05a2ecb5b17caa36aafe403e2266abea3e0e Reviewed-on: https://go-review.googlesource.com/37836 Run-TryBot: Rob Pike <r@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Rob Pike <r@golang.org>
2016-06-28encoding/gob: avoid allocating string for map keyDavid Crawshaw
On linux/386 compared to tip: name old time/op new time/op delta DecodeInterfaceSlice-40 1.23ms ± 1% 1.17ms ± 1% -4.93% (p=0.000 n=9+10) Recovers about half the performance regression from Go 1.6 on 386. For #16117. Change-Id: Ie8676d92a4da3e27ff21b91a98b3e13d16730ba1 Reviewed-on: https://go-review.googlesource.com/24468 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org> Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
2016-03-02all: single space after period.Brad Fitzpatrick
The tree's pretty inconsistent about single space vs double space after a period in documentation. Make it consistently a single space, per earlier decisions. This means contributors won't be confused by misleading precedence. This CL doesn't use go/doc to parse. It only addresses // comments. It was generated with: $ perl -i -npe 's,^(\s*// .+[a-z]\.) +([A-Z]),$1 $2,' $(git grep -l -E '^\s*//(.+\.) +([A-Z])') $ go test go/doc -update Change-Id: Iccdb99c37c797ef1f804a94b22ba5ee4b500c4f7 Reviewed-on: https://go-review.googlesource.com/20022 Reviewed-by: Rob Pike <r@golang.org> Reviewed-by: Dave Day <djd@golang.org> Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
2015-11-25encoding/gob: reduce the amount of memory allocations.Aliaksandr Valialkin
Benchmark results: benchmark old ns/op new ns/op delta BenchmarkEndToEndPipe-4 7547 7294 -3.35% BenchmarkEndToEndByteBuffer-4 5146 5092 -1.05% BenchmarkEndToEndSliceByteBuffer-4 552779 439768 -20.44% BenchmarkEncodeComplex128Slice-4 266370 266184 -0.07% BenchmarkEncodeFloat64Slice-4 111891 110258 -1.46% BenchmarkEncodeInt32Slice-4 74482 74080 -0.54% BenchmarkEncodeStringSlice-4 84404 84279 -0.15% BenchmarkEncodeInterfaceSlice-4 3942925 3045995 -22.75% BenchmarkDecodeComplex128Slice-4 451837 415282 -8.09% BenchmarkDecodeFloat64Slice-4 283584 262558 -7.41% BenchmarkDecodeInt32Slice-4 246571 237383 -3.73% BenchmarkDecodeStringSlice-4 734210 479625 -34.67% BenchmarkDecodeInterfaceSlice-4 4778225 4160935 -12.92% benchmark old allocs new allocs delta BenchmarkEndToEndPipe-4 3 2 -33.33% BenchmarkEndToEndByteBuffer-4 3 2 -33.33% BenchmarkEndToEndSliceByteBuffer-4 1002 402 -59.88% BenchmarkEncodeComplex128Slice-4 1 1 +0.00% BenchmarkEncodeFloat64Slice-4 1 1 +0.00% BenchmarkEncodeInt32Slice-4 1 1 +0.00% BenchmarkEncodeStringSlice-4 1 1 +0.00% BenchmarkEncodeInterfaceSlice-4 3001 1 -99.97% BenchmarkDecodeComplex128Slice-4 188 185 -1.60% BenchmarkDecodeFloat64Slice-4 188 185 -1.60% BenchmarkDecodeInt32Slice-4 188 185 -1.60% BenchmarkDecodeStringSlice-4 2188 1185 -45.84% BenchmarkDecodeInterfaceSlice-4 6197 4194 -32.32% benchmark old bytes new bytes delta BenchmarkEndToEndPipe-4 64 48 -25.00% BenchmarkEndToEndByteBuffer-4 64 48 -25.00% BenchmarkEndToEndSliceByteBuffer-4 34551 10554 -69.45% BenchmarkEncodeComplex128Slice-4 55 55 +0.00% BenchmarkEncodeFloat64Slice-4 33 33 +0.00% BenchmarkEncodeInt32Slice-4 32 32 +0.00% BenchmarkEncodeStringSlice-4 36 36 +0.00% BenchmarkEncodeInterfaceSlice-4 144555 347 -99.76% BenchmarkDecodeComplex128Slice-4 28240 28097 -0.51% BenchmarkDecodeFloat64Slice-4 11840 11697 -1.21% BenchmarkDecodeInt32Slice-4 10817 10673 -1.33% BenchmarkDecodeStringSlice-4 56128 39985 -28.76% BenchmarkDecodeInterfaceSlice-4 132565 100421 -24.25% Change-Id: Ief7c7706b1f2916486ab7190b81aafbb16b70f1e Reviewed-on: https://go-review.googlesource.com/13660 Reviewed-by: Russ Cox <rsc@golang.org>
2015-08-25encoding/gob: remove always false comparisonTodd Neal
This is not a functional change. nr is a uint64 and can never be less than zero, remove the no-op comparison. Fixes #11279 Change-Id: Iebb36cc8fe97428b503e65d01b5e67d2b2bc7369 Reviewed-on: https://go-review.googlesource.com/13876 Run-TryBot: Todd Neal <todd@tneal.org> Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
2015-06-01encoding/gob: fix infinite recursion caused by ignoring recursive typeRob Pike
This was a simple oversight: the algorithm to handle recursive types needed to be applied to the ignore-item case as well. Fixes #10415. Change-Id: I39ef31cad680ab8334e141f60d2f8707896785d1 Reviewed-on: https://go-review.googlesource.com/8942 Reviewed-by: Dmitry Vyukov <dvyukov@google.com>
2015-04-17encoding/gob: fix hang from skipping large slicesMatthew Dempsky
Change-Id: I4e59b5b1702e08d7c6191d0a70fb0a555f3340c8 Reviewed-on: https://go-review.googlesource.com/9061 Run-TryBot: Matthew Dempsky <mdempsky@google.com> Reviewed-by: Rob Pike <r@golang.org>
2015-04-09encoding/gob: more checks for corrupted dataRob Pike
Also unify the tests where possible to make it easy to add more. Fixes #10273. Change-Id: Idfa4f4a5dcaa05974066bafe17bed6cdd2ebedb7 Reviewed-on: https://go-review.googlesource.com/8662 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2015-04-08encoding/gob: clean up decoderMap after errBadTypeHåvard Haugen
When decoding an invalid typeId the associated *decEngine was not removed from decoderMap. If the decoder was run again on the same input a nil *decEngine was found in the map and assumed to be initialized, resulting in a panic. Fixes #9649 Change-Id: I5bb51808362a21c09228c2705a658f073e5b59b3 Reviewed-on: https://go-review.googlesource.com/3509 Reviewed-by: Rob Pike <r@golang.org>
2015-04-07encoding/gob: more cleanups handling slice lengthRob Pike
Fix the other places the slice length was being believed, and refactor the code to use a single function to unify the check. Fixes #10273. Change-Id: Ia62b25203fbe87c95d71a70ebc1db8d202eaa4a4 Reviewed-on: https://go-review.googlesource.com/8511 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2015-04-07encoding/gob: make method doc string match method namekortschak
Change-Id: I6859bd9c9dba30fc5eeb9bbc1de90af67984944c Reviewed-on: https://go-review.googlesource.com/8526 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2015-04-06encoding/gob: change panic into error for corrupt inputRob Pike
decBuffer.Drop is called using data provided by the user, don't panic if it's bogus. Fixes #10272. Change-Id: I913ae9c3c45cef509f2b8eb02d1efa87fbd52afa Reviewed-on: https://go-review.googlesource.com/8496 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2014-10-20encoding/gob: add custom decoder buffer for performanceRob Pike
As we did with encoding, provide a trivial byte reader for faster decoding. We can also reduce some of the copying by doing the allocation all at once using a slightly different interface from byte buffers. benchmark old ns/op new ns/op delta BenchmarkEndToEndPipe 13368 12902 -3.49% BenchmarkEndToEndByteBuffer 5969 5642 -5.48% BenchmarkEndToEndSliceByteBuffer 479485 470798 -1.81% BenchmarkEncodeComplex128Slice 92367 92201 -0.18% BenchmarkEncodeFloat64Slice 39990 38960 -2.58% BenchmarkEncodeInt32Slice 30510 27938 -8.43% BenchmarkEncodeStringSlice 33753 33365 -1.15% BenchmarkDecodeComplex128Slice 232278 196704 -15.32% BenchmarkDecodeFloat64Slice 150258 128191 -14.69% BenchmarkDecodeInt32Slice 133806 115748 -13.50% BenchmarkDecodeStringSlice 335117 300534 -10.32% LGTM=rsc R=rsc CC=golang-codereviews https://golang.org/cl/154360049
2014-10-17encoding/gob: custom array/slice decodersRob Pike
Use go generate to write better loops for decoding arrays, just as we did for encoding. It doesn't help as much, relatively speaking, but it's still noticeable. benchmark old ns/op new ns/op delta BenchmarkDecodeComplex128Slice 202348 184529 -8.81% BenchmarkDecodeFloat64Slice 135800 120979 -10.91% BenchmarkDecodeInt32Slice 121200 105149 -13.24% BenchmarkDecodeStringSlice 288129 278214 -3.44% LGTM=rsc R=rsc CC=golang-codereviews https://golang.org/cl/154420044
2014-09-25encoding/gob: fix 386 buildRob Pike
LGTM=ruiu R=golang-codereviews, ruiu CC=golang-codereviews https://golang.org/cl/146320043
2014-09-25encoding/gob: error rather than panic when decoding enormous slicesRob Pike
Fixes #8084. LGTM=ruiu R=golang-codereviews, ruiu CC=golang-codereviews https://golang.org/cl/142710043
2014-09-08build: move package sources from src/pkg to srcRuss Cox
Preparation was in CL 134570043. This CL contains only the effect of 'hg mv src/pkg/* src'. For more about the move, see golang.org/s/go14nopkg.