From f3a472121b19f1042707024dc5b3283c2e3dfb68 Mon Sep 17 00:00:00 2001 From: Tristan Swadell Date: Wed, 6 May 2020 15:11:19 -0700 Subject: [PATCH] Introduce a sync.Pool for ANTLR Lexer / Parser instances (#348) * 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. --- WORKSPACE | 2 +- go.mod | 2 +- go.sum | 4 ++-- parser/parser.go | 27 +++++++++++++++++++++++---- 4 files changed, 27 insertions(+), 8 deletions(-) diff --git a/WORKSPACE b/WORKSPACE index 2ce39e33..cf2b0c78 100644 --- a/WORKSPACE +++ b/WORKSPACE @@ -30,7 +30,7 @@ go_repository( go_repository( name = "com_github_antlr", - tag = "4.7.2", + commit = "621b933c7a7f01c67ae9de15103151fa0f9d6d90", importpath = "github.com/antlr/antlr4", ) diff --git a/go.mod b/go.mod index 6775194e..a8aa7d9e 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index 53729cec..aef46ed5 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/parser/parser.go b/parser/parser.go index 48165fd4..7ee90fa9 100644 --- a/parser/parser.go +++ b/parser/parser.go @@ -19,6 +19,7 @@ package parser import ( "fmt" "strconv" + "sync" "github.com/antlr/antlr4/runtime/Go/antlr" @@ -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()