Another great speedup-tool that I’ve just recently started taking advantage of is ccache.  This caches the preprocessor output of your compiler (if I understand right) and matches them against a hash.  The nice thing is that ccache guarantees that it’s output will always be the exact same, bit-for-bit, as the compiler’s output.

Where it comes in handy is rebuilding source code.  In other words, if you change something in one of your projects and decide to do a “make clean && make”, the compiler will not actually rebuild files that would not have changed at all.  This saves ENORMOUS amounts of time.

You know what else?  You can combine it with distcc.  In Ubuntu, the installation is simple.  First, install ccache by doing “sudo apt-get install ccache”.  Then, instead of putting /usr/lib/distcc in your path, put /usr/lib/ccache.  After that, just set the environment variable CCACHE_PREFIX to “distcc”.  What this will do is call ccache in place of gcc.  For files whose hash matches something in the cache, the cached file gets returned; if not, it passes the compile on to distcc.

Just so you have an idea of the potential time savings, here’s an example.  In our last post, we showed the time saved by using distcc.  The source code we tested that with has not changed, so we can use it again.  Compiling with distcc+ccache (after a make clean with NO source changes) yields the following results:

real    2m3.452s
user    1m9.508s
sys    0m25.002s

Remember, this is without making ANY changes to the source.  This means that distcc probably wasn’t doing anything, and it means this is the highest possible speed increase we can get.  If we change any files the increase will be less, but still significant.  Now, these numbers say we got a (1 – 94.51 /446.475) * 100 =78.83% speedup!

I also found that changing 1 file in the source gave nearly the exact same results.  Of course, if you doing a complete rebuild  you’ve probably changed a lot of files, but still.  Unless you change virtually every file in your source tree, expect to have significant gains over distcc alone.

You can check to see how much good your cache is doing you by issuing the command “ccache –show-stats”.  My output is as follows (note, I increased my max cache size by issuing “ccache –max-size=2g”):

cache directory                     /home/lytithwyn/.ccache
cache hit                           1699
cache miss                           441
called for link                       90
compile failed                         2
files in cache                       882
cache size                          12.6 Mbytes
max cache size                       2.0 Gbytes

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: