perf: cached Scanner API for 13x faster library-mode scanning#42
Merged
perf: cached Scanner API for 13x faster library-mode scanning#42
Conversation
Add NewScanner() that pre-compiles rules, regex patterns, and Aho-Corasick automatons once at startup. Subsequent ScanContent calls reuse the cached matcher, dropping per-scan latency from ~10ms to ~0.7ms on typical agent messages. Additional optimizations: - Filter decoder rescan to all-target rules only (skip extension-specific) - NLP fast-path: skip markdown analysis for structureless plain text - Post-processing: early return on 0 findings, O(n log n) proximity check - Memory: 99.9% fewer allocations (151K -> 85 per scan) Backwards compatible: existing ScanContent() package-level API unchanged.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
NewScanner(opts...)that pre-compiles rules, regex patterns, and Aho-Corasick automatons once.Scanner.ScanContent()reuses the cached matcher, dropping per-scan latency geomean from ~10ms to ~1.7ms (-82.7%).ScanContent()package-level API unchanged.Benchmarks (Apple M4 Max, benchstat 6 iterations, p=0.002)
Test plan
make build && make test && make vet && make lintall passing (0 issues)-race)bench_test.gocovering 7 scenarios + mixed workload + concurrent throughput