]> Cypherpunks repositories - gostls13.git/commitdiff
bytes: add test to ensure shallow copy of NewBuffer does not allocate
authorJoe Tsai <joetsai@digital-static.net>
Tue, 23 Apr 2024 22:42:22 +0000 (15:42 -0700)
committerGopher Robot <gobot@golang.org>
Wed, 24 Apr 2024 13:36:51 +0000 (13:36 +0000)
At present, there is no API to reset the underlying []byte
of an existing Buffer struct, except to shallow copy
the entire Buffer struct.

Updates #67004

Change-Id: I08998f7a95ae5bde0897d86247d47f23cd784583
Reviewed-on: https://go-review.googlesource.com/c/go/+/581297
Auto-Submit: Joseph Tsai <joetsai@digital-static.net>
Reviewed-by: Joedian Reid <joedian@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Keith Randall <khr@google.com>
Run-TryBot: Joseph Tsai <joetsai@digital-static.net>
Reviewed-by: Keith Randall <khr@golang.org>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>

src/bytes/buffer_test.go

index 845e5e2209e53e7090f08a29e443593422c4b8fb..322e7367c768115464f69f3e9c3c6092d394a914 100644 (file)
@@ -94,6 +94,21 @@ func TestNewBuffer(t *testing.T) {
        check(t, "NewBuffer", buf, testString)
 }
 
+var buf Buffer
+
+// Calling NewBuffer and immediately shallow copying the Buffer struct
+// should not result in any allocations.
+// This can be used to reset the underlying []byte of an existing Buffer.
+func TestNewBufferShallow(t *testing.T) {
+       n := testing.AllocsPerRun(1000, func() {
+               buf = *NewBuffer(testBytes)
+       })
+       if n > 0 {
+               t.Errorf("allocations occurred while shallow copying")
+       }
+       check(t, "NewBuffer", &buf, testString)
+}
+
 func TestNewBufferString(t *testing.T) {
        buf := NewBufferString(testString)
        check(t, "NewBufferString", buf, testString)