A lot of mysterious fog surrounds the Magento Compiler, a feature that was introduced in Magento version 1.4.2. According to the original announcement, it realizes 25-50% performance gain. But later Yoav Kutner (Magento chief) said:

In later versions since we optimized the catalog EAV, Magento Compilation is not really needed if you have byte code caching and if it is configured correctly

So is it still useful or not? What does he mean with “configured correctly”? Only one way to find out: descend into the crypts of Magento internals.

Background

First of all, “code compilation” does not refer to converting PHP code into executable machine code. It merely concatenates various PHP files into larger files and stores them in a single location (“includes/src”). Why would anyone want to do that? Because it increases the efficiency of the Autoloader. Normally, upon a request to load a class, the Autoloader looks in four locations to find the appropriate PHP file: app/code/local, app/code/community, app/code/core and finally lib. Useful if you want to overload certain core code with your own version, but slow because of the huge amount of classes scattered around the filesystem.

So the Magento Compiler ensures that the Autoloader only has to look in includes/src and will open less files. A good thing! But how good? Amaze yourself with the following figures.

Setup the Magento Compiler

I defined four testcases, namely with and without APC, with and without the Magento compiler enabled. The target shop is the sample shop running Magento 1.5.1. All types of block caching are enabled. If APC was enabled, this was used as blockcache store, otherwise the file backend was used. I used strace to measure the number of open() system calls and the amount of time spent on disk i/o per pageview. The server uses NFS storage and is otherwise idle. In each testcase I did 5 requests to make sure various caches were primed and subsequently did 30 requests (no concurrency) to measure the average time and number of system calls. 

Results

Discussion

After the results poured in, I was puzzled by the APC-enabled figures. How could Magento spend less time on disk i/o while it opened more files? The difference is apparently in the number of stat64() calls that are greatly reduced by using the compiler mode.

The average speed benefit in using the compiler mode without APC is 100-150 ms. 

The average speed benefit in using the compiler mode with APC is 25-30 ms. 

So based on average figures, one could confirm Yoav’s statement that the benefit of the compiler with APC is neglectible.

However, one should note that this system was otherwise completely idle and most likely all the filesystem calls were completely cached in local RAM. Previous measurements on normally loaded systems (iowait < 1%) show that when the local filesystem cache is saturated, the speed of stat64() calls can vary greatly, with real world figures of up to 300ms per call. That means that eliminating stat64() calls will reduce i/o performance fluctuation on NFS or other non-dedicated storage. In more human terms: enabling the compiler will eliminate this odd “slow page”.

So the answer is: Yes, use the Magento compiler!

  • Anoniem

    Hi Willem,

    thank you for this great post! What apc settings did you use during your tests? Did you have apc.stat enabled? Would be interesting to see if disabling this setting (looking up if the file has changed on each call) has any positive impact on performance.

    Bye,

    Fabrizio

  • Anoniem

    Hi! I used stat=0, include_once_override=0 (see full configuration at http://gertjanforpresident.nl/byte-bin/magento/apc.php ). So interestingly, APC still does the stats. It seems for two reasons. 1) APC doesn’t cache negative stats (probably by design) and 2) there is an unresolved bug in the latest APC code, reported here: https://bugs.php.net/bug.php?id=59951

    Cheers!

  • Gerard

    Interesting, don’t really care for the details, have followed up with your advice and activated the compiler!

  • Erik

    Hi Gerard..
    Hope your site gains from the activated compiler
    Greetings, Erik (Byte)

  • Emil

    Hi, I was always thinking that to have a better performance, I should have my compilation enable. And when I red this article, I found it is right. But my problem is: when I run my compilation my check out one page render only white page! What could be wrong? As I am a merchant and I can not find the problem, can you check it out?
    Best regards http://www.supermarket.no

  • http://twitter.com/rafaelcgstz Rafael CorrêaGomes ♛

    very cool! #MagentoCompiler

  • Ali Can

    I have like 6k files under that folder for some reason.