Skip to content
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

Improve case insensitive search to avoid allocations. #4394

Merged
merged 5 commits into from
Nov 23, 2021
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
48 changes: 43 additions & 5 deletions pkg/logql/log/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"fmt"
"regexp"
"regexp/syntax"
"unicode"
"unicode/utf8"

"github.com/prometheus/prometheus/pkg/labels"
)
Expand Down Expand Up @@ -166,11 +168,47 @@ type containsFilter struct {
caseInsensitive bool
}

func (l containsFilter) Filter(line []byte) bool {
if l.caseInsensitive {
line = bytes.ToLower(line)
func (l *containsFilter) Filter(line []byte) bool {
if !l.caseInsensitive {
return bytes.Contains(line, l.match)
}
return bytes.Contains(line, l.match)
if len(l.match) == 0 {
return true
}
if len(l.match) > len(line) {
return false
}
j := 0
for len(line) > 0 {
// ascii fast case
if c := line[0]; c < utf8.RuneSelf {
if c == l.match[j] || c+'a'-'A' == l.match[j] {
j++
if j == len(l.match) {
return true
}
line = line[1:]
continue
}
line = line[1:]
j = 0
continue
}
// unicode slow case
lr, lwid := utf8.DecodeRune(line)
mr, mwid := utf8.DecodeRune(l.match[j:])
if lr == mr || mr == unicode.To(unicode.LowerCase, lr) {
j += mwid
if j == len(l.match) {
return true
}
line = line[lwid:]
continue
}
line = line[lwid:]
j = 0
}
return false
}

func (l containsFilter) ToStage() Stage {
Expand All @@ -193,7 +231,7 @@ func newContainsFilter(match []byte, caseInsensitive bool) Filterer {
if caseInsensitive {
match = bytes.ToLower(match)
}
return containsFilter{
return &containsFilter{
match: match,
caseInsensitive: caseInsensitive,
}
Expand Down
23 changes: 21 additions & 2 deletions pkg/logql/log/filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
func Test_SimplifiedRegex(t *testing.T) {
fixtures := []string{
"foo", "foobar", "bar", "foobuzz", "buzz", "f", " ", "fba", "foofoofoo", "b", "foob", "bfoo", "FoO",
"foo, 世界", allunicode(), "fooÏbar",
}
for _, test := range []struct {
re string
Expand Down Expand Up @@ -53,6 +54,8 @@ func Test_SimplifiedRegex(t *testing.T) {
{".*||||", true, TrueFilter, true},
{"", true, TrueFilter, true},
{"(?i)foo", true, newContainsFilter([]byte("foo"), true), true},
{"(?i)界", true, newContainsFilter([]byte("界"), true), true},
{"(?i)ïB", true, newContainsFilter([]byte("ïB"), true), true},

// regex we are not supporting.
{"[a-z]+foo", false, nil, false},
Expand Down Expand Up @@ -93,6 +96,14 @@ func Test_SimplifiedRegex(t *testing.T) {
}
}

func allunicode() string {
var b []byte
for i := 0x00; i < 0x10FFFF; i++ {
b = append(b, byte(i))
}
return string(b)
}

func Test_TrueFilter(t *testing.T) {
empty := []byte("")
for _, test := range []struct {
Expand Down Expand Up @@ -167,13 +178,21 @@ func benchmarkRegex(b *testing.B, re, line string, match bool) {
b.ResetTimer()
b.Run(fmt.Sprintf("default_%v_%s", match, re), func(b *testing.B) {
for i := 0; i < b.N; i++ {
m = d.Filter(l)
for j := 0; j < 1e6; j++ {
m = d.Filter(l)
}
}
})
b.Run(fmt.Sprintf("simplified_%v_%s", match, re), func(b *testing.B) {
for i := 0; i < b.N; i++ {
m = s.Filter(l)
for j := 0; j < 1e6; j++ {
m = s.Filter(l)
}
}
})
res = m
}

func Test_rune(t *testing.T) {
require.True(t, newContainsFilter([]byte("foo"), true).Filter([]byte("foo")))
}