[go] bytes: add Clone function

6 views
Skip to first unread message

jiahua wang (Gerrit)

unread,
Mar 13, 2022, 10:48:38 AM3/13/22
to goph...@pubsubhelper.golang.org, jiahua wang, golang-co...@googlegroups.com

jiahua wang has uploaded this change for review.

View Change

bytes: add Clone function

Fixes: #45038
Change-Id: Ia7408cba419022da69cc1953c29274cccdd13213
---
M src/bytes/bytes.go
M src/bytes/bytes_test.go
2 files changed, 67 insertions(+), 0 deletions(-)

diff --git a/src/bytes/bytes.go b/src/bytes/bytes.go
index 41323ad..f01a0f3 100644
--- a/src/bytes/bytes.go
+++ b/src/bytes/bytes.go
@@ -27,6 +27,16 @@
return bytealg.Compare(a, b)
}

+// Clone returns a fresh copy of a.
+func Clone(a []byte) []byte {
+ if a == nil || len(a) == 0 {
+ return nil
+ }
+ b := make([]byte, len(a))
+ copy(b, a)
+ return b
+}
+
// explode splits s into a slice of UTF-8 sequences, one per Unicode code point (still slices of bytes),
// up to a maximum of n byte slices. Invalid UTF-8 sequences are chopped into individual bytes.
func explode(s []byte, n int) [][]byte {
diff --git a/src/bytes/bytes_test.go b/src/bytes/bytes_test.go
index 3bece6a..15f488f 100644
--- a/src/bytes/bytes_test.go
+++ b/src/bytes/bytes_test.go
@@ -14,6 +14,7 @@
"testing"
"unicode"
"unicode/utf8"
+ "unsafe"
)

func eq(a, b []string) bool {
@@ -117,6 +118,36 @@
}
}

+var emptyByte []byte = nil
+
+func TestClone(t *testing.T) {
+ var cloneTests = [][]byte{
+ nil,
+ []byte{},
+ []byte("short"),
+ []byte(strings.Repeat("a", 42)),
+ []byte(strings.Repeat("a", 42))[:0],
+ []byte(strings.Repeat("a", 42))[:0:0],
+ }
+ for _, input := range cloneTests {
+ clone := Clone(input)
+ if !Equal(input, clone) {
+ t.Errorf("Clone(%q) = %q; want %q", input, clone, input)
+ }
+
+ inputHeader := (*reflect.SliceHeader)(unsafe.Pointer(&input))
+ cloneHeader := (*reflect.SliceHeader)(unsafe.Pointer(&clone))
+ if input != nil && cloneHeader.Data == inputHeader.Data {
+ t.Errorf("Clone(%q) return value should not reference inputs backing memory.", input)
+ }
+
+ emptyHeader := (*reflect.SliceHeader)(unsafe.Pointer(&emptyByte))
+ if input == nil && cloneHeader.Data != emptyHeader.Data {
+ t.Errorf("Clone(%#v) return value should be equal to empty string.", inputHeader)
+ }
+ }
+}
+
var indexTests = []BinOpTest{
{"", "", 0},
{"", "a", -1},
@@ -2009,3 +2040,19 @@
})
}
}
+
+func BenchmarkClone(b *testing.B) {
+ var a = []byte(strings.Repeat("a", 42))
+ b.ReportAllocs()
+ for i := 0; i < b.N; i++ {
+ Clone(a)
+ }
+}
+
+// func BenchmarkClone(b *testing.B) {
+// var str = strings.Repeat("a", 42)
+// b.ReportAllocs()
+// for i := 0; i < b.N; i++ {
+// stringSink = strings.Clone(str)
+// }
+// }

To view, visit change 392194. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: Ia7408cba419022da69cc1953c29274cccdd13213
Gerrit-Change-Number: 392194
Gerrit-PatchSet: 1
Gerrit-Owner: jiahua wang <wjh1...@gmail.com>
Gerrit-MessageType: newchange

Martin Möhrmann (Gerrit)

unread,
Mar 13, 2022, 11:59:05 AM3/13/22
to jiahua wang, goph...@pubsubhelper.golang.org, Ian Lance Taylor, Brad Fitzpatrick, Gopher Robot, golang-co...@googlegroups.com

Attention is currently required from: jiahua wang, Brad Fitzpatrick, Ian Lance Taylor.

