GenericMappingTools / GenericMappingTools/gmt

Speedup the building time by 10x with ccache

Open
#7,838 6 comments 0 reactions 0 assignees View on GitHub
Dominant language
C
Stars
979
Forks
414
Avg merge
17h 26m
Merged PRs (30d)
54

Description

Quote from the ccache homepage:

> [Ccache](https://ccache.dev/) is a compiler cache. It [speeds up recompilation](https://ccache.dev/performance.html) 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.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.