Skip to content

Commit 18953b2

Browse files
authored
Calculate litMatcher.want at codegen time (#95)
parseLitMatcher() was calling strconv.AppendQuote() each time, which my profiler discovered was costing a measurably large amount of runtime during parsing within dhall-golang. This commit calculates litMatcher.want at codegen time so that we don't have to compute the `want` value each time we parse with the litMatcher.
1 parent 7ee56e1 commit 18953b2

File tree

39 files changed

+480
-266
lines changed

39 files changed

+480
-266
lines changed

bootstrap/cmd/bootstrap-pigeon/bootstrap_pigeon.go

Lines changed: 71 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

builder/builder.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,11 @@ func (b *builder) writeLitMatcher(lit *ast.LitMatcher) {
422422
b.writelnf("\tval: %q,", lit.Val)
423423
}
424424
b.writelnf("\tignoreCase: %t,", lit.IgnoreCase)
425+
ignoreCaseFlag := ""
426+
if lit.IgnoreCase {
427+
ignoreCaseFlag = "i"
428+
}
429+
b.writelnf("\twant: %q,", strconv.Quote(lit.Val)+ignoreCaseFlag)
425430
b.writelnf("},")
426431
}
427432

builder/generated_static_code.go

Lines changed: 3 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

builder/static_code.go

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,7 @@ type litMatcher struct {
357357
pos position
358358
val string
359359
ignoreCase bool
360+
want string
360361
}
361362

362363
//{{ if .Nolint }} nolint: structcheck {{else}} ==template== {{ end }}
@@ -1245,25 +1246,20 @@ func (p *parser) parseLitMatcher(lit *litMatcher) (interface{}, bool) {
12451246
}
12461247

12471248
// {{ end }} ==template==
1248-
ignoreCase := ""
1249-
if lit.ignoreCase {
1250-
ignoreCase = "i"
1251-
}
1252-
val := string(strconv.AppendQuote([]byte{}, lit.val)) + ignoreCase // wrap 'lit.val' with double quotes
12531249
start := p.pt
12541250
for _, want := range lit.val {
12551251
cur := p.pt.rn
12561252
if lit.ignoreCase {
12571253
cur = unicode.ToLower(cur)
12581254
}
12591255
if cur != want {
1260-
p.failAt(false, start.position, val)
1256+
p.failAt(false, start.position, lit.want)
12611257
p.restore(start)
12621258
return nil, false
12631259
}
12641260
p.read()
12651261
}
1266-
p.failAt(true, start.position, val)
1262+
p.failAt(true, start.position, lit.want)
12671263
return p.sliceFrom(start), true
12681264
}
12691265

examples/calculator/calculator.go

Lines changed: 10 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/indentation/indentation.go

Lines changed: 15 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)