Skip to content

perf(filter): Speed up expression String methods #414

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 34 additions & 4 deletions pkg/filter/ql/expr.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@

package ql

import "fmt"
import (
"strings"
)

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

// String returns a string representation of the parenthesized expression.
func (e *ParenExpr) String() string { return fmt.Sprintf("(%s)", e.Expr.String()) }
func (e *ParenExpr) String() string {
var b strings.Builder
b.Grow(len(e.Expr.String()) + 2)
b.WriteRune('(')
b.WriteString(e.Expr.String())
b.WriteRune(')')
return b.String()
}

// BinaryExpr represents an operation between two expressions.
type BinaryExpr struct {
Expand All @@ -47,7 +56,21 @@ type BinaryExpr struct {

// String returns a string representation of the binary expression.
func (e *BinaryExpr) String() string {
return fmt.Sprintf("%s %s %s", e.LHS.String(), e.Op.String(), e.RHS.String())
var b strings.Builder

lhs := e.LHS.String()
op := e.Op.String()
rhs := e.RHS.String()

b.Grow(len(lhs) + len(op) + len(rhs) + 2)

b.WriteString(lhs)
b.WriteString(" ")
b.WriteString(op)
b.WriteString(" ")
b.WriteString(rhs)

return b.String()
}

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

// String returns a string representation of the not expression.
func (e *NotExpr) String() string { return fmt.Sprintf("(%s)", e.Expr.String()) }
func (e *NotExpr) String() string {
var b strings.Builder
b.Grow(len(e.Expr.String()) + 2)
b.WriteRune('(')
b.WriteString(e.Expr.String())
b.WriteRune(')')
return b.String()
}
41 changes: 26 additions & 15 deletions pkg/filter/ql/literal.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@
package ql

import (
"bytes"
"fmt"
"github.com/rabbitstack/fibratus/pkg/filter/fields"
"github.com/rabbitstack/fibratus/pkg/kevent"
"github.com/rabbitstack/fibratus/pkg/kevent/ktypes"
Expand Down Expand Up @@ -129,16 +127,25 @@ type ListLiteral struct {

// String returns a string representation of the literal.
func (s *ListLiteral) String() string {
var buf bytes.Buffer
_, _ = buf.WriteString("(")
for idx, tagKey := range s.Values {
var n int
for _, elem := range s.Values {
n += len(elem) + 2
}

var b strings.Builder
b.Grow(n + 2)
b.WriteRune('(')

for idx, elem := range s.Values {
if idx != 0 {
_, _ = buf.WriteString(", ")
b.WriteString(", ")
}
_, _ = buf.WriteString(tagKey)
b.WriteString(elem)
}
_, _ = buf.WriteString(")")
return buf.String()

b.WriteRune(')')

return b.String()
}

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

// String returns a string representation of the call.
func (f *Function) String() string {
// join arguments.
var str []string
for _, arg := range f.Args {
str = append(str, arg.String())
}
args := strings.Join(f.ArgsSlice(), ", ")

var b strings.Builder
b.Grow(len(args) + len(f.Name) + 2)

b.WriteString(f.Name)
b.WriteRune('(')
b.WriteString(args)
b.WriteRune(')')

// Write function name and args.
return fmt.Sprintf("%s(%s)", f.Name, strings.Join(str, ", "))
return b.String()
}

// validate ensures that the function name obtained
Expand Down
Loading