-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Routing] check static routes first in a switch #25961
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
Conversation
Can anyone please benchmark this change with a non-trivial set of routes and using PHP 7.2? Thanks! |
896e5e9
to
fd00c81
Compare
fd00c81
to
f0b7433
Compare
The perf improvement is significant: for 400 static + 400 dynamic routes, matching the last static route
|
So, this basically changes the order of the routes, doesn't it? Could this be a problem? |
@derrabus not if done wisely, which should be the case here - at least there is some logic to ensure this should not be an issue. Please review :) |
Closing in favor of #26059 |
…e regexp (nicolas-grekas) This PR was merged into the 4.1-dev branch. Discussion ---------- [Routing] Match 77.7x faster by compiling routes in one regexp | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | - Builds on top of #25961 and http://nikic.github.io/2014/02/18/Fast-request-routing-using-regular-expressions.html to make routing 77.7x faster (measured when matching the last dynamic route of 400 static + 400 dynamic routes.) More benchs welcomed. - [x] check static routes first in a switch - [x] group same-modifiers regexps - [x] put condition-less routes in a static map, in a switch's "default" - [x] match host+pathinfo at the same time - [x] group same-host regexps in sub-patterns - [x] consider the host to move more static routes in the static switch - [x] move static single-route "case" to "default" by adding requirements to $routes - [x] group same-static-prefix in sub-patterns - [x] group consecutive same-regex in a single "case" - [x] move dynamic single-route "case" to "default" by adding requirements to $routes - [x] extract host variables from the main match and remove the current 2nd match - [x] extend same-prefix grouping to placeholders - [x] group same-suffix hosts together Here is my benchmarking code: ```php <?php namespace Symfony\Component\Routing; require 'vendor/autoload.php'; $routes = new RouteCollection(); for ($i = 0; $i < 400; ++$i) { $routes->add('r'.$i, new Route('/abc'.$i)); $routes->add('f'.$i, new Route('/abc{foo}/'.$i)); } $dumper = new Matcher\Dumper\PhpMatcherDumper($routes); eval('?'.'>'.$dumper->dump()); $router = new \ProjectUrlMatcher(new RequestContext()); $i = 10000; $s = microtime(1); while (--$i) { $router->match('/abcdef/399'); } echo 'Symfony: ', 1000 * (microtime(1) - $s), "\n"; namespace FastRoute; $dispatcher = simpleDispatcher(function(RouteCollector $r) { for ($i = 0; $i < 400; ++$i) { $r->addRoute('GET', '/abc'.$i, 'r'.$i); $r->addRoute('GET', '/abc{foo}/'.$i, 'f'.$i); } }); $i = 10000; $s = microtime(1); while (--$i) { $dispatcher->dispatch('GET', '/abcdef/399'); } echo 'FastRoute: ', 1000 * (microtime(1) - $s), "\n"; ``` Commits ------- f933f70 [Routing] Match 77.7x faster by compiling routes in one regexp
Improves performance by moving static routes first when possible. Should benefit PHP 7.2 most, where switch uses a hash table.
Embeds a few fixes for bugs found meanwhile, which I'm going to submit separately for lower branches (see #25962.)