Skip to content

Commit 48dae44

Browse files
authored
feat: improve syntax parser for pattern (#12489)
1 parent 3e6b946 commit 48dae44

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

‎pkg/logql/log/pattern/parser.go

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package pattern
22

3-
import "fmt"
3+
import (
4+
"fmt"
5+
"sync"
6+
)
47

58
const underscore = "_"
69

@@ -10,6 +13,19 @@ var tokens = map[int]string{
1013
UNDERSCORE: underscore,
1114
}
1215

16+
type parser struct {
17+
p *exprParserImpl
18+
}
19+
20+
var parserPool = sync.Pool{
21+
New: func() interface{} {
22+
p := &parser{
23+
p: &exprParserImpl{},
24+
}
25+
return p
26+
},
27+
}
28+
1329
func init() {
1430
// Improve the error messages coming out of yacc.
1531
exprErrorVerbose = true
@@ -23,9 +39,13 @@ func parseExpr(input string) (expr, error) {
2339
}
2440

2541
func parseExprBytes(input []byte) (expr, error) {
42+
p := parserPool.Get().(*parser)
43+
defer parserPool.Put(p)
44+
2645
l := newLexer()
2746
l.setData(input)
28-
e := exprNewParser().Parse(l)
47+
48+
e := p.p.Parse(l)
2949
if e != 0 || len(l.errs) > 0 {
3050
return nil, l.errs[0]
3151
}

‎pkg/logql/log/pattern/parser_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,14 @@ func Test_Parse(t *testing.T) {
5757
require.Equal(t, tc.expected, actual)
5858
}
5959
}
60+
61+
var result expr
62+
63+
func BenchmarkParseExpr(b *testing.B) {
64+
var err error
65+
b.ReportAllocs()
66+
for i := 0; i < b.N; i++ {
67+
result, err = parseExpr(`level=info <_> caller=main.go:107 msg="Starting Grafana Enterprise Traces" version="version=weekly-r138-f1920489, branch=weekly-r138, revision=f1920489"`)
68+
}
69+
require.NoError(b, err)
70+
}

0 commit comments

Comments
 (0)