Cache the joined log buffer and skip it for single-line rules

The log buffer joins every buffered line and scans all of them for
each pattern on every push, which is wasted work when a pattern
cannot even match across lines. The buffer now caches the joined
string, and Push invalidates the cache. CompilePattern marks the
patterns that cannot match across lines (no start anchor, no way to
match a newline, and the appended \z anchor binds every branch) and
Match checks them against the most recent line alone.
This commit is contained in:
Veer Singh
2026-07-27 16:37:28 -07:00
parent aab5e5e2bb
commit 4334390423
7 changed files with 394 additions and 42 deletions
-18
View File
@@ -118,21 +118,3 @@ func TestMatch(t *testing.T) {
}
}
}
func BenchmarkMatch(b *testing.B) {
buf := NewLogBuffer(10)
for i := 0; i < 10; i++ {
buf.Push(&types.Log{Message: "Out of memory: Kill process 20744 (mysqld) score 318 or sacrifice child"})
}
// A pattern from the default kernel monitor configuration which does not
// match the buffered logs.
expr := `task [\S ]+:\w+ blocked for more than \w+ seconds\.`
pattern, err := CompilePattern(expr)
if err != nil {
b.Fatalf("failed to compile pattern %q: %v", expr, err)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
buf.Match(pattern)
}
}