]> Cypherpunks repositories - gostls13.git/commitdiff
strings: make use of sizeclasses in (*Builder).Grow
authorMateusz Poliwczak <mpoliwczak34@gmail.com>
Fri, 2 Feb 2024 15:54:24 +0000 (15:54 +0000)
committerKeith Randall <khr@golang.org>
Mon, 19 Feb 2024 19:51:15 +0000 (19:51 +0000)
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>

src/bytes/bytes.go
src/internal/bytealg/bytealg.go
src/runtime/slice.go
src/strings/builder.go
src/strings/builder_test.go

index 0679b43a20a3b1cc66c7a192f8c63ecb593633f5..1871814c6e00d5c953444772d17516ab4ccd438b 100644 (file)
@@ -525,7 +525,7 @@ func Join(s [][]byte, sep []byte) []byte {
                n += len(v)
        }
 
-       b := bytealg.MakeNoZero(n)
+       b := bytealg.MakeNoZero(n)[:n:n]
        bp := copy(b, s[0])
        for _, v := range s[1:] {
                bp += copy(b[bp:], sep)
@@ -610,7 +610,7 @@ func Repeat(b []byte, count int) []byte {
                        chunkMax = len(b)
                }
        }
-       nb := bytealg.MakeNoZero(n)
+       nb := bytealg.MakeNoZero(n)[:n:n]
        bp := copy(nb, b)
        for bp < n {
                chunk := bp
@@ -640,7 +640,7 @@ func ToUpper(s []byte) []byte {
                        // Just return a copy.
                        return append([]byte(""), s...)
                }
-               b := bytealg.MakeNoZero(len(s))
+               b := bytealg.MakeNoZero(len(s))[:len(s):len(s)]
                for i := 0; i < len(s); i++ {
                        c := s[i]
                        if 'a' <= c && c <= 'z' {
@@ -670,7 +670,7 @@ func ToLower(s []byte) []byte {
                if !hasUpper {
                        return append([]byte(""), s...)
                }
-               b := bytealg.MakeNoZero(len(s))
+               b := bytealg.MakeNoZero(len(s))[:len(s):len(s)]
                for i := 0; i < len(s); i++ {
                        c := s[i]
                        if 'A' <= c && c <= 'Z' {
index 1103891eeefeb77ad9ae655642d24df9b7b3ddc7..6b79a2e1fabb901de3c7602bfddfabd2dce7189d 100644 (file)
@@ -111,7 +111,8 @@ func LastIndexRabinKarp[T string | []byte](s, sep T) int {
        return -1
 }
 
-// MakeNoZero makes a slice of length and capacity n without zeroing the bytes.
+// MakeNoZero makes a slice of length n and capacity of at least n Bytes
+// without zeroing the bytes (including the bytes between len and cap).
 // It is the caller's responsibility to ensure uninitialized bytes
 // do not leak to the end user.
 func MakeNoZero(n int) []byte
index eb628bb1694c4ec77ed932f04f577c8f24b406e7..8c1023c1e87a6c0cd5da32a0247ba375cf285e27 100644 (file)
@@ -366,5 +366,6 @@ func bytealg_MakeNoZero(len int) []byte {
        if uintptr(len) > maxAlloc {
                panicmakeslicelen()
        }
-       return unsafe.Slice((*byte)(mallocgc(uintptr(len), nil, false)), len)
+       cap := roundupsize(uintptr(len), true)
+       return unsafe.Slice((*byte)(mallocgc(uintptr(cap), nil, false)), cap)[:len]
 }
index 189dadb1e7f47c69156f3b5a8857317ff3167cdb..7c9b686241c02f1c3eb6a11190266e4253c39312 100644 (file)
@@ -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
index c3c627ee7d8b1a1a6025c901a42ffc8516972eb8..36fd7a77e3abaa23283adeaf44aac5a098bba5e6 100644 (file)
@@ -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)
+       }
+}