change isWhitespace to table lookup
$ repo/android.sh kati -c -kati_cpuprofile kati.prof
from (pprof) top30
before:
560ms 2.88% 43.88% 560ms 2.88% main.isWhitespace
440ms 2.26% 51.28% 940ms 4.83% main.(*wordScanner).Scan
after:
540ms 2.87% 40.45% 540ms 2.87% main.(*wordScanner).Scan
diff --git a/strutil.go b/strutil.go
index 08ed549..57d5545 100644
--- a/strutil.go
+++ b/strutil.go
@@ -6,13 +6,14 @@
"strings"
)
+var wsbytes = [256]bool{' ': true, '\t': true, '\n': true, '\r': true}
+
// TODO(ukai): use unicode.IsSpace?
func isWhitespace(ch rune) bool {
- switch ch {
- case ' ', '\t', '\n', '\r':
- return true
+ if int(ch) >= len(wsbytes) {
+ return false
}
- return false
+ return wsbytes[ch]
}
func splitSpaces(s string) []string {
@@ -73,8 +74,7 @@
func (ws *wordScanner) Scan() bool {
for ws.s = ws.i; ws.s < len(ws.in); ws.s++ {
- ch := rune(ws.in[ws.s])
- if !isWhitespace(ch) {
+ if !wsbytes[ws.in[ws.s]] {
break
}
}
@@ -82,8 +82,7 @@
return false
}
for ws.i = ws.s; ws.i < len(ws.in); ws.i++ {
- ch := rune(ws.in[ws.i])
- if isWhitespace(ch) {
+ if wsbytes[ws.in[ws.i]] {
break
}
}