diff options
| author | Mateusz Poliwczak <mpoliwczak34@gmail.com> | 2024-02-02 15:54:24 +0000 |
|---|---|---|
| committer | Keith Randall <khr@golang.org> | 2024-02-19 19:51:15 +0000 |
| commit | 968b71bce4489dd201c5247c6142a830d90a1ee1 (patch) | |
| tree | c9b44e6c18c8a2ca7910f804d786e569aabfc072 /src/strings | |
| parent | cf52e709977d331a70df9463cf9e307024b6779f (diff) | |
| download | go-968b71bce4489dd201c5247c6142a830d90a1ee1.tar.xz | |
strings: make use of sizeclasses in (*Builder).Grow
Fixes #64833
Change-Id: Ice3f5dfab65f5525bc7a6f57ddeaabda8d64dfa3
GitHub-Last-Rev: 38f1d6c19d8ec29ae5645ce677839a301f798df3
GitHub-Pull-Request: golang/go#64835
Reviewed-on: https://go-review.googlesource.com/c/go/+/552135
Reviewed-by: Keith Randall <khr@google.com>
Reviewed-by: Cherry Mui <cherryyz@google.com>
Reviewed-by: Keith Randall <khr@golang.org>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Diffstat (limited to 'src/strings')
| -rw-r--r-- | src/strings/builder.go | 6 | ||||
| -rw-r--r-- | src/strings/builder_test.go | 13 |
2 files changed, 18 insertions, 1 deletions
diff --git a/src/strings/builder.go b/src/strings/builder.go index 189dadb1e7..7c9b686241 100644 --- a/src/strings/builder.go +++ b/src/strings/builder.go @@ -15,7 +15,11 @@ import ( // Do not copy a non-zero Builder. type Builder struct { addr *Builder // of receiver, to detect copies by value - buf []byte + + // External users should never get direct access to this buffer, since + // the slice at some point will be converted to a string using unsafe, also + // data between len(buf) and cap(buf) might be uninitialized. + buf []byte } // noescape hides a pointer from escape analysis. It is the identity function diff --git a/src/strings/builder_test.go b/src/strings/builder_test.go index c3c627ee7d..36fd7a77e3 100644 --- a/src/strings/builder_test.go +++ b/src/strings/builder_test.go @@ -385,3 +385,16 @@ func BenchmarkBuildString_ByteBuffer(b *testing.B) { } }) } + +func TestBuilderGrowSizeclasses(t *testing.T) { + s := Repeat("a", 19) + allocs := testing.AllocsPerRun(100, func() { + var b Builder + b.Grow(18) + b.WriteString(s) + _ = b.String() + }) + if allocs > 1 { + t.Fatalf("unexpected amount of allocations: %v, want: 1", allocs) + } +} |
