go: bytes: IndexAny is slower than a naive implementation in some cases
What version of Go are you using (go version
)?
$ go version go version go1.20.3 windows/amd64
What operating system and processor architecture are you using (go env
)?
go env
Output
$ go env set GO111MODULE= set GOARCH=amd64 set GOBIN= set GOCACHE=C:\Users\Kroum\AppData\Local\go-build set GOENV=C:\Users\Kroum\AppData\Roaming\go\env set GOEXE=.exe set GOEXPERIMENT= set GOFLAGS= set GOHOSTARCH=amd64 set GOHOSTOS=windows set GOINSECURE= set GOMODCACHE=D:\MyPrograms\golibs\pkg\mod set GONOPROXY= set GONOSUMDB= set GOOS=windows set GOPATH=D:\MyPrograms\golibs set GOPRIVATE= set GOPROXY=https://proxy.golang.org,direct set GOROOT=C:\Program Files\Go set GOSUMDB=sum.golang.org set GOTMPDIR= set GOTOOLDIR=C:\Program Files\Go\pkg\tool\windows_amd64 set GOVCS= set GOVERSION=go1.20.3 set GCCGO=gccgo set GOAMD64=v1 set AR=ar set CC=gcc set CXX=g++ set CGO_ENABLED=1 set GOMOD=D:\MyDocuments\Programming\go\test\bench\go.mod set GOWORK= set CGO_CFLAGS=-O2 -g set CGO_CPPFLAGS= set CGO_CXXFLAGS=-O2 -g set CGO_FFLAGS=-O2 -g set CGO_LDFLAGS=-O2 -g set PKG_CONFIG=pkg-config set GOGCCFLAGS=-m64 -mthreads -Wl,--no-gc-sections -fmessage-length=0 -fdebug-prefix-map=D:\Temp\System\Kroum\go-build2837998563=/tmp/go-build -gno-record-gcc-switches
What did you do?
I consider the following naive implementation of bytes.IndexAny
func IndexAny(s []byte, chars string) int {
min := -1
for _, c := range chars {
if i := bytes.IndexRune(s, c); uint(i) < uint(min) {
min = i
}
}
return min
}
I benchmark it for 10 delimiters on a data where the first delimiter appears at position 1000, like this
package indexany
import (
"bytes"
"strings"
"testing"
)
func IndexAny(s []byte, chars string) int {
min := -1
for _, c := range chars {
if i := bytes.IndexRune(s, c); uint(i) < uint(min) {
min = i
}
}
return min
}
const (
t = "🚆"
firstchar = 1000
numdelimiters = 10
)
var (
data []byte
separators string
)
// create the data and the separators fot the benchmark
func init() {
// data = "xxx...🚆"
data = bytes.Repeat([]byte("x"), firstchar)
data = append(data, t...)
// separators = "yyy...🚆"
separators = strings.Repeat("y", numdelimiters)
separators = separators + t
}
func BenchmarkNaiveIndexAny(b *testing.B) {
for i := 0; i < b.N; i++ {
IndexAny(data, separators)
}
}
func BenchmarkBytesIndexAny(b *testing.B) {
for i := 0; i < b.N; i++ {
bytes.IndexAny(data, separators)
}
}
What did you expect to see?
That bytes.IndexAny
is faster then this “naive” IndexAny
.
What did you see instead?
$ go test -benchmem -bench=. .
goos: windows
goarch: amd64
pkg: kpym.github.com/gobench/indexany
cpu: Intel(R) Core(TM) i7-7700HQ CPU @ 2.80GHz
BenchmarkNaiveIndexAny-8 3033126 522.9 ns/op 0 B/op 0 allocs/op
BenchmarkBytesIndexAny-8 223227 7233 ns/op 0 B/op 0 allocs/op
PASS
ok kpym.github.com/gobench/indexany 4.325s
So this “naive” implementation is more than 10 times faster than the standard bytes.IndexAny
.
Am I doing something wrong ? Is this relevant ?
Remark : If we take a lot of delimiters (1000) the standard function is 30% better (on my computer).
About this issue
- Original URL
- State: closed
- Created a year ago
- Comments: 15 (9 by maintainers)
+1, thanks for the investigation!
@kpym Thanks for the investigation.
FTR, we try to aggregate more realistic benchmarks in
golang.org/x/benchmarks
. These run continuously in CI (https://perf.golang.org/dashboard). If you write a benchmark that is executable withgo get
, we can consider referencing it in bentcmd/bent
(https://cs.opensource.google/go/x/benchmarks/+/master:cmd/bent/configs/suites.toml). If you have an even larger, more complex text processing benchmark, it might be worth adding tosweet
.It sounds like there might not be anything left to do in this issue? Closing for now, please let me know if you disagree!