commit | cf26fbb1f6d9644f447342f42d2dddcbe9ceda61 | [log] [tgz] |
---|---|---|
author | Joe Tsai <joetsai@digital-static.net> | Thu Aug 26 12:02:47 2021 -0700 |
committer | Gopher Robot <gobot@golang.org> | Tue Aug 23 20:29:22 2022 +0000 |
tree | 6ad6439a21b17e5440bcad637332ee0273cd7a09 | |
parent | 70de482d17863e501be44450f8e60500a6f1b459 [diff] |
strconv: optimize Parse for []byte arguments When one has a []byte on hand, but desires to call the Parse functions, the conversion from []byte to string would allocate. var b []byte = ... v, err := strconv.ParseXXX(string(b), ...) This changes it such that the input string never escapes from any of the Parse functions. Together with the compiler optimization where the compiler stack allocates any string smaller than 32B this makes most valid inputs for strconv.ParseXXX(string(b), ...) not require an allocation for the input string. For example, the longest int64 or uint64 encoded in decimal is 20B. Also, the longest decimal formatting of a float64 in appendix B of RFC 8785 is 25B. Previously, this was not possible since the input leaked to the error, which causes the prover to give up and instead heap copy the []byte. We fix this by copying the input string in the error case. The advantage of this change is that you can now call strconv.ParseXXX with a []byte without allocations (most times) in the non-error case. The detriment is that the error-case now has an extra allocation. We should optimize for the non-error path, rather than the error path. The effects of this change is transitively seen through packages that must use strconv.ParseXXX on a []byte such as "encoding/json": name old time/op new time/op delta UnmarshalFloat64 186ns 157ns -15.89% (p=0.000 n=10+10) name old alloc/op new alloc/op delta UnmarshalFloat64 148B 144B -2.70% (p=0.000 n=10+10) name old allocs/op new allocs/op delta UnmarshalFloat64 2.00 1.00 -50.00% (p=0.000 n=10+10) In order for "encoding/json" to benefit, there needs to be a small change made to how "encoding/json" calls strconv.ParseXXX. That will be a future change. Credit goes to Jeff Wendling for a similar patch. Fixes #42429 Change-Id: I512d6927f965f82e95bd7ec14a28a587f23b7203 Reviewed-on: https://go-review.googlesource.com/c/go/+/345488 Reviewed-by: Martin Möhrmann <martin@golang.org> Run-TryBot: Joseph Tsai <joetsai@digital-static.net> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org> Auto-Submit: Joseph Tsai <joetsai@digital-static.net> Reviewed-by: Robert Griesemer <gri@golang.org> Reviewed-by: David Chase <drchase@google.com> Reviewed-by: Ian Lance Taylor <iant@google.com>
Go is an open source programming language that makes it easy to build simple, reliable, and efficient software.
Gopher image by Renee French, licensed under Creative Commons 3.0 Attributions license.
Our canonical Git repository is located at https://go.googlesource.com/go. There is a mirror of the repository at https://github.com/golang/go.
Unless otherwise noted, the Go source files are distributed under the BSD-style license found in the LICENSE file.
Official binary distributions are available at https://go.dev/dl/.
After downloading a binary release, visit https://go.dev/doc/install for installation instructions.
If a binary distribution is not available for your combination of operating system and architecture, visit https://go.dev/doc/install/source for source installation instructions.
Go is the work of thousands of contributors. We appreciate your help!
To contribute, please read the contribution guidelines at https://go.dev/doc/contribute.
Note that the Go project uses the issue tracker for bug reports and proposals only. See https://go.dev/wiki/Questions for a list of places to ask questions about the Go language.