This site is now 100% read-only, and retired.

Speeding up recompilation with ccache

Posted by Steve on Wed 27 Apr 2005 at 16:58

Previously we've discussed how to speed up compilation by running the job on multiple machines with "distcc". If you only have a single machine, and you are compiling the same program more than once a useful alternative is the compiler cache.

ccache is a cache for your compiler, every time you compile a source file it saves away the result. Then if you attempt to recompile it again you get the previous result instead of having to invoke the compiler.

ccache is a useful tool to know about if you're only running on a single machine and cannot use the distributed compiler distcc. To gain truly impressive speedups you can even combine them both!

The ccache speedup is impressive if you're working on a large project and only change a few files between compilation attempts. It is intelligent enough to recompile a file if you change the compiler options, and can show you statistics of how good it is.

The ccache package is included in all branches of Debian and can be installed and used very easily:

apt-get install ccache

Once the package is installed you will find that it installs a group of wrapper programs for your real compiler, these will be placed inside /usr/lib/ccache.

To use them all you have to do is setup your PATH variable so that the wrappers are used instead of the usual /usr/bin/gcc. Assuming you are running bash you need to run:

export PATH=/usr/lib/ccache:$PATH

Once that's done you can just run "gcc" or "g++" as normal, and gain the speed benefit.

For example I can compile a package (in this case jabber-1.4.2) and time it:

time make
...
...
snip
...
...

real    0m54.089s
user    0m32.277s
sys     0m7.486s

Now repeating that invocation will show a significant speedup, as the file should still be in the compiler cache's cache:

make clean
time make


real    0m16.622s
user    0m10.928s
sys     0m4.805s

There we can see a speedup of 54-16 = 38 seconds. Pretty good going.

We can use the cache statistics to see how many times we avoided re-compiling files that we'd already had cached:

skx@mystery:~/Programs/jabber-1.4.2$ ccache -s
cache hit                            133
cache miss                           139
called for link                       29
multiple source files                  1
compile failed                         1
not a C/C++ file                       4
autoconf compile/link                  4
no input file                          1
files in cache                       278
cache size                           3.6 Mbytes
max cache size                     976.6 Mbytes

Here we see that 139 files were missed from the cache and had to be built, that's the first invocation. Then 133 files were found in the cache when it ran the second time.

(The disparity probably comes from the fact that I had to run "./configure" before the "make" command - which does some compilation to detect compiler flags, etc).

The output also shows the number of files currently sitting in the cache, inside ~/.ccache, and the maximum size the cache will grow.

To reset the statistics run:

ccache -z

To remove all cached files you can run the following command:

ccache -C

This removes all the files in the cache. A more brutal approach would be to remove all the files accociated with the cache, by running:

rm -rf ~/.ccache

More options can be specified either with different arguments, or with environmental variables and these are discussed in the manpage which you can read by entering the following into a terminal:

man ccache

For example this shows you how to run ccache together with distcc:

export CCACHE_PREFIX=distcc

 

 


Re: Speeding up recompilation with ccache
Posted by samb (62.252.xx.xx) on Wed 27 Apr 2005 at 18:31
Great article, one small correction - you list the package to apt-get as 'cache'. The command should of course read:
apt-get install ccache
Sam Bashton Bashton Ltd - Linux Consultancy

[ Parent ]

Re: Speeding up recompilation with ccache
Posted by Steve (82.41.xx.xx) on Wed 27 Apr 2005 at 18:41
[ View Weblogs ]

Thanks for the correction, I've fixed it in the article now.

Steve
-- Steve.org.uk

[ Parent ]

Can it be used to build debs?
Posted by bmontgom (68.15.xx.xx) on Sat 30 Apr 2005 at 17:03
Does ccache work when building debian packages? Seems like this could basically take the build time to almost nothing for changes that don't involve source code.

[ Parent ]

Re: Can it be used to build debs?
Posted by Steve (82.41.xx.xx) on Sat 30 Apr 2005 at 19:10
[ View Weblogs ]

Yes it does, and you are correct it can reduce the time needed to build packages significantly.

Steve
-- Steve.org.uk

[ Parent ]

Re: Speeding up recompilation with ccache
Posted by Anonymous (209.237.xx.xx) on Sat 30 Apr 2005 at 19:07
Normally, make is clever enough to only recompile things that are neccessary. Does 'ccache' still has a benefit when using make, or can/should we use it instead of make?

[ Parent ]

Re: Speeding up recompilation with ccache
Posted by Steve (82.41.xx.xx) on Sat 30 Apr 2005 at 19:09
[ View Weblogs ]

yes it's still useful with "make", partly because people tend to use "make clean" every now and again when developing.

Also a lot of the time make will try to rebuild things that it doesn't need to.

Also, consider the case when you download and install an application from source. Chances are there are a few files that are identical between foo-1.3, and foo-1.2 so you don't need to recompile those files again when upgrading.

Steve
-- Steve.org.uk

[ Parent ]

Re: Speeding up recompilation with ccache
Posted by Anonymous (137.208.xx.xx) on Mon 2 May 2005 at 07:39
by the way, many people in my institute got distracted by the fact that the "time"-command didn't output according to its manpage.
well, until i told them to call "/usr/bin/time"

[ Parent ]

Re: Speeding up recompilation with ccache
Posted by Anonymous (128.131.xx.xx) on Wed 4 May 2005 at 11:23

regarding ccache while using distcc, ccache seems to be a bottleneck then. but on others it's nice to add to .bashrc:

         if [ -d /usr/lib/ccache ] ; then
                 PATH="/usr/lib/ccache:${PATH}"
         fi

[ Parent ]

Re: Speeding up recompilation with ccache
Posted by Anonymous (149.156.xx.xx) on Fri 21 Apr 2006 at 13:21
I think /usr/lib/ccache/bin should be added!

[ Parent ]