We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Slow internal stuff. Any best practises how to speed up?

Here's image from xdebug logs

Router is like this:

use \Phalcon\Mvc\Router;

$router = new Router();

$router->addGet('/v1/', array(
    'controller' => 'index',
    'action' => 'index'
));
$router->addGet('/v1/user/login',
    array(
        'controller' => 'User',
        'action' => 'login'
    ));
$router->addGet('/v1/user/logout',
    array(
        'controller' => 'User',
        'action' => 'logout'
    ));

... ~40+ more similar lines

Autoloader:

<?php

$loader = new \Phalcon\Loader();

$loader->registerDirs(array(
    $config->application->controllersDir,
    $config->application->pluginsDir,
));
$loader->registerPrefixes(
    array(
        "Model" => $config->application->modelsDir,
        "Model_Base" => $config->application->modelsBaseDir,
        "Swift_" => $config->mail->swiftDir,
    )
);
$loader->registerNamespaces(array(
    "App" => $config->application->libraryDir,
    "PayPal" => $config->application->paypalLibDir,
));

$loader->register();

Phalcon version: 1.3.4 PHP version: 5.6.10-1+deb.sury.org~precise+1

I use very simillar syntax in other projects and there is no lag. Loads up < 30ms. I use mongo db, maybe there is something with it I should know?

Have you opcache installed?, it looks like many files are being loaded from the filesystem which is slow. Also, if you can completely remove registerDirs by registerNamespaces that would be better.

edited Jun '15

Great tips, thanks.

You should do some cheat-sheet with best practises, like these 2. That would help.

I already managed to solve that "slowness" problem and now html is served mostly under 50 ms, that's speed i expect from Phalcon :) I've added quite a lot cachig logic and some other fixes just by debuging with xdebug. Opcache was enabled by default. Development & production servers but i definetly have where to improve, check out stats:

for other folks, who want to check opcache some usefull links: https://stackoverflow.com/questions/17224798/how-to-use-php-opcache https://www.php.net/manual/en/book.opcache.php https://github.com/rlerdorf/opcache-status

1. How about registerPrefixes vs Namespaces? Same or I should always use only namespaces to get maximum performance?

2. What's best way to get $di object from plugin's static method?

I use this workaround now:

<?php
class Foo extends Phalcon\Mvc\User\Plugin
{

    public static function bar()
    {
        $plugin = new Foo();
        $di = $plugin->getDI();
        $config = $di->get('config');
        // ...
    }
}
  1. How about registerPrefixes vs Namespaces? Same or I should always use only namespaces to get maximum performance?

When a class must be located by the Autoloader the each registered directory is checked if the file is located there so it means more stats and more accesses to the file system which is slow. A namespace based strategy locates classes in a O(1) operation which is faster.

  1. What's best way to get $di object from plugin's static method?

You can use Phalcon\Di::getDefault(): https://docs.phalcon.io/en/latest/reference/di.html#accessing-the-di-in-a-static-way

This may be super minor - but what about getting rid of all your routes? The routes you showed all followed the format of: /v1/[controller name]/[action name]. If they are all truly of that format, you should be able to replace them all with one route:

'/v1/:controller/:action/:params'   =>  [    ,'controller'  => 1
                                            ,'action'          => 2
                                            ,'params'          => 3
                                        ]
edited Jun '15

Phalcon\Di::getDefault(); will get "default" di, isn't it? I need services that was custom added in services.php (included to index.php) like "config", "EmailForUser" and other stuff.

they are not all like /v1/[controller name]/[action name] there are plenty "custom" stuff. The creaziest part is that for some routes this style of delcaring routes failed and raised 404.

$router->addGet('/v1/', array(
    'controller' => 'index',
    'action' => 'index'
));

So changed back all to this:

$router->addGet('/v1/', 'index::index');
$router->addGet('/v1/user/login', 'User::login');
$router->addGet('/v1/user/logout', 'User::logout');
$router->addGet('/v1/user/select', 'User::select');

It works now, service loads up under 50ms, so far so good.

getDefault() is a bit of a misnomer as it doesn't retrieve the default DI, but the most recently created one. In every case I've used Phalcon, I've only ever created one DI in the bootstrap.php (your "services.php") file, and added my custom services there. Those custom services will be accessible in the DI container you get back from getDefault()

Thanks, going to use it from now :)

getDefault() is a bit of a misnomer as it doesn't retrieve the default DI, but the most recently created one. In every case I've used Phalcon, I've only ever created one DI in the bootstrap.php (your "services.php") file, and added my custom services there. Those custom services will be accessible in the DI container you get back from getDefault()