strings: make IndexRune faster

re-implement IndexRune by Index which is well optimized to get
performance gain.

name                   old time/op  new time/op  delta
IndexRune-4            30.2ns ± 1%  28.3ns ± 1%   -6.22%  (p=0.000 n=20+19)
IndexRuneLongString-4   156ns ± 1%    49ns ± 1%  -68.72%  (p=0.000 n=19+19)
IndexRuneFastPath-4    10.6ns ± 2%  10.0ns ± 1%   -6.30%  (p=0.000 n=18+18)

Change-Id: Ie663b8f7860ca51892dd4be182fca3caa5f8ae61
Reviewed-on: https://go-review.googlesource.com/28546
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
This commit is contained in:
Hiroshi Ioka 2016-09-06 20:23:40 +09:00 committed by Brad Fitzpatrick
parent a4bdd64555
commit 8737dac1f2
3 changed files with 30 additions and 9 deletions

View file

@ -176,17 +176,11 @@ func LastIndex(s, sep string) int {
// IndexRune returns the index of the first instance of the Unicode code point
// r, or -1 if rune is not present in s.
func IndexRune(s string, r rune) int {
switch {
case r < utf8.RuneSelf:
if r < utf8.RuneSelf {
return IndexByte(s, byte(r))
default:
for i, c := range s {
if c == r {
return i
}
}
}
return -1
return Index(s, string(r))
}
// IndexAny returns the index of the first instance of any Unicode code point