Skip to content

Commit

Permalink
Calculate litMatcher.want at codegen time (#95)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
philandstuff authored Feb 24, 2020
1 parent 7ee56e1 commit 18953b2
Show file tree
Hide file tree
Showing 39 changed files with 480 additions and 266 deletions.
78 changes: 71 additions & 7 deletions bootstrap/cmd/bootstrap-pigeon/bootstrap_pigeon.go

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,11 @@ func (b *builder) writeLitMatcher(lit *ast.LitMatcher) {
b.writelnf("\tval: %q,", lit.Val)
}
b.writelnf("\tignoreCase: %t,", lit.IgnoreCase)
ignoreCaseFlag := ""
if lit.IgnoreCase {
ignoreCaseFlag = "i"
}
b.writelnf("\twant: %q,", strconv.Quote(lit.Val)+ignoreCaseFlag)
b.writelnf("},")
}

Expand Down
10 changes: 3 additions & 7 deletions builder/generated_static_code.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 3 additions & 7 deletions builder/static_code.go
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,7 @@ type litMatcher struct {
pos position
val string
ignoreCase bool
want string
}

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

// {{ end }} ==template==
ignoreCase := ""
if lit.ignoreCase {
ignoreCase = "i"
}
val := string(strconv.AppendQuote([]byte{}, lit.val)) + ignoreCase // wrap 'lit.val' with double quotes
start := p.pt
for _, want := range lit.val {
cur := p.pt.rn
if lit.ignoreCase {
cur = unicode.ToLower(cur)
}
if cur != want {
p.failAt(false, start.position, val)
p.failAt(false, start.position, lit.want)
p.restore(start)
return nil, false
}
p.read()
}
p.failAt(true, start.position, val)
p.failAt(true, start.position, lit.want)
return p.sliceFrom(start), true
}

Expand Down
17 changes: 10 additions & 7 deletions examples/calculator/calculator.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 15 additions & 7 deletions examples/indentation/indentation.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 18953b2

Please sign in to comment.