Optimizing the compiler

Simple ways to speed up the compilation of your CaffeineC program

Using a different LLVM compiler

By default CaffeineC uses clang to compile the generated LLVM IR to an executable file (or any other format supported by clang). This is by far not optimal. There are other compilers that can be used, some even have configurations for them builtin to CaffeineC.

Using LLC + GCC

If you have both llc and gcc installed on your system, you can make use of them to compile your code a little faster. The CaffeineC compiler makes using this combination easy, you only have to add the-G flag. Depending on the system, the change in compilation speed can be pretty big. Here is an example benchmark performed on a simple Hello, World program

Benchmark 1: CaffeineC build
  Time (mean ± σ):      45.9 ms ±   0.7 ms    [User: 21.1 ms, System: 25.1 ms]
  Range (min … max):    44.6 ms …  47.6 ms    62 runs
 
Benchmark 2: CaffeineC build -G
  Time (mean ± σ):      32.7 ms ±   0.6 ms    [User: 15.9 ms, System: 17.2 ms]
  Range (min … max):    31.7 ms …  33.9 ms    89 runs
 
Summary
  CaffeineC build -G ran
    1.40 ± 0.03 times faster than CaffeineC build

Using LLC + TCC

If you want to go even faster, and don't mind having a less optimized/slower executable file generated, you can go to the extreme and use Tiny C Compiler (TCC). Once you install it, you add the -G flag as in the example above, but you also add the -T flag. This configuration speeds up the compilation over 2x compared to the default configuration.

Benchmark 1: CaffeineC build
  Time (mean ± σ):      46.8 ms ±   1.1 ms    [User: 21.8 ms, System: 25.3 ms]
  Range (min … max):    44.4 ms …  49.0 ms    61 runs
 
Benchmark 2: CaffeineC build -G -T
  Time (mean ± σ):      18.9 ms ±   0.6 ms    [User: 8.5 ms, System: 10.8 ms]
  Range (min … max):    17.5 ms …  20.3 ms    144 runs
 
Summary
  CaffeineC build -G -T ran
    2.48 ± 0.09 times faster than CaffeineC build

Using a different linker

Using GOLD

You can pass arguments directly to the compiler (clang, gcc, or tcc) by using the -a flag for clang or the -g flag for gcc and tcc. By adding the -fuse-ld flag, you can specify what linker you would like the compiler to use. So by appending -a -fuse-ld=gold you can tell the clang compiler to use the GOLD linker. On a more complicated example program, that requires linking with the X11 library, you can see a slight improvement in compilation speed when using GOLD.

Benchmark 1: CaffeineC build -a -lX11
  Time (mean ± σ):      53.2 ms ±   0.8 ms    [User: 27.3 ms, System: 27.0 ms]
  Range (min … max):    51.4 ms …  55.3 ms    52 runs
 
Benchmark 2: CaffeineC build -a -fuse-ld=gold -a -lX11
  Time (mean ± σ):      52.6 ms ±   0.9 ms    [User: 23.5 ms, System: 30.1 ms]
  Range (min … max):    50.6 ms …  55.3 ms    54 runs
 
Summary
  CaffeineC build -a -fuse-ld=gold -a -lX11 ran
    1.01 ± 0.02 times faster than CaffeineC build -a -lX11

Using MOLD

MOLD is a modern linker, that performs many times better than both GOLD and LD. You can find it's source here: https://github.com/rui314/mold. Using the same flag as above but replacing gold for mold you can tell clang to use MOLD as the linker. This offers a more noticable difference in compilation time. The difference will be more noticable when more linking has to be performed (larger projects).

Benchmark 1: CaffeineC build -a -lX11
  Time (mean ± σ):      53.3 ms ±   0.7 ms    [User: 26.5 ms, System: 27.9 ms]
  Range (min … max):    51.9 ms …  54.9 ms    54 runs
 
Benchmark 2: CaffeineC build -a -fuse-ld=mold -a -lX11
  Time (mean ± σ):      47.1 ms ±   0.9 ms    [User: 20.8 ms, System: 23.9 ms]
  Range (min … max):    45.0 ms …  49.4 ms    59 runs
 
Summary
  CaffeineC build -a -fuse-ld=mold -a -lX11 ran
    1.13 ± 0.03 times faster than CaffeineC build -a -lX11

Last updated