Skip to content

Commit

Permalink
Only sort paths once.
Browse files Browse the repository at this point in the history
This removes overhead when adding a large number of routes, as the path
keys should only be sorted when we're parsing and not on each add.
  • Loading branch information
markstory committed Dec 16, 2017
1 parent a08ae39 commit d5e9ff2
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions src/Routing/RouteCollection.php
Expand Up @@ -121,10 +121,6 @@ public function add(Route $route, array $options = [])

// Index path prefixes (for parsing)
$path = $route->staticPath();
if (empty($this->_paths[$path])) {
$this->_paths[$path] = [];
krsort($this->_paths);
}
$this->_paths[$path][] = $route;

$extensions = $route->getExtensions();
Expand All @@ -144,7 +140,12 @@ public function add(Route $route, array $options = [])
public function parse($url, $method = '')
{
$decoded = urldecode($url);
foreach (array_keys($this->_paths) as $path) {

// Sort path segments matching longest paths first.
$paths = array_keys($this->_paths);
rsort($paths);

foreach ($paths as $path) {
if (strpos($decoded, $path) !== 0) {
continue;
}
Expand Down Expand Up @@ -189,7 +190,12 @@ public function parseRequest(ServerRequestInterface $request)
{
$uri = $request->getUri();
$urlPath = urldecode($uri->getPath());
foreach (array_keys($this->_paths) as $path) {

// Sort path segments matching longest paths first.
$paths = array_keys($this->_paths);
rsort($paths);

foreach ($paths as $path) {
if (strpos($urlPath, $path) !== 0) {
continue;
}
Expand Down

0 comments on commit d5e9ff2

Please sign in to comment.