Zend Framework performance optimize – Class Loading

Zend Framework is poweful with out questions. It has provided full-featured class library and components. But one thing also become a problem at the same time when Zend Framework show greatly convenience to users which is so called unbridled use.

 

One of the example is the Class Loading. It cost a lot in Zend Framework especially require_once and inclue_once which placed anywhere.

 

Now I will share my own experience in Class-Loading optimize based on Zend Framework Performance Guide :

 

1. Optimize include_path

 

(1) Use absolute paths but not relative to make php’s realpath cache works and also for opcode cache (e.g. Apc).

 

(2) Reduce the paths as many as possible to faster the scanning. In the other hand, Zend Framework follows PEAR naming conventions which means we should merge up with others when setting include_path.

 

(3) Zend Framework is always priority.

 

(4) Current path "." is always the last, or ignore it.

 

Here is one of the perfect example I am using :

 

 

Code :

 

// Define path to project root
defined('PROJECT_ROOT')
    || define('PROJECT_ROOT',
              realpath(dirname(dirname(__FILE__))));

// Typically, you will also want to add your library/ directory
// to the include_path, particularly if it contains your ZF install
set_include_path(implode(PATH_SEPARATOR, array(
    PROJECT_ROOT . '/library',
    // We don't use the default include_path.
    //get_include_path(),
)));

 

It can’t be more easier ! As you can see above, I put all of them (including Zend Framework, Pear, fckeditor etc.) within library in order to speed up the loading.

 

We now have to make some changes to our rewrite conditions in htaccess to make sure our js libraries work and that’s easy :

 

# To make our js libs work

RewriteCond %{REQUEST_URI} =""
RewriteCond %{REQUEST_URI} !^/(library/fckeditor)/
RewriteCond %{REQUEST_URI} !^/(library/fckeditorPlugins)/
RewriteCond %{REQUEST_URI} !^/(library/syntaxhighlighter_2.0.320)/
RewriteRule ^.*$ /public/index.php [NC,L]

RewriteCond %{REQUEST_URI} !^/public/.*$
RewriteCond %{REQUEST_URI} !^/(library/fckeditor)/
RewriteCond %{REQUEST_URI} !^/(library/fckeditorPlugins)/
RewriteCond %{REQUEST_URI} !^/(library/syntaxhighlighter_2.0.320)/
RewriteRule ^(.*)$ /public/$1

 

 

2. Remove useless require_once()

 

We need autoload within php in order to implement lazy loading and avoid useless require_once. There are two steps for us :

 

(1) Use Zend_Loader for class autoloading.

 

$autoloader = Zend_Loader_Autoloader::getInstance();
$autoloader->registerNamespace('Zend_');
$autoloader->registerNamespace('Kbs_');

 

(2) Use these command lines to remove useless require_once (from manual) :

 

% cd path/to/ZendFramework/library
% find . -name '*.php' -print0 | xargs -0 \
  sed --regexp-extended --in-place 's/(require_once)/\/\/ \1/g'

 

It’s usually used for production.

 

 

3. Use plugin-loader as few as possible

 

Here are components that related to the plugin loaders (from manual) :

 

Zend_Controller_Action_HelperBroker     : helpers
Zend_Dojo                               : view helpers, form elements and decorators
Zend_File_Transfer                      : adapters
Zend_Filter_Inflector                   : filters (used by the ViewRenderer action helper and Zend_Layout)
Zend_Filter_Input                       : filters and validators
Zend_Form                               : elements, validators, filters, decorators, captcha and file transfer adapters
Zend_Paginator                          : adapters
Zend_View                               : helpers, filters

 

We have to use them as few as possible but only in need of them. In the other hand we use Zend_Loader_PluginLoader::setIncludeFileCache to cache the plugin class files.

 

// index or bootstrap
$classFileIncCache = PROJECT_ROOT . '/repository/cache/pluginLoaderCache.php';
if (file_exists($classFileIncCache)) {
    include_once $classFileIncCache;
}
Zend_Loader_PluginLoader::setIncludeFileCache($classFileIncCache);

 

That’s 3 steps to optimize the class loading in Zend Framework. Enjoy yourself !

Posted in Zend Framework | Tagged , | 1 Comment
  1. Well done!自卖自夸:)