Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[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

Closed
wants to merge 1 commit into from

Conversation

nicolas-grekas
Copy link
Member

@nicolas-grekas nicolas-grekas commented Jan 29, 2018

Q A
Branch? master
Bug fix? no
New feature? no
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets -
License MIT
Doc PR -

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.)

@javiereguiluz
Copy link
Member

Can anyone please benchmark this change with a non-trivial set of routes and using PHP 7.2? Thanks!

@nicolas-grekas nicolas-grekas force-pushed the router-static branch 2 times, most recently from 896e5e9 to fd00c81 Compare February 4, 2018 13:13
@nicolas-grekas
Copy link
Member Author

nicolas-grekas commented Feb 4, 2018

The perf improvement is significant: for 400 static + 400 dynamic routes, matching the last static route

  • when the 400 static precede the others: 72ms vs 65 ms for 10k match()
  • when the routes are interweaved: 775ms vs 65ms

@derrabus
Copy link
Member

derrabus commented Feb 4, 2018

So, this basically changes the order of the routes, doesn't it? Could this be a problem?

@nicolas-grekas
Copy link
Member Author

@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 :)

@nicolas-grekas
Copy link
Member Author

Closing in favor of #26059

@nicolas-grekas nicolas-grekas deleted the router-static branch February 6, 2018 07:25
Tobion added a commit that referenced this pull request Feb 12, 2018
…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
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.

4 participants