Skip to content

Commit

Permalink
Convert string to bytes once only when doing string filtering. (#1405)
Browse files Browse the repository at this point in the history
This makes it slightly faster:

name              old time/op    new time/op    delta
ContainsFilter-4    18.2ns ± 2%     9.8ns ± 1%  -46.20%  (p=0.000 n=10+8)

name              old alloc/op   new alloc/op   delta
ContainsFilter-4     0.00B          0.00B          ~     (all equal)

name              old allocs/op  new allocs/op  delta
ContainsFilter-4      0.00           0.00          ~     (all equal)

Signed-off-by: Peter Štibraný <peter.stibrany@grafana.com>
  • Loading branch information
pstibrany authored and cyriltovena committed Dec 12, 2019
1 parent 7b834c5 commit 7bb77ec
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
6 changes: 4 additions & 2 deletions pkg/logql/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,13 +138,15 @@ func (e *filterExpr) Filter() (Filter, error) {
}

case labels.MatchEqual:
mb := []byte(e.match)
f = func(line []byte) bool {
return bytes.Contains(line, []byte(e.match))
return bytes.Contains(line, mb)
}

case labels.MatchNotEqual:
mb := []byte(e.match)
f = func(line []byte) bool {
return !bytes.Contains(line, []byte(e.match))
return !bytes.Contains(line, mb)
}

default:
Expand Down
22 changes: 22 additions & 0 deletions pkg/logql/ast_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,25 @@ func Test_FilterMatcher(t *testing.T) {
})
}
}

func BenchmarkContainsFilter(b *testing.B) {
expr, err := ParseLogSelector(`{app="foo"} |= "foo"`)
if err != nil {
b.Fatal(err)
}

f, err := expr.Filter()
if err != nil {
b.Fatal(err)
}

line := []byte("hello world foo bar")

b.ResetTimer()

for i := 0; i < b.N; i++ {
if !f(line) {
b.Fatal("doesn't match")
}
}
}

0 comments on commit 7bb77ec

Please sign in to comment.