Repository navigation
perf(patternfinder): optimize Trie memory efficiency with Radix Tree - #944
Conversation
There was a problem hiding this comment.
Code Review
This pull request refactors the PatternFinder interface and its implementations (including a new Radix Tree-backed Trie) to use string instead of []rune, parallelizes log filtering in LogFilterTask using a worker pool, and introduces lightweight extractor functions to optimize log filtering tasks. The review feedback highlights critical bugs in the Radix Tree implementation where byte-to-string conversions for non-ASCII characters can corrupt node indices, and a potential memory leak when deleting child nodes. Additionally, the feedback points out a bug in OSS K8s audit log error extraction where unpopulated status codes (value 0) are incorrectly flagged as errors, and suggests a performance optimization to return early on empty serial port payloads.
Replace the character-by-character uncompressed Trie and heavy per-node maps with a Radix Tree (Patricia Tree) with compact slice-based child nodes. Also update PatternFinder.Match to take string and return (PatternMatchResult[T], bool), eliminating all []rune conversions and intermediate heap allocations during log inspection.
d601917 to
3805d65
Compare
…indices and deletion leak
- Rename triePatternFinder to radixPatternFinder across monorepo
- Use string([]byte{b}) to prevent UTF-8 expansion when adding non-ASCII bytes to radixNode.indices
- Add NonASCIIPatterns unit test to verify multi-byte UTF-8 pattern matching and splits
- Clear trailing element to nil on node deletion to prevent memory leak
3805d65 to
6e23732
Compare
Summary
This PR replaces the uncompressed, character-by-character Trie in
pkg/common/patternfinderwith a Radix Tree (compressed Patricia tree) and optimizes the search pipeline to use zero-allocation string slicing.Key Improvements & Benchmarks
Massive Memory Reduction:
Higher Insertion & Search Throughput:
AddPattern: 14.6x - 21.3x faster (506 μs vs 10,787 μs for 1,000 patterns) with 90.2% less memory allocation.Match: 0 B/op, 0 allocs/op (zero-allocation longest prefix match).FindAllWithStarterRunes: 3.4x - 9.4x faster; temporary allocation churn during sparse match scanning is reduced from 6.4 MB down to 3.2 KB (99.95% reduction, 102x fewer allocs).API & Engine Modernization:
PatternFinder[T].Match: acceptsstringinstead of[]runeand returns(PatternMatchResult[T], bool)instead of heap-escaped pointers.FindAllWithStarterRunes: eliminates per-log[]rune(searchText)heap conversions and per-log map allocations.