Patch set 1:Code-Review -1

View Change

1 comment:

To view, visit change 392194. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: Ia7408cba419022da69cc1953c29274cccdd13213
Gerrit-Change-Number: 392194
Gerrit-PatchSet: 1
Gerrit-Owner: jiahua wang <wjh1...@gmail.com>
Gerrit-Reviewer: Brad Fitzpatrick <brad...@golang.org>
Gerrit-Reviewer: Ian Lance Taylor <ia...@golang.org>
Gerrit-Reviewer: Martin Möhrmann <mar...@golang.org>
Gerrit-CC: Gopher Robot <go...@golang.org>
Gerrit-Attention: jiahua wang <wjh1...@gmail.com>
Gerrit-Attention: Brad Fitzpatrick <brad...@golang.org>
Gerrit-Attention: Ian Lance Taylor <ia...@golang.org>
Gerrit-Comment-Date: Sun, 13 Mar 2022 15:58:59 +0000
Gerrit-HasComments: Yes
Gerrit-Has-Labels: Yes
Gerrit-MessageType: comment

Martin Möhrmann (Gerrit)

unread,
Mar 13, 2022, 12:11:31 PM3/13/22
to jiahua wang, goph...@pubsubhelper.golang.org, Ian Lance Taylor, Brad Fitzpatrick, Gopher Robot, golang-co...@googlegroups.com

Attention is currently required from: jiahua wang, Brad Fitzpatrick, Ian Lance Taylor.

View Change

2 comments:


    • // var str = strings.Repeat("a", 42)

    • // 	b.ReportAllocs()
      // for i := 0; i < b.N; i++ {
      // stringSink = strings.Clone(str)
      // }
      // }

      We should not leave uncommented code around.

To view, visit change 392194. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: Ia7408cba419022da69cc1953c29274cccdd13213
Gerrit-Change-Number: 392194
Gerrit-PatchSet: 1
Gerrit-Owner: jiahua wang <wjh1...@gmail.com>
Gerrit-Reviewer: Brad Fitzpatrick <brad...@golang.org>
Gerrit-Reviewer: Ian Lance Taylor <ia...@golang.org>
Gerrit-Reviewer: Martin Möhrmann <mar...@golang.org>
Gerrit-CC: Gopher Robot <go...@golang.org>
Gerrit-Attention: jiahua wang <wjh1...@gmail.com>
Gerrit-Attention: Brad Fitzpatrick <brad...@golang.org>
Gerrit-Attention: Ian Lance Taylor <ia...@golang.org>
Gerrit-Comment-Date: Sun, 13 Mar 2022 16:11:26 +0000
Gerrit-HasComments: Yes
Gerrit-Has-Labels: No
Gerrit-MessageType: comment

Martin Möhrmann (Gerrit)

unread,
Mar 13, 2022, 12:11:44 PM3/13/22
to jiahua wang, goph...@pubsubhelper.golang.org, Ian Lance Taylor, Brad Fitzpatrick, Gopher Robot, golang-co...@googlegroups.com

Attention is currently required from: jiahua wang, Brad Fitzpatrick, Ian Lance Taylor.

View Change

1 comment:

To view, visit change 392194. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: Ia7408cba419022da69cc1953c29274cccdd13213
Gerrit-Change-Number: 392194
Gerrit-PatchSet: 1
Gerrit-Owner: jiahua wang <wjh1...@gmail.com>
Gerrit-Reviewer: Brad Fitzpatrick <brad...@golang.org>
Gerrit-Reviewer: Ian Lance Taylor <ia...@golang.org>
Gerrit-Reviewer: Martin Möhrmann <mar...@golang.org>
Gerrit-CC: Gopher Robot <go...@golang.org>
Gerrit-Attention: jiahua wang <wjh1...@gmail.com>
Gerrit-Attention: Brad Fitzpatrick <brad...@golang.org>
Gerrit-Attention: Ian Lance Taylor <ia...@golang.org>
Gerrit-Comment-Date: Sun, 13 Mar 2022 16:11:39 +0000
Gerrit-HasComments: Yes
Gerrit-Has-Labels: No
Comment-In-Reply-To: Martin Möhrmann <mar...@golang.org>
Gerrit-MessageType: comment
Reply all
Reply to author
Forward
0 new messages