Routing performance

On my home page, routing is the top user of CPU according to Webgrind. I have a little bit of code that detects special requests then passes general requests to Phalcon routing.

There is one generic route:

$router->add('/(.*)', ['controller' => 'index', 'action' => 'index']);

then several groups of routes similar to the following.

$router->add('/users/(.*)', ['controller' => 'users', 'action' => 'redirect']);
$router->add('/users/user/(.*)', ['controller' => 'users', 'action' => 'user']);
$router->add('/users/user/', ['controller' => 'users', 'action' => 'redirect']);
$router->add('/users/user', ['controller' => 'users', 'action' => 'redirect']);

The home page is the one where I need to impress with speed. Other pages tend to be lists with big database components; the routing microseconds will not be significant.

The home page total time is 21 microseconds and php::Phalcon\Mvc\Router->handle uses 6.36. There are a couple of other items where I can shave off 1 microsecond. Chomping 6 down to something small will be the biggest saving. All suggestions welcome.

edited 29d ago

I added the following to the end of the routing list. php::Phalcon\Mvc\Router->handle dropped to 0.68 microseconds. Total page response dropped from 21 to 19 microseconds.

$router->add('/', ['controller' => 'index', 'action' => 'index']);

edited 29d ago

Down to 14 microseconds. Put in an if() to check the request and load only the / route for the home page.

edited 29d ago

Have you try to measure Symfony? :) There fun begins.

Are you benchmarking over entire Internet? Try with LAN access only in order to remove any latency and get more realistic picture of your app.


Using Webgrind on the machine running the code. All measurements through Webgrind. No network delay. Running page loads twice to reduce any cache effect in PHP 7 or Apache.

Definately a significant start up time to build router with lots of routes then significant time to search through all the routes to find the right one. Placing the most significant route at the bottom of the list reduced the search time. Reducing the number of route adds has reduced the router build time.

That should be microseconds, not milliseconds. Will edit the posts.

edited 24d ago

Well i plan to change routing related things in phalcon 4 - to simply make it faster and more reliable drop any idea you have there.

Also keep in mind - any regex will be slower.