In the previous part on optimizing ZF2 (version 2.3.1) the total function call for a simple “hello world” application is around 7800 and the index page takes about 800 ms to load. Autoloading by class maps can be a help. These class maps are generated by Composer by examining each library in the vendor directory. it is included in the composer.phar update command. Curiously the Zend library itself is excluded from the resulting class map. The possible reason is that the process can take a while. Zf2’s own class map generator can create a custom ZF2 class map by executing a command similar to this one (in the vendor map):

./bin/classmap_generator.php -w --library zendframework/zendframework/l
ibrary/Zend --output zend_autoload_classmap.php

The resulting class map array contains about 2100 entries. Registering the map to SPL_autoload() can be done through the Composer autoload process already running in init_autoload.php

if (file_exists('vendor/autoload.php')) {
    $loader = include 'vendor/autoload.php';
}
$classMap = require __DIR__ . '/vendor/zend_autoload_classmap.php';

// Register the class map:
$loader->addClassMap($classMap);

// Register with spl_autoload:
$loader->register(true);

Composer has a quicker way. The command

php composer.phar install --optimize-autoloader

will simply add the 2000 additional lines to the existing central class map. Disadvantage of this method: Composer updates take a lot longer.

The results are impressive. The total function call drops from 7838 to 6379 (-18%) and the loading times is reduced to 630 (-21%).

xdebugwith added ZF2 class map

See latest commit for code changes.

Advertisements