-
Notifications
You must be signed in to change notification settings - Fork 2
Compile the pattern for PatternFormatter #7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #7 +/- ##
============================================
+ Coverage 99.31% 99.33% +0.01%
+ Complexity 141 132 -9
============================================
Files 14 15 +1
Lines 294 302 +8
============================================
+ Hits 292 300 +8
Misses 2 2 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
58a6fb7 to
28227b9
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
This pull request refactors the PatternFormatter to use a compiled pattern approach for performance optimization. The changes introduce a new CompiledPattern class that compiles pattern strings into regex-based search patterns with instructions for character transformation. Compiled patterns are cached statically to avoid recompilation of frequently used patterns.
Changes:
- Introduced
CompiledPatternclass that compiles pattern strings into regex-based search patterns with transformation instructions - Refactored
PatternFormatterto useCompiledPatternfor pattern compilation and caching - Updated test coverage to include the new
CompiledPatternclass
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
| src/Internal/CompiledPattern.php | New class that handles pattern compilation, caching, and provides transformation logic |
| src/PatternFormatter.php | Refactored to delegate pattern compilation to CompiledPattern and use compiled patterns for formatting |
| tests/Unit/PatternFormatterTest.php | Added coverage annotation for CompiledPattern class |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
The verification that was made upon the pattern was almost like a parser/tokenizer in itself. This change leverages that behavior to introduce full pattern compilation. When the PatternFormatter encounters a novel pattern that it has never seen before, it compiles that pattern into a 3-step CompiledPattern instance that has a search regex, a replacement pattern and instructions for the callback. Upon seeing already compiled patterns, all the PatternFormatter has to do is perform the motions (one preg_replace_callback) of the existing compiled pattern. Further steps for pattern canonicalization could be taken, such as normalizing equivalent patterns into a single form, so they could share the same cached space. However, that micro-optimization was too expensive and counter-productive. This change also opens up possibilities for in-file warmup, as CompiledPattern instances are simple objects. An user could pre-compile his/her hot-path patterns beforehand to share the cache even across diferent processes.
28227b9 to
8f4650f
Compare
The verification that was made upon the pattern was almost like a parser/tokenizer in itself.
This change leverages that behavior to introduce full pattern compilation.
When the PatternFormatter encounters a novel pattern that it has never seen before, it compiles that pattern into a 3-step CompiledPattern instance that has a search regex, a replacement pattern and instructions for the callback.
Upon seeing already compiled patterns, all the PatternFormatter has to do is perform the motions (one preg_replace_callback) of the existing compiled pattern.
Further steps for pattern canonicalization could be taken, such as normalizing equivalent patterns into a single form, so they could share the same cached space. However, that micro-optimization was too expensive and counter-productive.
This change also opens up possibilities for in-file warmup, as CompiledPattern instances are simple objects. An user could pre-compile his/her hot-path patterns beforehand to share the cache even across diferent processes.