Skip to content
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

Performance: Precompile the ImmutableComponentTrait::encode() regex #28

Merged
merged 1 commit into from
Dec 30, 2015
Merged

Performance: Precompile the ImmutableComponentTrait::encode() regex #28

merged 1 commit into from
Dec 30, 2015

Conversation

rbayliss
Copy link
Contributor

This pattern is used in several places:

$reservedChars = implode('', array_map(function ($value) {
  return preg_quote($value, '/');
}, static::$characters_set));

preg_replace_callback( '/(?:[^'.$reservedChars.']+|%(?![A-Fa-f0-9]{2}))/' ...

This regex is being recalculated and rebuilt every time ImmutableComponentTrait::encode(), Path::validate(), or HierarchicalPath::validate() is being called. Believe it or not, compiling this regex accounts for ~50% of the time spent in this benchmark. This PR compiles the entire regex 1x per implementor of ImmutableComponentTrait. A blackfire run of that benchmark script will show anywhere from a 40-60% reduction in time spent running that script (blackfire run --samples=5 php perf.php). So basically, we get a huge speed boost with no downside that I can see.

nyamsprod added a commit that referenced this pull request Dec 30, 2015
Performance: Precompile the ImmutableComponentTrait::encode() regex
@nyamsprod nyamsprod merged commit cf87675 into thephpleague:master Dec 30, 2015
nyamsprod added a commit that referenced this pull request Nov 17, 2022
Performance: Precompile the ImmutableComponentTrait::encode() regex
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