Translate: 
EnglishFrenchGermanItalianPolishPortugueseRussianSpanish

How to speed up the compilation of PHP code in HipHop for PHP – part I: ccache

Compilation of any program written in PHP, depending on its size, can take anywhere from few to tens of minutes.

The long compile time is nothing unusual for C, C++ and Java developers. Unfortunately the same can not be said for PHP programmers who are accustomed to the fact that all changes in the source code are seen online in their browsers.

In this article I’ll present you one of the solutions that significantly reduces the compile time of PHP code.

Theory

In HipHop for PHP changing a single line of code in a large PHP software system may trigger massive recompilations.

Fortunately to remedy this problem you can simply use the ccache tool. It caches the output of C/C++ compilation, so the next compilation of unchanged files can be avoided and the results can be taken from the cache.

Testing platform

Processor: Intel(R) Core(TM)2 Duo CPU E7600 @ 3.06GHz
Memory: 2.5GB RAM
System: Fedora 12 (64bit)
Kernel: 2.6.32.26-175.fc12.x86_64 #1 SMP

The machine was used exclusively to compile the code.

Introductory remarks

In this article I will show you how to install ccache on the Fedora system. Following these steps on a different Linux distribution should not pose any problems, because packages mentioned in this article are available in other operating systems.

Installing ccache

Ccache tool can be easily installed on a Fedora system with the following command:

yum -y install ccache

After successful installation, we need to explicitly use ccache gcc as the C compiler and ccache g++ as the C++ compiler. In this case it is enough to override two environment variables:

export CC="ccache gcc"
export CXX="ccache g++"

You can also speed up the compilation process by using the parallel compilation of PHP files. This is done with the -jX switch, where X is a number of parallel gcc processes working on the source codes. This value should be less or equal to the number of CPUs available on the server.

Here is a sample setting enabling parallel compilation using three concurrent processes:

export MAKEOPTS=' -j3 '

When setting this parameter, you must remember that running too many concurrent processes can take entire system memory and consequently slow down the compilation.

Performance Test

I chosed Drupal 7 as a test application. This is a large CMS system consisting of 596 PHP files.

I used the following command to compile Drupal:

cd drupal
~/hiphop/hiphop-php/src/hphp/hphp \
 --keep-tempdir=1\
 --log=3\
 --input-list=files.full.list\
 --include-path="."\
 --force=1\
 --cluster-count=120\
 -v "AllDynamic=true"\
 -v "AllVolatile=true"\
 -o /tmp/drupal\
 --parse-on-demand 0

I did not have to set any parameters to use the ccache tool during compilation, make launches it automatically thanks to the previously exported CC and CXX environment variables.

As a reference result I compiled Drupal without the ccache tool enabled:

[...]
compiling and linking CPP files took 14'47" (887611 ms) (null)
all files saved in /tmp/drupal ...
running hphp took 14'59" (899128 ms) (null)

Then I did another test compiling Drupal with ccache enabled:

[...]
compiling and linking CPP files took 14'22" (862060 ms) (null)
all files saved in /tmp/drupal ...
running hphp took 14'33" (873759 ms) (null)

In both cases compile times were almost equal and took more than 14 minutes.

In the next test I put one additional line of code to the includes/install.core.inc file to trigger an E_NOTICE error using the trigger_error() function.

This time I recorded the following results:

[...]
compiling and linking CPP files took 1'19" (79849 ms) (null)
all files saved in /tmp/drupal ...
running hphp took 1'31" (91438 ms) (null)

This time thanks to the ccache tool compilation took less than two minutes!

The last test I did was to check how long takes it to re-compile the code that has not been modified at all since the last compilation.

I recorder the following results:

[...]
compiling and linking CPP files took 1'11" (71654 ms) (null)
all files saved in /tmp/drupal ...
running hphp took 1'23" (83436 ms) (null)

Summary

Here I present a summary of the results:

Type of the compilation Duration[ms] Duration[%]
Compilation without ccache 887611 100,00%
First compilation (ccache) 873759 98,43%
After code changes (ccache) 91438 10,30%
Without code changes (ccache) 83436 9,40%

HipHop for PHP & ccache
The tests clearly show the benefits of using ccache to compile the PHP files. This tool is very helpful for small but frequent changes in the source code, speeding up the compilation process tenfold.

This mechanism, unfortunately, does not affect the first compilation. In the next article in this series I’ll show you how to further speed up this process by distributed compilation.

Tags: , ,

2 Responses to “How to speed up the compilation of PHP code in HipHop for PHP – part I: ccache”

  1. Max says:

    Great! I’ll try it soon. Many thanks

  2. Mira says:

    I managed to reduce the compile time of our quite big project (150k LoC) from cca 2 hours to 5-20 minutes using this tip (depending on amount of code changes). So thank you!

Leave a Reply