diff options
author | M. J. Fromberger <michael.j.fromberger@gmail.com> | 2019-06-03 09:25:40 -0700 |
---|---|---|
committer | M. J. Fromberger <michael.j.fromberger@gmail.com> | 2019-06-03 09:29:07 -0700 |
commit | 1425d3176896932e270efd687dae93e38e557160 (patch) | |
tree | ef2af7c16684f7c30ab43633496605cccbfec960 | |
parent | c36b5a63b8369f08149a9f5986fcc5a8fa51babe (diff) | |
download | go-creachadair-shell-1425d3176896932e270efd687dae93e38e557160.tar.gz |
Replace classOf with a lookup table.
Improve performance of byte classification using a static lookup table. The
improvement over a function is not as great as the gain from using a function
over a map, but it's enough to be worthwhile.
```
BENCHMARK BEFORE AFTER SPEEDUP (%)
BenchmarkSplit/len_1-12 570 563 1.2
BenchmarkSplit/len_4-12 612 600 2.0
BenchmarkSplit/len_16-12 714 704 1.4
BenchmarkSplit/len_64-12 1119 1082 3.3
BenchmarkSplit/len_256-12 2965 2799 5.6
BenchmarkSplit/len_1024-12 10216 8928 12.6
BenchmarkSplit/len_4096-12 39333 33102 15.8
BenchmarkSplit/len_16384-12 158126 130540 17.4
BenchmarkSplit/len_65536-12 669554 566011 15.5
BenchmarkSplit/len_100739-12 1039795 884622 14.9
```
-rw-r--r-- | shell.go | 28 |
1 files changed, 10 insertions, 18 deletions
@@ -52,12 +52,12 @@ const ( type class int const ( - clBreak class = iota + clOther class = iota + clBreak clNewline clQuote clSingle clDouble - clOther ) type action int @@ -140,21 +140,13 @@ var update = [...][]struct { }, } -func classOf(b byte) class { - switch b { - case ' ', '\t': - return clBreak - case '\n': - return clNewline - case '\\': - return clQuote - case '\'': - return clSingle - case '"': - return clDouble - default: - return clOther - } +var classOf = [256]class{ + ' ': clBreak, + '\t': clBreak, + '\n': clNewline, + '\\': clQuote, + '\'': clSingle, + '"': clDouble, } // A Scanner partitions input from a reader into tokens divided on space, tab, @@ -190,7 +182,7 @@ func (s *Scanner) Next() bool { } else if err != nil { return false } - next := update[s.st][classOf(c)] + next := update[s.st][classOf[c]] s.st = next.state switch next.action { case push: |