PHP Accelerators: Why? Which?

Lulu's picture

You can be curious what is an PHP accelerator and why it does speed up the execution of PHP scripts and how it does it ? There are several PHP extensions capable of doing that task: APC, XCache, Windows PHP Cache extension (Windows only), eAccelerator and some more closed-source commercial thingy's.

Its hard to decide which one is better if here are so many, yes, but basically they do same thing in the same way. There is a pic how PHP script is executed without cache/accel extension:


* a script is being read from disk, loaded into preprocessor ( that does combine all the included files )
* preprocessed script is being interpreted into bytecode
* bytecode is passed to Zend Engine

And thats how it works with acceleration:

When first call to a script is made processing is done normally (load, preprocess, bytecode compile)
* bytecode is catched by cache
* bytecode is passed to ZE

on sequential calls - the cache will just skip anything but the most basic checks (i.e. modification time of script file) and just pass cached and compiled bytecode to ZE. Thats why its fast, thats why it saved CPU time, thats why it even may save some memory.

Which one to use then? There is no champion. The best is try a several extensions yourself and select the one which suits you best. You can start with those 3:
APC - Alternative PHP Cache : libre, decent, up to date. Being a PECL extension it is planned to be included into PHP6 , a good thing to start with.
eAccelerator - libre, decent, up to date successor of Turck MMCache, it just works and works good.
XCache - another thing to try, libre, decent, up to date.

If you still want my recommendation: for mod_php it is better to use APC, since its PECL, it allows user variable cache (object caching for apps), for php-fpm it caused OOM-crashes sometimes, maybe future versions will fix it. For php-fpm i'll advise to use Eaccelerator ( 0.9.6.x version has no variable cache and may be confused if you have many chrooted identical sites ) but its best choice if you want to spare RAM, if you have lots of RAM - use XCache, it is using more RAM than EA, but it has variable cache, its optimizer does not much delay (as EA does) first script execution.

Additional features can be:
* optimize pass - the bytecode is optimized yet more when storing into cache, CON: first load can be slow.
* code scrambling - Its a way to distribute PHP scripts without source code, as binaries, therefore end user cannot view and modify the code. Not so appealing but still an option, mostly found in not-libre extensions.
* user cache functions - an API for scripts to actively put data into cache (like using memcached)
* user control functions - API to control cache, view stats, flush, change something at runtime.

No questions as usual?

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <img><i><b><h1><h2><h3>
  • Lines and paragraphs break automatically.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions. There is no CAPTCHA shown for registered and logged in users.
Image CAPTCHA
Enter the characters shown in the image.