At 40 mb and 1 sec is not an option for sure .
trying not to hit the database to find the first segment and query various tables.
Is a Region ?
Is a city ?
Is a category ?
Is a subcategory ?
Is a page ?
Is a user ?
Altorouter (optimized ) vs Symfony benchmark same 8000 routes (Spain has 8000 cities ).
Symfony 1 loop random route match . 1 loop no match
[map_symfony] => Array
(
[start] => 1568314486.7859
[end] => 1568314486.8639
[start_memory] => 21650600
[end_memory] => 49116216
[duration] => 0.07800007
[memory] => 26.19 mb
)
[symfony_match] => Array
(
[start] => 1568314486.8639
[end] => 1568314486.8951
[start_memory] => 49116592
[end_memory] => 54674560
[duration] => 0.03119993
[memory] => 5.3 mb
)
[symfony_no_match] => Array
(
[start] => 1568314486.8951
[end] => 1568314487.1291
[start_memory] => 54674936
[end_memory] => 97618904
[duration] => 0.23399997
[memory] => 40.95 mb
)
Altorouter 10000 loops to match 10 000 loops no match
[map_altorouter] => Array
(
[start] => 1568315731.476
[end] => 1568315731.476
[start_memory] => 8039096
[end_memory] => 11265408
[duration] => 0
[memory] => 3.08 mb
)
[altorouter_match] => Array
(
[start] => 1568315731.476
[end] => 1568315731.5228
[start_memory] => 11265784
[end_memory] => 11265784
[duration] => 0.04680014
[memory] => NAN b
)
[altorouter_no_match] => Array
(
[start] => 1568315731.5228
[end] => 1568315731.5696
[start_memory] => 11266160
[end_memory] => 11266160
[duration] => 0.0467999
[memory] => NAN b
)
PHP Code:
$nRoutes = 8000;
$requests = array();
for($i=0; $i<$nRoutes; $i++) {
$requests[] = array(
'method' => random_request_method(),
'url' => random_request_url(),
'target' => 'Home::index',
'name'=>'scriptswatre'.$i
);
}
//echo count($requests);
$router = new AltoRouter();
// map requests
Bench::start('map_altorouter');
foreach($requests as $r) {
$router->map($r['method'], $r['url'], $r['target'],$r['name']);
}
Bench::stop('map_altorouter');
// pick random route to match
$r = $requests[array_rand($requests)];
p_print_r($r);
Bench::start('altorouter_match');
for($i=0; $i<10000; $i++) {
$match = $router->match($r['url'], $r['method']);
}
//p_print_r($match);
Bench::stop('altorouter_match');
Bench::start('altorouter_no_match');
for($i=0; $i<10000; $i++) {
$router->match('/55-foo-bar', 'GET');
}
Bench::stop('altorouter_no_match');
}
at extra 3 mb and this speed to match is a viable option to replace the database lookup