Skip to content

Commit 5bdc026

Browse files
committed
allkeywords: replace switch with map in GetKeywordID
Long time ago in 74f630c we switched the implementation of keyword ID lookup in lexer from the map to a large generated switch. At that time it was more performant, but with the new swiss table implementation of hash maps in Go I was curious to see whether the map approach is now faster, and it turns out to be so, so this commit switches back to the map lookup. ``` name old time/op new time/op delta Parse/simple-8 12.4µs ± 0% 12.1µs ± 0% -2.05% (p=0.000 n=8+9) Parse/string-8 18.2µs ± 0% 18.0µs ± 0% -1.24% (p=0.000 n=10+9) Parse/tpcc-delivery-8 25.0µs ± 0% 24.5µs ± 0% -1.73% (p=0.000 n=10+10) Parse/account-8 32.9µs ± 0% 32.2µs ± 0% -2.14% (p=0.000 n=10+10) name old alloc/op new alloc/op delta Parse/simple-8 3.56kB ± 0% 3.56kB ± 0% ~ (all equal) Parse/string-8 3.83kB ± 0% 3.83kB ± 0% ~ (all equal) Parse/tpcc-delivery-8 6.14kB ± 0% 6.14kB ± 0% ~ (all equal) Parse/account-8 11.2kB ± 0% 11.2kB ± 0% ~ (all equal) name old allocs/op new allocs/op delta Parse/simple-8 22.0 ± 0% 22.0 ± 0% ~ (all equal) Parse/string-8 26.0 ± 0% 26.0 ± 0% ~ (all equal) Parse/tpcc-delivery-8 41.0 ± 0% 41.0 ± 0% ~ (all equal) Parse/account-8 75.0 ± 0% 75.0 ± 0% ~ (all equal) ``` Release note: None
1 parent e670091 commit 5bdc026

File tree

3 files changed

+16
-9
lines changed

3 files changed

+16
-9
lines changed

pkg/sql/lexbase/allkeywords/main.go

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -107,18 +107,21 @@ var KeywordNames = []string{
107107
{{end -}}
108108
}
109109
110+
var keywordID = map[string]int32{
111+
{{range . -}}
112+
"{{.Keyword}}": {{.Ident}},
113+
{{end -}}
114+
}
115+
110116
// GetKeywordID returns the lex id of the SQL keyword k or IDENT if k is
111117
// not a keyword.
118+
//
119+
//gcassert:inline
112120
func GetKeywordID(k string) int32 {
113-
// The previous implementation generated a map that did a string ->
114-
// id lookup. Various ideas were benchmarked and the implementation below
115-
// was the fastest of those, between 3% and 10% faster (at parsing, so the
116-
// scanning speedup is even more) than the map implementation.
117-
switch k {
118-
{{range . -}}
119-
case "{{.Keyword}}": return {{.Ident}}
120-
{{end -}}
121-
default: return IDENT
121+
id, ok := keywordID[k]
122+
if !ok {
123+
return IDENT
122124
}
125+
return id
123126
}
124127
`

pkg/testutils/lint/gcassert_paths.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ sql/colexec/colexecspan
2323
sql/colexec/colexecwindow
2424
sql/colfetcher
2525
sql/execinfrapb
26+
sql/lexbase
2627
sql/opt
28+
sql/plpgsql/parser/lexbase
2729
sql/row
2830
sql/sem/tree
2931
storage
@@ -33,6 +35,7 @@ util
3335
util/admission
3436
util/hlc
3537
util/intsets
38+
util/jsonpath/parser/lexbase
3639
util/mon
3740
util/num32
3841
util/vector

pkg/testutils/lint/lint_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2396,6 +2396,7 @@ func TestLint(t *testing.T) {
23962396
filter,
23972397
stream.GrepNot("sql/colexec/execgen/cmd/execgen/*"),
23982398
stream.GrepNot("sql/colexec/execgen/testdata/*"),
2399+
stream.GrepNot("sql/lexbase/allkeywords/main.go"),
23992400
stream.GrepNot("testutils/lint/lint_test.go"),
24002401
), func(s string) {
24012402
// s here is of the form

0 commit comments

Comments
 (0)