Speedup the building time by 10x with ccache

Open
#7,838 6 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
35/100
Issue type
Feature
Clarity
Mostly clear
Activity status
Stale
Tech stack
c, cmake, linux

Research direction

Start by reviewing BUILDING.md and the repository's current CI setup to see how CMake and compiler commands are configured. Document ccache usage for GMT developers and evaluate enabling it in CI, with completion shown by clearer build instructions and a measurable rebuild or CI benefit.

Written by the indexing model from the issue text.

Description

Quote from the ccache homepage:

Ccache is a compiler cache. It speeds up recompilation by caching previous compilations and detecting when the same compilation is being done again.

I did some tests on my Linux machine.

Without ccache
$ mkdir build
$ cd build
$ cmake .. -G Ninja        # 9.17s user 7.43s system 99% cpu 16.651 total
$ ninja                    # 360.21s user 16.89s system 2973% cpu 12.683 total

Cmake configuration takes ~16 s and building takes ~12 s (parallel building with many cores using Ninja). For reference, the Linux CI we're using has two cores, and the building step takes about 90 s.

With ccache

After setting up ccache (for instruction about setting up ccache, see below), the building step is accelerated significantly.

$ mkdir build
$ cd build
$ cmake .. -G Ninja       # 11.88s user 9.39s system 99% cpu 21.480 total 
# first time building after setting up ccache
$ ninja                   # 383.69s user 25.69s system 3134% cpu 13.059 total    

# clean up 
$ ninja clean

# 2nd time building after setting up ccache
$ ninja                  # 1.51s user 1.38s system 245% cpu 1.182 total 

As you can see, the 2nd building takes only 1 s. It's 10x faster!

PS: I also tried using make -j2 (make with two cores), the building times are 90 s and 3 s without/with ccache. So it's 30x faster with ccache!

ccache stores compiler cache in the ~/.cache/ccache directory by default, so it still works even if you delete the entire build directory and rebuild everything.

$ cd ..
$ rm -r build
$ mkdir build
$ cd build
$ cmake .. -G Ninja

# 3rd time building after setting up ccache, but it's the first time building in this build directory
$ ninja                 # 1.52s user 1.41s system 248% cpu 1.181 total 
Setting up ccache

Setting up ccache is also very easy. You just need to prefix your compilation command with ccache, i.e., use ccache gcc instead of gcc. This can be done by:

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

or see https://github.com/ccache/ccache/blob/master/doc/INSTALL.md#installation for other ways.

Actually, you even don't need to setup ccache, because the package manager may already does it for you.

On Fedora, sudo dnf install ccache installs ccache and it also creates some symbol links. After installing ccache, I have

$ which gcc
/usr/lib64/ccache/gcc
$ ll /usr/lib64/ccache/gcc 
lrwxrwxrwx. 1 root root 16 Sep 24 15:19 /usr/lib64/ccache/gcc -> ../../bin/ccache
Summary

In summary, ccache is a useful tool for GMT developers who build GMT very often. We should document it in the BUILDING.md and also use it in our CI.

Please let me know what you think and if ccache works for you.

Dominant language
C
Stars
997
Forks
420
Avg merge
13h 27m
Merged PRs (30d)
47

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from GenericMappingTools/gmt

All issues in GenericMappingTools/gmt

Similar issues

More C issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.