Improve performance of Prefix transformer#1665
Merged
Merged
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #1665 +/- ##
============================================
+ Coverage 99.29% 99.53% +0.23%
Complexity 920 920
============================================
Files 190 190
Lines 2141 2148 +7
============================================
+ Hits 2126 2138 +12
+ Misses 15 10 -5 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Pull request overview
This PR optimizes the Prefix transformer by replacing multiple string-prefix loops with a single cached PCRE regex match, and adds PhpBench benchmarks to measure the impact.
Changes:
- Reworked
Respect\Validation\Transformers\Prefixto use a cachedpreg_match()-based parser. - Replaced linear skip-list checks with
isset()lookups on associative arrays. - Added
PhpBenchbenchmarks for common Prefix transformer scenarios.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
src/Transformers/Prefix.php |
Replaces prefix detection loops with a cached regex-based match and reorganizes rule lists for faster lookups. |
tests/benchmark/PrefixBench.php |
Adds benchmark scenarios to evaluate Prefix::transform() performance across common inputs. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
The Prefix transformer had many loops that could be avoided. This change replaces them for a compiled PCRE regex, taking advantage of recent PCRE JIT capabilities introduced in PHP. These changes offer no performance trade-offs, improving lookup for all categories of prefixes (property/key with shift, ignore list and fallback to simple rule). The most affected is the simple rule (no prefix or no conflict with any kind of prefix rule), yielding the most gains.
henriquemoody
approved these changes
Feb 1, 2026
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.
The Prefix transformer had many loops that could be avoided. This change replaces them for a compiled PCRE regex, taking advantage of recent PCRE JIT capabilities introduced in PHP.
These changes offer no performance trade-offs, improving lookup for all categories of prefixes (property/key with shift, ignore list and fallback to simple rule).
The most affected is the simple rule (no prefix or no conflict with any kind of prefix rule), yielding the most gains.
Gains by scenario: