Skip to content

perf(patternfinder): optimize Trie memory efficiency with Radix Tree - #944

Merged
kyasbal merged 2 commits into
GoogleCloudPlatform:mainfrom
kyasbal:push-vnxnvvsntssl
Sep 3, 2026
Merged

kyasbal merged 2 commits into
GoogleCloudPlatform:mainfrom
kyasbal:push-vnxnvvsntssl

Conversation

@kyasbal

@kyasbal kyasbal commented Sep 3, 2026

Copy link
Copy Markdown
Member

Summary

This PR replaces the uncompressed, character-by-character Trie in pkg/common/patternfinder with a Radix Tree (compressed Patricia tree) and optimizes the search pipeline to use zero-allocation string slicing.

Key Improvements & Benchmarks

  1. Massive Memory Reduction:

    • For 10,000 container IDs (64-char hex strings, ~625 KB raw text), in-use heap memory is reduced from 125.08 MB down to 1.06 MB (99.1% reduction, 117.6x smaller).
    • Total heap objects dropped from 1,830,294 down to 20,693 objects (98.9% reduction, 88.4x fewer objects), drastically reducing GC pressure during log parsing.
  2. 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).
  3. API & Engine Modernization:

    • PatternFinder[T].Match: accepts string instead of []rune and returns (PatternMatchResult[T], bool) instead of heap-escaped pointers.
    • FindAllWithStarterRunes: eliminates per-log []rune(searchText) heap conversions and per-log map allocations.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread pkg/common/patternfinder/radix.go Resolved
Comment thread pkg/common/patternfinder/radix.go Resolved
Comment thread pkg/common/patternfinder/radix.go Resolved
Comment thread pkg/common/patternfinder/radix.go Resolved
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.
…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
@kyasbal
kyasbal marked this pull request as ready for review September 3, 2026 01:36
@kyasbal
kyasbal merged commit 0fae0a8 into GoogleCloudPlatform:main Sep 3, 2026
16 checks passed
@kyasbal
kyasbal deleted the push-vnxnvvsntssl branch September 3, 2026 01:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

1 participant