Improving the build times of my compiler

2 min read Original article ↗

#My Poor Productivity

I’ve been working on code generation for my COOL compiler, but I’ve been running head first into 5-minute incremental re-compiles after changing a single line of code. This has been rough on my productivity to say the least, so I decided to go ahead and fix it once and for all.

#The Problem

The compiler’s code generation is defined in a single translation unit, so we won’t get any caching for incremental builds. Worse still, the translation unit expands to 188K heavily templated LOC after preprocessing from just 5 includes:

#include "cgen.h"

#include <cmath>
#include <sstream>
#include <string>
#include <llvm/Support/FileSystem.h>

#The Half-Solution

In an ideal world we would reduce the amount of code in headers and forward declare as much as possible, but we don’t live in that world.

Since most of the included code is effectively constant, we can use pre-compiled headers (PCH) to amortize as much of that processing across multiple builds.

Unfortunately, this is still not a great band-aid, since templates can still slow everything down. If the template isn’t instantiated in the PCH, the compiler will still have to instantiate it during each compile.

#Implementation

To generate and then use the PCH, I added a make rule to generate a PCH from a header:

$(PCH): %.h.gch: %.h
	$(CXX) $(CXXFLAGS) -x c++-header -c $< -o $@

and then added the PCH as a dep of the code generator:

cgen-1.o: cgen.cc cgen.h.gch
	$(CXX) -c $(CXXFLAGS) -include cgen.h $< -o $@

We need the -include cgen.h so that the compiler searches for the PCH first, before processing any other includes.

#Results

After swapping to pre-compiled headers, incremental debug builds went from 5 minutes to around 1 minute.

This quick implementation doesn’t precompile everything, so further improvements could be attained by precompiling more headers and optimizing templates.

Make is also not exactly fast, so switching to Ninja could also improve the build times.