bcd44b61d3
When one has a []byte on hand, but desires to call the Parse function, the conversion from []byte to string would allocate. This occurs frequently through UnmarshalText and UnmarshalJSON. 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 Parse(layout, string(input)) not require an allocation for the input string. This optimization works well for most RFC3339 timestamps. All timestamps with second resolution (e.g., 2000-01-01T00:00:00Z or 2000-01-01T00:00:00+23:59) or timestamps with nanosecond resolution in UTC (e.g., 2000-01-01T00:00:00.123456789Z) are less than 32B and benefit from this optimization. Unfortunately, nanosecond timestamps with non-UTC timezones (e.g., 2000-01-01T00:00:00.123456789+23:59) do not benefit since they are 35B long. Previously, this was not possible since the input leaked to the error and calls to FixedZone with the zone name, which causes the prover to give up and heap copy the []byte. We fix this by copying the input string in both cases. The advantage of this change is that you can now call Parse with a []byte without allocating (most of the times). The detriment is that the timezone and error path has an extra allocation. Handling of timezones were already expensive (3 allocations and 160B allocated), so the additional cost of another string allocation is relatively minor. We should optimize for the common case, rather than the exceptional case. Performance: name old time/op new time/op delta ParseRFC3339UTCBytes 54.4ns ± 1% 40.3ns ± 1% -25.91% (p=0.000 n=9+10) Now that parsing of RFC3339 has been heavily optimized in CL 425197, the performance gains by this optimization becomes relatively more notable. Related to CL 345488. Change-Id: I2a8a9cd6354b3bd46c2f57818ed2646a2e485f36 Reviewed-on: https://go-review.googlesource.com/c/go/+/429862 Reviewed-by: Ian Lance Taylor <iant@google.com> Reviewed-by: Cherry Mui <cherryyz@google.com> Run-TryBot: Joseph Tsai <joetsai@digital-static.net> TryBot-Result: Gopher Robot <gobot@golang.org> |
||
---|---|---|
.github | ||
api | ||
doc | ||
lib/time | ||
misc | ||
src | ||
test | ||
.gitattributes | ||
.gitignore | ||
codereview.cfg | ||
CONTRIBUTING.md | ||
LICENSE | ||
PATENTS | ||
README.md | ||
SECURITY.md |
The Go Programming Language
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.
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.