Ccache and Compilation times
This would be a relatively short post about a tool called ccache and how it can/could reduce the re-compilation time taken.
If people had been following this blog, you would know that this blogger likes to compile fresh code for games he’s interested in. I could venture into libraries and apps too but then I won’t have time for anything else. Now if people have been reading this blog, they would also have seen me talking about ccache but very briefly about 10 odd months ago where I had mentioned about ccache but only in passing which is a pity.
Anyways, this is the problem. Compiling any program could take a long time. For instance the last time I did a kernel compilation on this machine (an Intel Pentium dual core machine) having 2 GB of RAM it took me more than an hour. Now while I’m thankful that I’m not a coder and hence a subsystems maintainer, these guys (kernel coders) maybe compiling more than once in the day at least or maybe even more to see if the kernel is constantly undergoing churn with all the git pulls and merges that are at play there. Now imagine the time spent on compiling if each small change you had to compile from scratch. While the kernel is extreme example as development time and efforts are crazy, many applications are not so. But even then it makes sense if the compilation times are reduced.
Enter ccache. The description itself says it all :-
ccache is a compiler cache. It speeds up recompilation by caching previous compilations and detecting when the same compilation is being done again. Supported languages are C, C++, Objective-C and Objective-C++.
Now the idea behind is that many or/and most software in general doesn’t change drastically. Most of the development models follow a something on the lines of :-
a.Post the code
And then do the whole thing again.Now when you make a change in the code you are affecting only a part of the project/code-base and not the whole thing. So it’s useless and wasteful (time,RAM,computing cycles,hdd) to redo the whole thing. Hence you can use ccache to compile only those parts which have changed. I have seen compilation runs reduced by almost 60-80% using ccache .
Another reason is I usually make builds of some programs in different directories. For instance one build I will have which has commit access, while another build will not and so on and so forth. ccache also helps when building the different builds as well. Also after doing a $make clean it compiles faster than it normally would.
Installing ccache is simple if you are in a Debian/Ubuntu based/derivative distribution. Simply take your favorite package-manager and install it. For instance I like aptitude so installing it as simple as :-
$sudo aptitude install ccache
And it should install the same for you. You have to configure it mildly before you can use it. You can either put ccache in your $PATH before the compiler or the best way is to symlink (symbolically linking,softlinking,shortcut) it :-
$sudo ln -s /usr/bin/ccache /usr/local/bin/gcc
Testing it is easy. Just do the following :-
$ which gcc
$ ll /usr/local/bin/gcc
lrwxrwxrwx 1 root staff 6 2011-08-31 10:57 /usr/local/bin/gcc -> ccache
This means ccache is ready to work on your code. There is a limitation with this tool though, it works and is attuned to only gcc and/or its brethren.
Now when you are running ccache it’s important to know that there’s a hidden .ccache directory as well in your $HOME directory. Now if you go into that directory you will find various sub-directories in it as well from 0 to 9 and a to f which probably all computer science students would be familiar with.
I don’t want to go much into the details for frankly I don’t know much and then again this post is just meant as an overview of what it can do.
That’s all for now, happy hacking.