perf(rules): multi-pattern merge optimization with Aho-Corasick - #43
Merged
Merged
Conversation
- Add aho-corasick dependency for batched literal pattern matching - Implement TextPattern classifier and LiteralPatternMatcher (Aho-Corasick DFA) - Integrate batch matcher into execute_rules_sequential: single-pass scan for all literal patterns, replacing N individual regex scans - Add compiled regex cache (compiled_regexes) to avoid re-compilation - Add 12 regression tests guarding multi-rule correctness invariants - Add 2 GaussDB integration tests for combined-vs-individual correctness - Bump version to 0.4.13 For 10 rules on a single SQL file, reduces text scans from O(N×S) to O((L+1)×S) where L = number of literal patterns (typically >80% of total).
|
Caution Review failedThe pull request is closed. ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (9)
📝 WalkthroughWalkthroughThe PR adds Aho-Corasick literal matching for multi-rule execution, caches compiled regexes, preserves fallback paths for complex rules, and adds unit, crate-level, and GaussDB integration coverage. ChangesBatch literal rule matching
Estimated code review effort: 4 (Complex) | ~45 minutes Sequence Diagram(s)sequenceDiagram
participant RuleExecutionEngine
participant LiteralPatternMatcher
participant RegexCache
participant Findings
RuleExecutionEngine->>RuleExecutionEngine: classify patterns
RuleExecutionEngine->>LiteralPatternMatcher: scan eligible literal patterns
LiteralPatternMatcher-->>RuleExecutionEngine: grouped match spans
RuleExecutionEngine->>Findings: create literal findings
RuleExecutionEngine->>RegexCache: retrieve or compile complex pattern regex
RegexCache-->>RuleExecutionEngine: compiled regex
RuleExecutionEngine->>Findings: append fallback findings
Possibly related PRs
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
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
Reduces per-file rule scanning from O(N×S) to O((L+1)×S) where N = number of rules×patterns, S = source size, L = number of literal patterns.
Changes
LiteralPatternMatchercompiled_regexes: HashMap<String, Regex>avoids re-compilation per pattern per fileTest Results
Files
Cargo.tomlcrates/astgrep-rules/Cargo.tomlcrates/astgrep-rules/src/engine/traversal/text_pattern.rscrates/astgrep-rules/src/engine/traversal/execution.rscrates/astgrep-rules/src/engine/traversal/types.rscrates/astgrep-rules/src/engine/traversal/pattern.rscrates/astgrep-rules/src/lib.rstests/lib/sql_parser_integration_tests.rsCloses #44
Summary by CodeRabbit
New Features
Bug Fixes
Tests