Frank DENIS random thoughts.

Optimizing require_once with xend

Although things are getting better with PHP 5.2, file inclusion (require_once) has always been slow with PHP.

Libraries like Zend Framework, AdoDB or Smarty are designed have their source code is divided into multiple files, in a very clean way. The drawback is that in order to use them, PHP has to included files that include other files, that include other files… and it introduces a noticeable startup delay. And it even happens with accelerators like Xcache.

An obvious workaround is to concatenate every required file into a single file. Several people noticed that the Zend Framework was from 30% to 3 times faster that way.

There’s an interesting project that automates that task : the Xend PHP extension.

Xend finds require_once() statements, inserts the content where it should be and then it automatically saves the one-file version into a new PHP file.

Just look at the benchmarks.

The Xend extension seems to be an easy way to speed up a lot of PHP applications without any code change. Unfortunately it’s still at early stage (at least on OpenBSD, it’s as stable as nitroglycerine).

By the way, another tiny project that can be interesting is the Dgx’s PHP shrinker. Shrinking the source code will reduce the time needed by the accelerator (Xcache, APC…) in order to build the opcode cache for the file.