1
0
mirror of https://github.com/golang/go synced 2024-09-29 18:44:30 -06:00
The Go programming language
Go to file
Joe Tsai cf26fbb1f6 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>
2022-08-23 20:29:22 +00:00
.github .github/ISSUE_TEMPLATE: add issue template for Go vulnerability management 2022-08-19 16:55:03 +00:00
api encoding/xml: add (*Encoder).Close 2022-08-23 18:24:30 +00:00
doc doc: move Go 1.19 release notes to x/website 2022-08-01 18:07:12 +00:00
lib/time lib/time, time/tzdata: update to 2022b 2022-08-11 20:03:19 +00:00
misc misc/cgo/test: disable setgid tests with musl 2022-08-22 14:48:53 +00:00
src strconv: optimize Parse for []byte arguments 2022-08-23 20:29:22 +00:00
test cmd/compile: handle partially overlapping assignments 2022-08-23 19:56:32 +00:00
.gitattributes all: treat all files as binary, but check in .bat with CRLF 2020-06-08 15:31:43 +00:00
.gitignore internal/buildcfg: move build configuration out of cmd/internal/objabi 2021-04-16 19:20:53 +00:00
codereview.cfg codereview.cfg: add codereview.cfg for master branch 2021-02-19 18:44:53 +00:00
CONTRIBUTING.md all: restore changes from faulty merge/revert 2018-02-12 20:13:59 +00:00
LICENSE
PATENTS
README.md README.md: update wiki link 2022-04-26 16:21:18 +00:00
SECURITY.md SECURITY.md: replace golang.org with go.dev 2022-04-26 19:59:47 +00:00

The Go Programming Language

Go is an open source programming language that makes it easy to build simple, reliable, and efficient software.

Gopher image 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.

Download and Install

Binary Distributions

Official binary distributions are available at https://go.dev/dl/.

After downloading a binary release, visit https://go.dev/doc/install for installation instructions.

Install From Source

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.

Contributing

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.