diff options
author | Brad Fitzpatrick <bradfitz@golang.org> | 2013-02-07 16:00:06 -0800 |
---|---|---|
committer | Brad Fitzpatrick <bradfitz@golang.org> | 2013-02-07 16:00:06 -0800 |
commit | 6ca691474f79339dabf5187d7675fd4f67fdc7ca (patch) | |
tree | cb402d96dbd1c7ab5932abbcb5c8d86ab49e2e64 /src/pkg/bytes | |
parent | bb30e1845d84adc58707e0fbabf706e9d67eddab (diff) | |
download | go-6ca691474f79339dabf5187d7675fd4f67fdc7ca.tar.gz |
bytes: minor optimization to lastIndexFunc
Before and after:
BenchmarkTrimSpace 20000000 81.3 ns/op
BenchmarkTrimSpace 50000000 58.0 ns/op
(most whitespace trimming is ASCII whitespace)
Same optimization appeared a handful of other places
in this file, but not here.
R=golang-dev, dave
CC=golang-dev
https://codereview.appspot.com/7305063
Diffstat (limited to 'src/pkg/bytes')
-rw-r--r-- | src/pkg/bytes/bytes.go | 5 | ||||
-rw-r--r-- | src/pkg/bytes/bytes_test.go | 7 |
2 files changed, 11 insertions, 1 deletions
diff --git a/src/pkg/bytes/bytes.go b/src/pkg/bytes/bytes.go index 31cf89ea8..6d40af443 100644 --- a/src/pkg/bytes/bytes.go +++ b/src/pkg/bytes/bytes.go @@ -571,7 +571,10 @@ func indexFunc(s []byte, f func(r rune) bool, truth bool) int { // inverted. func lastIndexFunc(s []byte, f func(r rune) bool, truth bool) int { for i := len(s); i > 0; { - r, size := utf8.DecodeLastRune(s[0:i]) + r, size := rune(s[i-1]), 1 + if r >= utf8.RuneSelf { + r, size = utf8.DecodeLastRune(s[0:i]) + } i -= size if f(r) == truth { return i diff --git a/src/pkg/bytes/bytes_test.go b/src/pkg/bytes/bytes_test.go index 1d073b143..1d6274c33 100644 --- a/src/pkg/bytes/bytes_test.go +++ b/src/pkg/bytes/bytes_test.go @@ -1073,3 +1073,10 @@ func BenchmarkFieldsFunc(b *testing.B) { FieldsFunc(fieldsInput, unicode.IsSpace) } } + +func BenchmarkTrimSpace(b *testing.B) { + s := []byte(" Some text. \n") + for i := 0; i < b.N; i++ { + TrimSpace(s) + } +} |