Skip to content

Commit 2d13f51

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

File tree

2 files changed

+60
-19
lines changed

2 files changed

+60
-19
lines changed

pkg/filter/ql/expr.go

Lines changed: 34 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.WriteRune('(')
45+
b.WriteString(e.Expr.String())
46+
b.WriteRune(')')
47+
return b.String()
48+
}
4049

4150
// BinaryExpr represents an operation between two expressions.
4251
type BinaryExpr struct {
@@ -47,7 +56,21 @@ 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+
67+
b.WriteString(lhs)
68+
b.WriteString(" ")
69+
b.WriteString(op)
70+
b.WriteString(" ")
71+
b.WriteString(rhs)
72+
73+
return b.String()
5174
}
5275

5376
// NotExpr represents an unary not expression.
@@ -56,4 +79,11 @@ type NotExpr struct {
5679
}
5780

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

pkg/filter/ql/literal.go

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

2121
import (
22-
"bytes"
23-
"fmt"
2422
"github.com/rabbitstack/fibratus/pkg/filter/fields"
2523
"github.com/rabbitstack/fibratus/pkg/kevent"
2624
"github.com/rabbitstack/fibratus/pkg/kevent/ktypes"
@@ -129,16 +127,25 @@ type ListLiteral struct {
129127

130128
// String returns a string representation of the literal.
131129
func (s *ListLiteral) String() string {
132-
var buf bytes.Buffer
133-
_, _ = buf.WriteString("(")
134-
for idx, tagKey := range s.Values {
130+
var n int
131+
for _, elem := range s.Values {
132+
n += len(elem) + 2
133+
}
134+
135+
var b strings.Builder
136+
b.Grow(n + 2)
137+
b.WriteRune('(')
138+
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+
146+
b.WriteRune(')')
147+
148+
return b.String()
142149
}
143150

144151
// Function represents a function call.
@@ -158,14 +165,18 @@ func (f *Function) ArgsSlice() []string {
158165

159166
// String returns a string representation of the call.
160167
func (f *Function) String() string {
161-
// join arguments.
162-
var str []string
163-
for _, arg := range f.Args {
164-
str = append(str, arg.String())
165-
}
168+
args := strings.Join(f.ArgsSlice(), ", ")
169+
170+
var b strings.Builder
171+
b.Grow(len(args) + len(f.Name) + 2)
172+
173+
b.WriteString(f.Name)
174+
b.WriteRune('(')
175+
b.WriteString(args)
176+
b.WriteRune(')')
166177

167178
// Write function name and args.
168-
return fmt.Sprintf("%s(%s)", f.Name, strings.Join(str, ", "))
179+
return b.String()
169180
}
170181

171182
// validate ensures that the function name obtained

0 commit comments

Comments
 (0)