Skip to content

Commit

Permalink
Introduce a sync.Pool for ANTLR Lexer / Parser instances (#348)
Browse files Browse the repository at this point in the history
* Introduce a sync.Pool for ANTLR Parser instances
* Introduce sync.Pool instances for the Lexer as well.

Note, the change brings in github.com/antlr/antlr4/pulls/2816 which updates the
ANTLR Go runtime. Other applications using vendored code, or alternate sources
of truth besides the main ANTLR GitHub repository will also need to be udpated
for this change to work.
  • Loading branch information
TristonianJones authored May 6, 2020
1 parent abf6094 commit f3a4721
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 8 deletions.
2 changes: 1 addition & 1 deletion WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ go_repository(

go_repository(
name = "com_github_antlr",
tag = "4.7.2",
commit = "621b933c7a7f01c67ae9de15103151fa0f9d6d90",
importpath = "github.com/antlr/antlr4",
)

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/google/cel-go
go 1.12

require (
github.com/antlr/antlr4 v0.0.0-20190819145818-b43a4c3a8015
github.com/antlr/antlr4 v0.0.0-20200503195918-621b933c7a7f
github.com/golang/protobuf v1.3.4
github.com/google/cel-spec v0.4.0
golang.org/x/text v0.3.2
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ cloud.google.com/go v0.26.0 h1:e0WKqKTd5BnrG8aKH3J3h+QvEIQtSUcf2n5UZ5ZgLtQ=
cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/antlr/antlr4 v0.0.0-20190819145818-b43a4c3a8015 h1:StuiJFxQUsxSCzcby6NFZRdEhPkXD5vxN7TZ4MD6T84=
github.com/antlr/antlr4 v0.0.0-20190819145818-b43a4c3a8015/go.mod h1:T7PbCXFs94rrTttyxjbyT5+/1V8T2TYDejxUfHJjw1Y=
github.com/antlr/antlr4 v0.0.0-20200503195918-621b933c7a7f h1:0cEys61Sr2hUBEXfNV8eyQP01oZuBgoMeHunebPirK8=
github.com/antlr/antlr4 v0.0.0-20200503195918-621b933c7a7f/go.mod h1:T7PbCXFs94rrTttyxjbyT5+/1V8T2TYDejxUfHJjw1Y=
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
github.com/client9/misspell v0.3.4 h1:ta993UF76GwbvJcIo3Y68y/M3WxlpEHPWIGDkJYwzJI=
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
Expand Down
27 changes: 23 additions & 4 deletions parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package parser
import (
"fmt"
"strconv"
"sync"

"github.com/antlr/antlr4/runtime/Go/antlr"

Expand Down Expand Up @@ -69,12 +70,30 @@ type parser struct {
macros map[string]Macro
}

var _ gen.CELVisitor = (*parser)(nil)
var (
_ gen.CELVisitor = (*parser)(nil)

lexerPool *sync.Pool = &sync.Pool{
New: func() interface{} {
return gen.NewCELLexer(nil)
},
}

parserPool *sync.Pool = &sync.Pool{
New: func() interface{} {
return gen.NewCELParser(nil)
},
}
)

func (p *parser) parse(expression string) *exprpb.Expr {
stream := antlr.NewInputStream(expression)
lexer := gen.NewCELLexer(stream)
prsr := gen.NewCELParser(antlr.NewCommonTokenStream(lexer, 0))
lexer := lexerPool.Get().(*gen.CELLexer)
lexer.SetInputStream(antlr.NewInputStream(expression))
defer lexerPool.Put(lexer)

prsr := parserPool.Get().(*gen.CELParser)
prsr.SetInputStream(antlr.NewCommonTokenStream(lexer, 0))
defer parserPool.Put(prsr)

lexer.RemoveErrorListeners()
prsr.RemoveErrorListeners()
Expand Down

0 comments on commit f3a4721

Please sign in to comment.