commit | bec12f153a2ad1b0902857bc1dafc635fc5dc06a | [log] [tgz] |
---|---|---|
author | Aoang <aoang@x2oe.com> | Sat Nov 23 21:19:50 2024 +0800 |
committer | Jonathan Amsterdam <jba@google.com> | Wed Mar 12 09:53:59 2025 -0700 |
tree | b4e51c3de72488649ab4c877d4e694160c2f2b96 | |
parent | 7e8ceadf85d1b0fc177ef0754f779a7a87b2c2b9 [diff] |
log/slog: optimize appendKey to reduce allocations This change introduces a new method, `appendTwoStrings`, which optimizes the `appendKey` function by avoiding the allocation of a temporary string (string concatenation of prefix and key). Instead, it directly appends the prefix and key to the buffer. Additionally, added `BenchmarkAppendKey` benchmark tests to validate performance improvements. This change improves performance in cases where large prefixes are used, as verified by the following benchmarks: goos: darwin goarch: arm64 pkg: log/slog cpu: Apple M1 Max │ old.out │ new.out │ │ sec/op │ sec/op vs base │ AppendKey/prefix_size_5-10 44.41n ± 0% 35.62n ± 0% -19.80% (p=0.000 n=10) AppendKey/prefix_size_10-10 48.17n ± 0% 39.12n ± 0% -18.80% (p=0.000 n=10) AppendKey/prefix_size_30-10 84.50n ± 0% 62.30n ± 0% -26.28% (p=0.000 n=10) AppendKey/prefix_size_50-10 124.9n ± 0% 102.3n ± 0% -18.09% (p=0.000 n=10) AppendKey/prefix_size_100-10 203.6n ± 1% 168.7n ± 0% -17.14% (p=0.000 n=10) geomean 85.61n 68.41n -20.09% │ old.out │ new.out │ │ B/op │ B/op vs base │ AppendKey/prefix_size_5-10 0.000 ± 0% 0.000 ± 0% ~ (p=1.000 n=10) ¹ AppendKey/prefix_size_10-10 0.000 ± 0% 0.000 ± 0% ~ (p=1.000 n=10) ¹ AppendKey/prefix_size_30-10 48.00 ± 0% 0.00 ± 0% -100.00% (p=0.000 n=10) AppendKey/prefix_size_50-10 128.00 ± 0% 64.00 ± 0% -50.00% (p=0.000 n=10) AppendKey/prefix_size_100-10 224.0 ± 0% 112.0 ± 0% -50.00% (p=0.000 n=10) geomean ² ? ² ³ ¹ all samples are equal ² summaries must be >0 to compute geomean ³ ratios must be >0 to compute geomean │ old.out │ new.out │ │ allocs/op │ allocs/op vs base │ AppendKey/prefix_size_5-10 0.000 ± 0% 0.000 ± 0% ~ (p=1.000 n=10) ¹ AppendKey/prefix_size_10-10 0.000 ± 0% 0.000 ± 0% ~ (p=1.000 n=10) ¹ AppendKey/prefix_size_30-10 1.000 ± 0% 0.000 ± 0% -100.00% (p=0.000 n=10) AppendKey/prefix_size_50-10 2.000 ± 0% 1.000 ± 0% -50.00% (p=0.000 n=10) AppendKey/prefix_size_100-10 2.000 ± 0% 1.000 ± 0% -50.00% (p=0.000 n=10) geomean ² ? ² ³ ¹ all samples are equal ² summaries must be >0 to compute geomean ³ ratios must be >0 to compute geomean This patch improves performance without altering the external behavior of the `slog` package. Change-Id: I8b47718de522196f06e0ddac48af73e352d2e5cd Reviewed-on: https://go-review.googlesource.com/c/go/+/631415 Reviewed-by: Alan Donovan <adonovan@google.com> Reviewed-by: Jonathan Amsterdam <jba@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.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 4.0 Attribution 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.