8000 [Routing] Replace previously added preg_match with preg_replace_callback by arjenm · Pull Request #18424 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Routing] Replace previously added preg_match with preg_replace_callback #18424

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

Closed
wants to merge 2 commits into from
Closed

[Routing] Replace previously added preg_match with preg_replace_callback #18424

wants to merge 2 commits into from

Conversation

arjenm
Copy link
Contributor
@arjenm arjenm commented Apr 3, 2016
Q A
Branch? master
Bug fix? refactor
New feature? performance fix, that also deprecates little used and dangerous parameter
BC breaks? yes (deprecates parameter that should be very little used)
Deprecations? yes
Tests pass? yes
Fixed tickets as discussed in #18257 and #18379
License MIT
Doc PR See discussions of as discussed in #18257 and #18379

This change was originally provided via #18379, but it was considered a BC-breakage due to the deprecation of the decodedChars-parameter on the UrlGenerator-class.

Please note that while that parameter could be seen as an extension point, it was also prone to errors in routing: by altering that array the UrlGenerator's output was not guaranteed to be RFC-safe or to match the UrlMatcher's expectations. I.e. one could set %2F to be converted to \ rather than /, completely breaking url-matching.

This PR is to remove the array, it was initially intended for its performance gain. But it also yields safer url generation (see before) and has more logical code. The old code percent-encoded (almost) all chars, but reverted some to their original character.

By replacing it with a regexp, we can inverse the behaviour of the 'rawurlencode, than decode "wanted chars" back via strtr' into 'only rawurlencode the unwanted chars'. This gives a small (in absolute terms) performance gain, since strtr is fairly slow. Due to its high relative gain it starts to adds up when you generate many urls; in the tests quoted in #18257 (comment) it'd gain about 0,5ms for every 100 url's.
While that is a minor absolute number, for pages (like ours) that generally complete within 50ms, that's still a 1% gain by simply having faster url generation.

See this test script as well: http://achelois.tweakers.net/~acm/urlrawencode_or_preg_replace.php
The not-empty test of the BC-addition will probably counter part of that gain, though.

@fabpot
Copy link
Member
fabpot commented Apr 15, 2016

ping @Tobion

@Tobion
Copy link
Contributor
Tobion commented Apr 16, 2016

@@ -28,6 +28,30 @@
class UrlGenerator implements UrlGeneratorInterface, ConfigurableRequirementsInterface
{
/**
* This regexp matches all characters that should be percent encoded in paths for url's generated by this class.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

URLs

@Tobion
Copy link
Contributor
Tobion commented Apr 16, 2016

it was also prone to errors in routing: by altering that array the UrlGenerator's output was not guaranteed to be RFC-safe

This argument is irrelevant as every extension point can be misused.

} elseif (preg_match($this->urlEncodingSkipRegexp, $url)) {
// the context base URL is already encoded (see Symfony\Component\HttpFoundation\Request)
} elseif (!empty($this->decodedChars)) {
// Support old style of character replacement, for when someone changed the decodedChars-array,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please remove this comment as it's obvious given the deprecation

@arjenm
Copy link
Contributor Author
arjenm commented Apr 17, 2016

I fixed that server, so achelois.tweakers.net should be available again.

@arjenm
Copy link
Contributor Author
arjenm commented Apr 17, 2016

@Tobion I think I got all your comments

@fabpot
Copy link
Member
fabpot commented Apr 28, 2016

Reverted #18257 instead as @Tobion was against the change in the first place and this PR introduces yet some other big changes that we cannot make in 2.7 where the original PR was merged. Sorry for the back and forth on this topic.

@fabpot fabpot closed this Apr 28, 2016
@Tobion
Copy link
Contributor
Tobion commented May 22, 2016

Btw, I also ran your benchmarks against PHP 7 and it showed a performance decrease. So IMO there is no consistently better way than the current approach.

@arjenm arjenm deleted the faster_url_generator_part1a branch January 17, 2017 07:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants
0