Rust - Incremental Compilation

Incremental Compilation

1、the idea is basically this: when you’re working on a project, you often compile it, then change something small, then compile again.
2、The idea with incremental compilation is that you only need to compile the code you’ve actually changed1
3、One small note about this change: it makes builds faster, but makes the final binary a bit slower.

How To Use

2、Cargo.toml, add [profile.dev] incremental = true3.

opt-level = 0
debug = true
incremental = true



Incremental compilation avoids redoing work when you recompile a crate, which will ultimately lead to a much faster edit-compile-debug cycle.

Much of a programmer’s time is spent in an edit-compile-debug workflow:

1、you make a small change (often in a single module or even function),
2、you let the compiler translate the code into a binary, and finally
3、you run the program or a bunch of unit tests in order to see results of the change.

One basic strategy we can employ to achieve this is to view one big computation (like compiling a program) as a composite of many smaller, interrelated computations that build up on each other. Each of those smaller computations will yield an intermediate result that can be cached and hopefully re-used in a later iteration, sparing us the need to re-compute that particular intermediate result again.

dependency graphs

It looks like this: Each input and each intermediate result is represented as a node in a directed graph. The edges in the graph, on the other hand, represent which intermediate result or input can have an influence on some other intermediate result.

every time, some piece of data is written (like an object file), we record which other pieces of data we are accessing while doing so.

At the beginning of a subsequent compilation session, we detect which inputs (=AST nodes) have changed by comparing them to the previous version. Given the graph and the set of changed inputs, we can easily find all cache entries that are not up-to-date anymore and just remove them from the cache: