Skip to content

Conversation

@alganet
Copy link
Member

@alganet alganet commented Feb 4, 2026

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.

@codecov-commenter
Copy link

codecov-commenter commented Feb 4, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.33%. Comparing base (abb8a25) to head (8f4650f).

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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link

Copilot AI left a 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 CompiledPattern class that compiles pattern strings into regex-based search patterns with transformation instructions
  • Refactored PatternFormatter to use CompiledPattern for pattern compilation and caching
  • Updated test coverage to include the new CompiledPattern class

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.
@alganet alganet marked this pull request as ready for review February 4, 2026 15:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants