Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bytes, strings: optimize Cut for single-byte separators #67125

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/bytes/bytes.go
Original file line number Diff line number Diff line change
Expand Up @@ -1330,6 +1330,12 @@ func Index(s, sep []byte) int {
//
// Cut returns slices of the original slice s, not copies.
func Cut(s, sep []byte) (before, after []byte, found bool) {
if len(sep) == 1 {
if i := IndexByte(s, sep[0]); i >= 0 {
return s[:i], s[i+1:], true
}
return s, nil, false
}
if i := Index(s, sep); i >= 0 {
return s[:i], s[i+len(sep):], true
}
Expand Down
28 changes: 28 additions & 0 deletions src/bytes/bytes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2263,3 +2263,31 @@ func TestClone(t *testing.T) {
}
}
}

func BenchmarkCut(b *testing.B) {
b.ReportAllocs()

for _, skip := range [...]int{2, 4, 8, 16, 32, 64} {
s := Repeat(append(append(Repeat([]byte(" "), skip), 'a', 'a'), Repeat([]byte(" "), skip)...), 1<<16/skip)
b.Run(fmt.Sprintf("Cut-One/%d", skip), func(b *testing.B) {
for range b.N {
_, _, _ = Cut(s, []byte{'a'})
}
})
b.Run(fmt.Sprintf("Cut-Two/%d", skip), func(b *testing.B) {
for range b.N {
_, _, _ = Cut(s, []byte{'a', 'a'})
}
})
b.Run(fmt.Sprintf("Cut-One-Nil/%d", skip), func(b *testing.B) {
for range b.N {
_, _, _ = Cut(s, []byte{'c'})
}
})
b.Run(fmt.Sprintf("Cut-Two-Nil/%d", skip), func(b *testing.B) {
for range b.N {
_, _, _ = Cut(s, []byte{'c', 'c'})
}
})
}
}
6 changes: 6 additions & 0 deletions src/internal/stringslite/strings.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,12 @@ func Index(s, substr string) int {
}

func Cut(s, sep string) (before, after string, found bool) {
if len(sep) == 1 {
if i := IndexByte(s, sep[0]); i >= 0 {
return s[:i], s[i+1:], true
}
return s, "", false
}
if i := Index(s, sep); i >= 0 {
return s[:i], s[i+len(sep):], true
}
Expand Down
28 changes: 28 additions & 0 deletions src/strings/strings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2071,3 +2071,31 @@ func BenchmarkReplaceAll(b *testing.B) {
stringSink = ReplaceAll("banana", "a", "<>")
}
}

func BenchmarkCut(b *testing.B) {
b.ReportAllocs()

for _, skip := range [...]int{2, 4, 8, 16, 32, 64} {
s := Repeat(Repeat(" ", skip)+"aa"+Repeat(" ", skip), 1<<16/skip)
b.Run(fmt.Sprintf("Cut-One/%d", skip), func(b *testing.B) {
for range b.N {
_, _, _ = Cut(s, "a")
}
})
b.Run(fmt.Sprintf("Cut-Two/%d", skip), func(b *testing.B) {
for range b.N {
_, _, _ = Cut(s, "aa")
}
})
b.Run(fmt.Sprintf("Cut-One-Nil/%d", skip), func(b *testing.B) {
for range b.N {
_, _, _ = Cut(s, "c")
}
})
b.Run(fmt.Sprintf("Cut-Two-Nil/%d", skip), func(b *testing.B) {
for range b.N {
_, _, _ = Cut(s, "cc")
}
})
}
}