Why Go compiles so fast

Devraj Singh
3 min readJan 31, 2021

--

Go is one of the fastest compiling programming languages out there. It compiles faster than most of the programming languages, like C/C++, Java, Rust, etc. Since the existing large codebase at Google used to take long hours to build, the quick compilation was one of the most important requirements during the creation of Golang at Google.

Here are a few reasons why Golang compiles so fast:

  1. Eliminating cycles in Go dependencies graph: I believe this is one of the important reasons for Golang having a short compilation time. Cyclic dependency in Golang is a compilation error. So if you visualize the dependencies graph, there is no cycle in it. The entire dependency tree can be visualized as a directed acyclic graph. This makes the compilation process simple for Golang and it just has to compile recursively to the bottom of the dependencies tree. Since it is assumed that there will be no cyclic dependency, each Go package can be compiled independently and in parallel.
  2. Efficient Compiler Design keeping compilation speed in mind: Faster compilation was one of the most important design goals while creating Golang. Implementing a compiler is a complex thing and there are many design factors that affect the compilation time. Since faster compilation was one of the topmost priorities while designing Go, the Go team made every effort to create a very fast compiler. For example, it is mentioned in their official FAQ page: Finally, it is intended to be fast: it should take at most a few seconds to build a large executable on a single computer. To meet these goals required addressing a number of linguistic issues: an expressive but lightweight type system; concurrency and garbage collection; rigid dependency specification; and so on. (FAQ: https://golang.org/doc/faq)
  3. Go is very simple, lean, and minimalist: For example, there are only 25 keywords in Golang. Being simple and easy to learn was also one of the design choices while creating Golang. There are very little clutter and complexities. However, this also helped in faster compilation time.
  4. Efficient Dependency Analysis: Go will only include those packages which you have imported directly in the Go file. Unlike, C/C++ there are no headers here. C/C++ takes a lot of time just to parse the enormous amount of headers present in every C/C++ code.
  5. No symbol table: Symbol tables are slow. Language like C and C++ uses symbol table to parse the language. According to Go on its official website “the language has been designed to be easy to analyze and can be parsed without a symbol table”. No use of symbol table further increases the compilation speed.
  6. Eliminating unused dependencies: Unused import in Golang is an error. Any Go file can and will import the package it needs. While this may not be an important factor for faster compilation time, but it makes sure the compilation time is not increased due to unused packages.
  7. Few other reasons: In the book: “The Go Programming Language” written by Brian Kernighan and Alan Donovan, there are few reasons listed behind the faster compilation speed of Go: 1) All imports must be listed at the beginning of source file, so the compiler does not have to read the entire file to determine the dependencies. 2) Object file for a compiled Go package records export information not just for the package itself, but for its dependencies too. When compiling a package, the compiler must read one object file for each import but need not look beyond these files.

References:

If you have liked my articles, please support me here: https://www.buymeacoffee.com/devraj

--

--

Devraj Singh
Devraj Singh

Written by Devraj Singh

Lifelong learner, Java and Go enthusiasts

No responses yet