0
0
Fork 0

Skip already lowercased runes on transformation.

The LowerCaseFilter works on the original slice to avoid allocations,
so skipping already lowercased runes avoids unnecessary work.

benchmark                      old ns/op     new ns/op     delta
BenchmarkLowerCaseFilter-8     1302          815           -37.40%
This commit is contained in:
Michael Nitschinger 2016-10-11 11:49:48 +02:00
parent de0c26718d
commit ff35d75aa4
1 changed files with 11 additions and 1 deletions

View File

@ -64,8 +64,18 @@ func toLowerDeferredCopy(s []byte) []byte {
if r >= utf8.RuneSelf {
r, wid = utf8.DecodeRune(s[i:])
}
l := unicode.ToLower(r)
lwid := utf8.RuneLen(l)
// If the rune is already lowercased, just move to the
// next rune.
if l == r {
i += wid
j += wid
continue
}
lwid := utf8.RuneLen(l)
if lwid > wid {
// utf-8 encoded replacement is wider
// for now, punt and defer