aboutsummaryrefslogtreecommitdiff
path: root/src/encoding/binary/binary.go
AgeCommit message (Collapse)Author
2023-09-08encoding: modernize Go documentationJoe Tsai
Across all encoding packages, linkify declarations if possible. In some cases, we convert a code block into a bulleted list, which then further allows for more linkification. Change-Id: I68fedf362615b34228bab5d4859b7d87d831c570 Reviewed-on: https://go-review.googlesource.com/c/go/+/524977 LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Daniel Martí <mvdan@mvdan.cc> Reviewed-by: Ian Lance Taylor <iant@google.com> Reviewed-by: qiulaidongfeng <2645477756@qq.com> Reviewed-by: Matthew Dempsky <mdempsky@google.com>
2023-06-20encoding/binary: on invalid type return -1 from SizeIan Lance Taylor
Size is defined as returning -1 if the type is not fixed-size. Before this CL cases like Size((*[]int)(nil)) would crash. Fixes #60892 Change-Id: Iee8e20a0aee24b542b78cb4160c3b2c5a3eb02c2 Reviewed-on: https://go-review.googlesource.com/c/go/+/504575 Auto-Submit: Ian Lance Taylor <iant@golang.org> Run-TryBot: Ian Lance Taylor <iant@golang.org> Reviewed-by: Robert Griesemer <gri@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Bryan Mills <bcmills@google.com>
2023-04-04encoding/binary: add word size to the error message of the failed constraintConstantin Konstantinidis
Test added. Fixes #22860 Change-Id: I08304834a2b7b10b4ac729bf36761692eb4731da Reviewed-on: https://go-review.googlesource.com/c/go/+/113075 Reviewed-by: Emmanuel Odeke <emmanuel@orijtech.com> Reviewed-by: Ian Lance Taylor <iant@google.com> Run-TryBot: Ian Lance Taylor <iant@golang.org> Reviewed-by: Michael Knyszek <mknyszek@google.com> Auto-Submit: Ian Lance Taylor <iant@golang.org> TryBot-Result: Gopher Robot <gobot@golang.org>
2023-01-30encoding/binary: add String and GoString method to nativeEndiancuiweixie
Updates #57237 Change-Id: Ib626610130cae9c1d1aff5dd2a5035ffde0e127f Reviewed-on: https://go-review.googlesource.com/c/go/+/463985 Reviewed-by: Ian Lance Taylor <iant@google.com> Run-TryBot: xie cui <523516579@qq.com> TryBot-Result: Gopher Robot <gobot@golang.org> Run-TryBot: Ian Lance Taylor <iant@google.com> Auto-Submit: Ian Lance Taylor <iant@google.com> Reviewed-by: Michael Knyszek <mknyszek@google.com>
2022-03-02encoding/binary: add AppendByteOrderJoe Tsai
AppendByteOrder specifies new methods for LittleEndian and BigEndian for appending an unsigned integer to a byte slice. The performance of AppendXXX methods are slower than PutXXX methods since the former needs to do a few slice operations, while the latter is essentially a single integer store. In practice, existing usages of PutXXX performed slicing operations around the call such that this cost was present, regardless. name time/op PutUint16-24 0.48ns ± 2% AppendUint16-24 1.54ns ± 1% PutUint32-24 0.46ns ± 2% AppendUint32-24 0.89ns ± 1% PutUint64-24 0.46ns ± 2% AppendUint64-24 0.89ns ± 1% LittleEndianPutUint16-24 0.47ns ± 2% LittleEndianAppendUint16-24 1.54ns ± 1% LittleEndianPutUint32-24 0.45ns ± 3% LittleEndianAppendUint32-24 0.92ns ± 2% LittleEndianPutUint64-24 0.46ns ± 3% LittleEndianAppendUint64-24 0.95ns ± 4% Fixes #50601 Change-Id: I33d2bbc93a3ce01a9269feac33a2432bc1166ead Reviewed-on: https://go-review.googlesource.com/c/go/+/386017 Trust: Joseph Tsai <joetsai@digital-static.net> Trust: Josh Bleecher Snyder <josharian@gmail.com> Reviewed-by: Josh Bleecher Snyder <josharian@gmail.com> Run-TryBot: Joseph Tsai <joetsai@digital-static.net> TryBot-Result: Gopher Robot <gobot@golang.org>
2021-12-13all: gofmt -w -r 'interface{} -> any' srcRuss Cox
And then revert the bootstrap cmd directories and certain testdata. And adjust tests as needed. Not reverting the changes in std that are bootstrapped, because some of those changes would appear in API docs, and we want to use any consistently. Instead, rewrite 'any' to 'interface{}' in cmd/dist for those directories when preparing the bootstrap copy. A few files changed as a result of running gofmt -w not because of interface{} -> any but because they hadn't been updated for the new //go:build lines. Fixes #49884. Change-Id: Ie8045cba995f65bd79c694ec77a1b3d1fe01bb09 Reviewed-on: https://go-review.googlesource.com/c/go/+/368254 Trust: Russ Cox <rsc@golang.org> Run-TryBot: Russ Cox <rsc@golang.org> Reviewed-by: Robert Griesemer <gri@golang.org> TryBot-Result: Gopher Robot <gobot@golang.org>
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>
2020-09-23encoding/binary: remove TODO in Write and add benchmarkszhouzhongyuan
Benchmarks: goos: linux goarch: amd64 BenchmarkReadSlice1000Uint8s-8 4097088 296 ns/op 3381.06 MB/s BenchmarkWriteSlice1000Uint8s-8 4372588 271 ns/op 3694.96 MB/s Change-Id: I5b6ef0da5052e3381ee9c714bbff541c11ed0259 Reviewed-on: https://go-review.googlesource.com/c/go/+/246837 Run-TryBot: Alberto Donizetti <alb.donizetti@gmail.com> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Alberto Donizetti <alb.donizetti@gmail.com> Reviewed-by: Ian Lance Taylor <iant@golang.org> Trust: Alberto Donizetti <alb.donizetti@gmail.com>
2019-11-08encoding/binary: add float support to fast pathMartin Garton
This adds float type support to the main switch blocks in Read and Write, instead of falling back to reflection. This gives a considerable speedup for the float types: ReadFloats-8 129ns ± 9% 70ns ± 8% -46.02% (p=0.001 n=7+7) WriteFloats-8 131ns ± 6% 86ns ±11% -34.59% (p=0.001 n=7+7) ReadSlice1000Float32s-8 14.6µs ±14% 4.8µs ±12% -67.29% (p=0.001 n=7+7) WriteSlice1000Float32s-8 16.4µs ±20% 4.7µs ± 8% -71.01% (p=0.001 n=7+7) Change-Id: I0be99d068b07d10dd6eb1137b45eff6f7c216b87 GitHub-Last-Rev: 4ff326e99ca35977d819f0ba29c10d9efc7e811c GitHub-Pull-Request: golang/go#31803 Reviewed-on: https://go-review.googlesource.com/c/go/+/174959 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2019-11-08encoding/binary: make Read return an error when data is not a pointerUdalov Max
Make binary.Read return an error when passed `data` argument is not a pointer to a fixed-size value or a slice of fixed-size values. Fixes #32927 Change-Id: I04f48be55fe9b0cc66c983d152407d0e42cbcd95 Reviewed-on: https://go-review.googlesource.com/c/go/+/184957 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2019-11-01encoding/binary: cache struct sizes to speed up Read and WriteLorenz Bauer
A majority of work is spent in dataSize when en/decoding the same struct over and over again. This wastes a lot of work, since the result doesn't change for a given reflect.Value. Cache the result of the function for structs, so that subsequent calls to dataSize can avoid doing work. name old time/op new time/op delta ReadStruct 1.00µs ± 1% 0.37µs ± 1% -62.99% (p=0.029 n=4+4) WriteStruct 1.00µs ± 3% 0.37µs ± 1% -62.69% (p=0.008 n=5+5) name old speed new speed delta ReadStruct 75.1MB/s ± 1% 202.9MB/s ± 1% +170.16% (p=0.029 n=4+4) WriteStruct 74.8MB/s ± 3% 200.4MB/s ± 1% +167.96% (p=0.008 n=5+5) Fixes #34471 Change-Id: Ic5d987ca95f1197415ef93643a0af6fc1224fdf0 Reviewed-on: https://go-review.googlesource.com/c/go/+/199539 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org> Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
2018-09-04encoding/binary: simplify Read and WriteJosh Bleecher Snyder
There's no need to manually manage the backing slice for bs. Removing it simplifies the code, removes some allocations, and speeds it up slightly. Fixes #27403 name old time/op new time/op delta ReadSlice1000Int32s-8 6.39µs ± 1% 6.31µs ± 1% -1.37% (p=0.000 n=27+27) ReadStruct-8 1.25µs ± 2% 1.23µs ± 2% -1.06% (p=0.003 n=30+29) ReadInts-8 301ns ± 0% 297ns ± 1% -1.21% (p=0.000 n=27+30) WriteInts-8 325ns ± 1% 320ns ± 1% -1.59% (p=0.000 n=26+29) WriteSlice1000Int32s-8 6.60µs ± 0% 6.52µs ± 0% -1.23% (p=0.000 n=28+27) PutUint16-8 0.72ns ± 2% 0.71ns ± 2% ~ (p=0.286 n=30+30) PutUint32-8 0.71ns ± 1% 0.71ns ± 0% -0.42% (p=0.003 n=30+25) PutUint64-8 0.78ns ± 2% 0.78ns ± 0% -0.55% (p=0.001 n=30+27) LittleEndianPutUint16-8 0.57ns ± 0% 0.57ns ± 0% ~ (all equal) LittleEndianPutUint32-8 0.57ns ± 0% 0.57ns ± 0% ~ (all equal) LittleEndianPutUint64-8 0.57ns ± 0% 0.57ns ± 0% ~ (all equal) PutUvarint32-8 23.1ns ± 1% 23.1ns ± 1% ~ (p=0.925 n=26+29) PutUvarint64-8 57.5ns ± 2% 57.3ns ± 1% ~ (p=0.338 n=30+26) [Geo mean] 23.0ns 22.9ns -0.61% name old speed new speed delta ReadSlice1000Int32s-8 626MB/s ± 1% 634MB/s ± 1% +1.38% (p=0.000 n=27+27) ReadStruct-8 60.2MB/s ± 2% 60.8MB/s ± 2% +1.08% (p=0.002 n=30+29) ReadInts-8 100MB/s ± 1% 101MB/s ± 1% +1.24% (p=0.000 n=27+30) WriteInts-8 92.2MB/s ± 1% 93.6MB/s ± 1% +1.56% (p=0.000 n=26+29) WriteSlice1000Int32s-8 606MB/s ± 0% 614MB/s ± 0% +1.24% (p=0.000 n=28+27) PutUint16-8 2.80GB/s ± 1% 2.80GB/s ± 1% ~ (p=0.095 n=28+29) PutUint32-8 5.61GB/s ± 1% 5.62GB/s ± 1% ~ (p=0.069 n=27+28) PutUint64-8 10.2GB/s ± 1% 10.2GB/s ± 0% +0.15% (p=0.039 n=27+27) LittleEndianPutUint16-8 3.50GB/s ± 1% 3.50GB/s ± 1% ~ (p=0.552 n=30+29) LittleEndianPutUint32-8 7.01GB/s ± 1% 7.02GB/s ± 1% ~ (p=0.160 n=29+27) LittleEndianPutUint64-8 14.0GB/s ± 1% 14.0GB/s ± 1% ~ (p=0.413 n=29+29) PutUvarint32-8 174MB/s ± 1% 173MB/s ± 1% ~ (p=0.648 n=25+30) PutUvarint64-8 139MB/s ± 2% 140MB/s ± 1% ~ (p=0.271 n=30+26) [Geo mean] 906MB/s 911MB/s +0.55% name old alloc/op new alloc/op delta ReadSlice1000Int32s-8 4.14kB ± 0% 4.13kB ± 0% -0.19% (p=0.000 n=30+30) ReadStruct-8 200B ± 0% 200B ± 0% ~ (all equal) ReadInts-8 64.0B ± 0% 32.0B ± 0% -50.00% (p=0.000 n=30+30) WriteInts-8 112B ± 0% 64B ± 0% -42.86% (p=0.000 n=30+30) WriteSlice1000Int32s-8 4.14kB ± 0% 4.13kB ± 0% -0.19% (p=0.000 n=30+30) PutUint16-8 0.00B 0.00B ~ (all equal) PutUint32-8 0.00B 0.00B ~ (all equal) PutUint64-8 0.00B 0.00B ~ (all equal) LittleEndianPutUint16-8 0.00B 0.00B ~ (all equal) LittleEndianPutUint32-8 0.00B 0.00B ~ (all equal) LittleEndianPutUint64-8 0.00B 0.00B ~ (all equal) PutUvarint32-8 0.00B 0.00B ~ (all equal) PutUvarint64-8 0.00B 0.00B ~ (all equal) [Geo mean] 476B 370B -22.22% name old allocs/op new allocs/op delta ReadSlice1000Int32s-8 3.00 ± 0% 2.00 ± 0% -33.33% (p=0.000 n=30+30) ReadStruct-8 16.0 ± 0% 16.0 ± 0% ~ (all equal) ReadInts-8 8.00 ± 0% 8.00 ± 0% ~ (all equal) WriteInts-8 14.0 ± 0% 14.0 ± 0% ~ (all equal) WriteSlice1000Int32s-8 3.00 ± 0% 2.00 ± 0% -33.33% (p=0.000 n=30+30) PutUint16-8 0.00 0.00 ~ (all equal) PutUint32-8 0.00 0.00 ~ (all equal) PutUint64-8 0.00 0.00 ~ (all equal) LittleEndianPutUint16-8 0.00 0.00 ~ (all equal) LittleEndianPutUint32-8 0.00 0.00 ~ (all equal) LittleEndianPutUint64-8 0.00 0.00 ~ (all equal) PutUvarint32-8 0.00 0.00 ~ (all equal) PutUvarint64-8 0.00 0.00 ~ (all equal) [Geo mean] 6.94 5.90 -14.97% Change-Id: I3790b93e4190d98621d5f2c47e42929a18f56c2e Reviewed-on: https://go-review.googlesource.com/133135 Run-TryBot: Josh Bleecher Snyder <josharian@gmail.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2018-05-08encoding/binary: returns length of bool slice in intDataSizeJoe Kyo
intDataSize should return length of bool slice, so functions Read and Write can use the fast path to process bool slice. Change-Id: I8cd275e3ffea82024850662d86caca64bd91bf70 Reviewed-on: https://go-review.googlesource.com/112135 Reviewed-by: Ian Lance Taylor <iant@golang.org> Run-TryBot: Ian Lance Taylor <iant@golang.org>
2018-03-06encoding/binary: use an offset instead of slicingJosh Bleecher Snyder
While running make.bash, over 5% of all pointer writes come from encoding/binary doing struct reads. This change replaces slicing during such reads with an offset. This avoids updating the slice pointer with every struct field read or write. This has no impact when the write barrier is off. Running the benchmarks with GOGC=1, however, shows significant improvement: name old time/op new time/op delta ReadStruct-8 13.2µs ± 6% 10.1µs ± 5% -23.24% (p=0.000 n=10+10) name old speed new speed delta ReadStruct-8 5.69MB/s ± 6% 7.40MB/s ± 5% +30.18% (p=0.000 n=10+10) Change-Id: I22904263196bfeddc38abe8989428e263aee5253 Reviewed-on: https://go-review.googlesource.com/98757 Run-TryBot: Josh Bleecher Snyder <josharian@gmail.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org>
2017-06-29encoding/binary: clarify the repercussions for not following the docsBrad Fitzpatrick
Fixes #19794 Change-Id: I462cbc432fe9d4a9e6e79a9833b0013d82a0780e Reviewed-on: https://go-review.googlesource.com/47093 Reviewed-by: Ian Lance Taylor <iant@golang.org>
2016-12-01encoding/binary: document the new bool supportBrad Fitzpatrick
Updates #16856 Change-Id: I57af6b0c0d5ecdaf19cf6f969b05ec9ec03058f1 Reviewed-on: https://go-review.googlesource.com/33756 Reviewed-by: Ian Lance Taylor <iant@golang.org>
2016-09-28encoding/binary: add bool supportBlixt
This change adds support for decoding and encoding the bool type. The encoding is a single byte, with a zero value for false and a non-zero value for true. Closes #16856. Change-Id: I1d1114b320263691473bb100cad0f380e0204186 Reviewed-on: https://go-review.googlesource.com/28514 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org> Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
2016-04-15all: remove unnecessary type conversionsMatthew Dempsky
cmd and runtime were handled separately, and I'm intentionally skipped syscall. This is the rest of the standard library. CL generated mechanically with github.com/mdempsky/unconvert. Change-Id: I9e0eff886974dedc37adb93f602064b83e469122 Reviewed-on: https://go-review.googlesource.com/22104 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org> Run-TryBot: Matthew Dempsky <mdempsky@google.com> TryBot-Result: Gobot Gobot <gobot@golang.org>
2016-03-31cmd/compile: extend prove pass to handle constant comparisonsKeith Randall
Find comparisons to constants and propagate that information down the dominator tree. Use it to resolve other constant comparisons on the same variable. So if we know x >= 7, then a x > 4 condition must return true. This change allows us to use "_ = b[7]" hints to eliminate bounds checks. Fixes #14900 Change-Id: Idbf230bd5b7da43de3ecb48706e21cf01bf812f7 Reviewed-on: https://go-review.googlesource.com/21008 Reviewed-by: Alexandru Moșoi <alexandru@mosoi.ro>
2016-03-21encoding/binary: fix bound checkAlexandru Moșoi
The inserted early bound checks cause the slice to expand beyond the original length of the slice. Change-Id: Ib38891605f4a9a12d3b9e2071a5f77640b083d2d Reviewed-on: https://go-review.googlesource.com/20981 Reviewed-by: Keith Randall <khr@golang.org> Reviewed-by: Minux Ma <minux@golang.org>
2016-03-17encoding/binary: remove bound checks from conversions.Alexandru Moșoi
* This the simplest solution I could came up with that doesn't required changing the compiler. * The bound checks become constants now so they are removed during opt phase. Updates #14808 Change-Id: If32c33d7ec08bb400321b465015d152f0a5d3001 Reviewed-on: https://go-review.googlesource.com/20654 Reviewed-by: Russ Cox <rsc@golang.org> Run-TryBot: Alexandru Moșoi <alexandru@mosoi.ro> TryBot-Result: Gobot Gobot <gobot@golang.org>
2016-03-01all: make copyright headers consistent with one space after periodBrad Fitzpatrick
This is a subset of https://golang.org/cl/20022 with only the copyright header lines, so the next CL will be smaller and more reviewable. Go policy has been single space after periods in comments for some time. The copyright header template at: https://golang.org/doc/contribute.html#copyright also uses a single space. Make them all consistent. Change-Id: Icc26c6b8495c3820da6b171ca96a74701b4a01b0 Reviewed-on: https://go-review.googlesource.com/20111 Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org> Reviewed-by: Matthew Dempsky <mdempsky@google.com> TryBot-Result: Gobot Gobot <gobot@golang.org>
2015-09-30encoding/binary: document that Read returns io.EOF iff zero bytes are readJoe Tsai
Also add a unit test to lock this behavior into the API. Fixes #12016 Change-Id: Ib6ec6e7948f0705f3504ede9143b5dc4e790fc44 Reviewed-on: https://go-review.googlesource.com/15171 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org> Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
2015-06-29encoding/binary: update protobuf documentation linkDmitry Savintsev
Updated the protobuf documentation URL (code.google.com deprecated) to avoid a redirect. Change-Id: I134f6e4a2bf2bba699942883bf6347bc61700bcb Reviewed-on: https://go-review.googlesource.com/11634 Reviewed-by: Russ Cox <rsc@golang.org>
2015-03-14encoding/binary: cull dead codeJosh Bleecher Snyder
Change-Id: I91f9b5280e08e005f5a891aaa249267c211d814b Reviewed-on: https://go-review.googlesource.com/7592 Reviewed-by: Minux Ma <minux@golang.org>
2014-12-22encoding/binary: check for unsigned integers in intDataSize.Stan Schwertly
intDataSize ignores unsigned integers, forcing reads/writes to miss the fast path. Fixes #8956 Change-Id: Ie79b565b037db3c469aa1dc6d0a8a5a9252d5f0a Reviewed-on: https://go-review.googlesource.com/1777 Reviewed-by: Russ Cox <rsc@golang.org>
2014-10-02encoding/binary: fix error messageRobert Griesemer
In the process, simplified internal sizeOf and dataSize functions. Minor positive impact on performance. Added test case. benchmark old ns/op new ns/op delta BenchmarkReadSlice1000Int32s 14006 14122 +0.83% BenchmarkReadStruct 2508 2447 -2.43% BenchmarkReadInts 921 928 +0.76% BenchmarkWriteInts 2086 2081 -0.24% BenchmarkWriteSlice1000Int32s 13440 13497 +0.42% BenchmarkPutUvarint32 28.5 26.3 -7.72% BenchmarkPutUvarint64 81.3 76.7 -5.66% benchmark old MB/s new MB/s speedup BenchmarkReadSlice1000Int32s 285.58 283.24 0.99x BenchmarkReadStruct 27.90 28.60 1.03x BenchmarkReadInts 32.57 32.31 0.99x BenchmarkWriteInts 14.38 14.41 1.00x BenchmarkWriteSlice1000Int32s 297.60 296.36 1.00x BenchmarkPutUvarint32 140.55 151.92 1.08x BenchmarkPutUvarint64 98.36 104.33 1.06x Fixes #6818. LGTM=r R=r CC=golang-codereviews https://golang.org/cl/149290045
2014-10-01encoding/binary: slightly better documentationRobert Griesemer
Fixes #7306. LGTM=r R=r CC=golang-codereviews https://golang.org/cl/153820044
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.