Skip to content

Commit 625f944

Browse files
committed
perf(filter): Speed up expression String methods
Use the StringBuilder for a more efficient string concatenation.
1 parent 94d5bcf commit 625f944

File tree

2 files changed

+47
-12
lines changed

2 files changed

+47
-12
lines changed

pkg/filter/ql/expr.go

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818

1919
package ql
2020

21-
import "fmt"
21+
import (
22+
"strings"
23+
)
2224

2325
// Node represents a node in the abstract syntax tree.
2426
type Node interface {
@@ -36,7 +38,14 @@ type ParenExpr struct {
3638
}
3739

3840
// String returns a string representation of the parenthesized expression.
39-
func (e *ParenExpr) String() string { return fmt.Sprintf("(%s)", e.Expr.String()) }
41+
func (e *ParenExpr) String() string {
42+
var b strings.Builder
43+
b.Grow(len(e.Expr.String()) + 2)
44+
b.WriteString("(")
45+
b.WriteString(e.Expr.String())
46+
b.WriteString(")")
47+
return b.String()
48+
}
4049

4150
// BinaryExpr represents an operation between two expressions.
4251
type BinaryExpr struct {
@@ -47,7 +56,20 @@ type BinaryExpr struct {
4756

4857
// String returns a string representation of the binary expression.
4958
func (e *BinaryExpr) String() string {
50-
return fmt.Sprintf("%s %s %s", e.LHS.String(), e.Op.String(), e.RHS.String())
59+
var b strings.Builder
60+
61+
lhs := e.LHS.String()
62+
op := e.Op.String()
63+
rhs := e.RHS.String()
64+
65+
b.Grow(len(lhs) + len(op) + len(rhs) + 2)
66+
b.WriteString(lhs)
67+
b.WriteString(" ")
68+
b.WriteString(op)
69+
b.WriteString(" ")
70+
b.WriteString(rhs)
71+
72+
return b.String()
5173
}
5274

5375
// NotExpr represents an unary not expression.
@@ -56,4 +78,11 @@ type NotExpr struct {
5678
}
5779

5880
// String returns a string representation of the not expression.
59-
func (e *NotExpr) String() string { return fmt.Sprintf("(%s)", e.Expr.String()) }
81+
func (e *NotExpr) String() string {
82+
var b strings.Builder
83+
b.Grow(len(e.Expr.String()) + 2)
84+
b.WriteString("(")
85+
b.WriteString(e.Expr.String())
86+
b.WriteString(")")
87+
return b.String()
88+
}

pkg/filter/ql/literal.go

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
package ql
2020

2121
import (
22-
"bytes"
2322
"fmt"
2423
"github.com/rabbitstack/fibratus/pkg/filter/fields"
2524
"github.com/rabbitstack/fibratus/pkg/kevent"
@@ -129,16 +128,23 @@ type ListLiteral struct {
129128

130129
// String returns a string representation of the literal.
131130
func (s *ListLiteral) String() string {
132-
var buf bytes.Buffer
133-
_, _ = buf.WriteString("(")
134-
for idx, tagKey := range s.Values {
131+
var n int
132+
for _, elem := range s.Values {
133+
n += len(elem) + 2
134+
}
135+
136+
var b strings.Builder
137+
b.Grow(n + 2)
138+
b.WriteString("(")
139+
for idx, elem := range s.Values {
135140
if idx != 0 {
136-
_, _ = buf.WriteString(", ")
141+
b.WriteString(", ")
137142
}
138-
_, _ = buf.WriteString(tagKey)
143+
b.WriteString(elem)
139144
}
140-
_, _ = buf.WriteString(")")
141-
return buf.String()
145+
b.WriteString(")")
146+
147+
return b.String()
142148
}
143149

144150
// Function represents a function call.

0 commit comments

Comments
 (0)