<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Calvin Weng</title>
    <description>The latest articles on DEV Community by Calvin Weng (@dcalvin).</description>
    <link>https://dev.to/dcalvin</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F326977%2F9106cc3d-24b2-4809-8488-9a099b04ac65.jpeg</url>
      <title>DEV Community: Calvin Weng</title>
      <link>https://dev.to/dcalvin</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/dcalvin"/>
    <language>en</language>
    <item>
      <title>The Rust Compilation Model Calamity</title>
      <dc:creator>Calvin Weng</dc:creator>
      <pubDate>Thu, 30 Jan 2020 10:42:13 +0000</pubDate>
      <link>https://dev.to/dcalvin/the-rust-compilation-model-calamity-2h12</link>
      <guid>https://dev.to/dcalvin/the-rust-compilation-model-calamity-2h12</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;About the author&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/brson" rel="noopener noreferrer"&gt;&lt;strong&gt;Brian Anderson&lt;/strong&gt;&lt;/a&gt; is one of the co-founders of the Rust programming language and its sister project, the Servo web browser. He is now working in PingCAP as a senior database engineer.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;em&gt;The Rust programming language was designed for slow compilation times.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;I mean, that wasn't &lt;em&gt;the goal&lt;/em&gt;. As is often cautioned in debates among their designers, programming language design is full of tradeoffs. One of those fundamental tradeoffs is &lt;strong&gt;run-time performance&lt;/strong&gt; vs. &lt;strong&gt;compile-time performance&lt;/strong&gt;, and the Rust team nearly always (if not always) chose run-time over compile-time.&lt;/p&gt;

&lt;p&gt;So Rust compile times are bad. This is kinda infuriating, as almost everything that matters about Rust is pretty damn good. But Rust compile times are so, so bad.&lt;/p&gt;

&lt;h2&gt;
  
  
  Rust Compile-time Adventures with TiKV: Episode 1
&lt;/h2&gt;

&lt;p&gt;At &lt;a href="https://pingcap.com/" rel="noopener noreferrer"&gt;PingCAP&lt;/a&gt;, we develop our distributed storage system, &lt;a href="https://github.com/tikv/tikv/" rel="noopener noreferrer"&gt;TiKV&lt;/a&gt;, in Rust, and it compiles slow enough to discourage many in the company from using Rust. I recently spent some time, along with several others on the TiKV team and its wider community, investigating TiKV's compile times.&lt;/p&gt;

&lt;p&gt;Over a series of posts, I'll discuss what we have learned:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Why compiling Rust is slow, and/or feels slow;&lt;/li&gt;
&lt;li&gt;How Rust's development lead to slow compile-times;&lt;/li&gt;
&lt;li&gt;Compile-time use cases;&lt;/li&gt;
&lt;li&gt;Things we measured; things we want to measure but haven't or don't know how;&lt;/li&gt;
&lt;li&gt;Ideas that improved compile times;&lt;/li&gt;
&lt;li&gt;Ideas that did not improve compile times;&lt;/li&gt;
&lt;li&gt;How TiKV compile times have changed over time;&lt;/li&gt;
&lt;li&gt;Suggestions for how to organize Rust projects that compile fast;&lt;/li&gt;
&lt;li&gt;Recent and future upstream improvements to compile times.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In this episode:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The spectre of poor Rust compile times at PingCAP&lt;/li&gt;
&lt;li&gt;Preview: the TiKV compile-time adventure so far&lt;/li&gt;
&lt;li&gt;
Rust's designs for poor compilation time

&lt;ul&gt;
&lt;li&gt;Bootstrapping Rust&lt;/li&gt;
&lt;li&gt;(Un)virtuous cycles&lt;/li&gt;
&lt;li&gt;Early decisions that favored run-time over compile-time&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;Recent work on Rust compile times&lt;/li&gt;

&lt;li&gt;In the next episode&lt;/li&gt;

&lt;li&gt;Thanks&lt;/li&gt;

&lt;/ul&gt;

&lt;h2&gt;
  
  
  The spectre of poor Rust compile times at PingCAP
&lt;/h2&gt;

&lt;p&gt;At &lt;a href="https://pingcap.com/en/" rel="noopener noreferrer"&gt;PingCAP&lt;/a&gt;, my colleagues use Rust to write &lt;a href="https://github.com/tikv/tikv/" rel="noopener noreferrer"&gt;TiKV&lt;/a&gt;, the storage node of &lt;a href="https://github.com/pingcap/tidb" rel="noopener noreferrer"&gt;TiDB&lt;/a&gt;, our distributed database. They do this because they want this most important node in the system to be fast and reliable by construction, at least to the greatest extent reasonable.&lt;/p&gt;

&lt;p&gt;It was mostly a great decision, and most people internally are mostly happy about it.&lt;/p&gt;

&lt;p&gt;But many complain about how long it takes to build. For some, a full rebuild might take 15 minutes in development mode, and 30 minutes in release mode. To developers of large systems projects, this might not sound so bad, but it's much slower than what many developers expect out of modern programming environments. TiKV is a relatively large Rust codebase, with 2 million lines of Rust. In comparison, Rust itself contains over 3 million lines of Rust, and &lt;a href="https://github.com/servo/servo" rel="noopener noreferrer"&gt;Servo&lt;/a&gt; contains 2.7 million (see &lt;a href="https://gist.github.com/brson/31b6f8c5467b050779ce9aa05d41aa84" rel="noopener noreferrer"&gt;full line counts here&lt;/a&gt;).&lt;/p&gt;

&lt;p&gt;Other nodes in TiDB are written in Go, which of course comes with a different set of advantages and disadvantages from Rust. Some of the Go developers at PingCAP resent having to wait for the Rust components to build. They are used to a rapid build-test cycle.&lt;/p&gt;

&lt;p&gt;Rust developers, on the other hand, are used to taking a lot of coffee breaks (or tea, cigarettes, sobbing, or whatever as the case may be — Rust developers have the spare time to nurse their demons).&lt;/p&gt;

&lt;h2&gt;
  
  
  Preview: The TiKV Compile-time adventure so far
&lt;/h2&gt;

&lt;p&gt;The first entry in this series is just a story about the history of Rust with respect to compilation time. Since it might take several more entries before we dive into concrete technical details of what we've done with TiKV's compile times, here's a pretty graph to capture your imagination, without comment.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdownload.pingcap.com%2Fimages%2Fblog%2Frust-compile-times-tikv.svg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdownload.pingcap.com%2Fimages%2Fblog%2Frust-compile-times-tikv.svg" alt="Rust Compile Times for TiKV"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Rust Compile Times for TiKV &lt;/p&gt;

&lt;h2&gt;
  
  
  Rust's designs for poor compilation time
&lt;/h2&gt;

&lt;p&gt;Rust was designed for slow compilation times.&lt;/p&gt;

&lt;p&gt;I mean, that wasn't &lt;em&gt;the goal&lt;/em&gt;. As is often cautioned in debates among their designers, programming language design is full of tradeoffs. One of those fundamental tradeoffs is &lt;strong&gt;run-time performance&lt;/strong&gt; vs. &lt;strong&gt;compile-time performance&lt;/strong&gt;, and the Rust team nearly always (if not always) chose run-time over compile-time.&lt;/p&gt;

&lt;p&gt;The intentional run-time/compile-time tradeoff isn't the only reason Rust compile times are horrific, but it's a big one. There are also language designs that are not crucial for run-time performance, but accidentally bad for compile-time performance. The Rust compiler was also implemented in ways that inhibit compile-time performance.&lt;/p&gt;

&lt;p&gt;So there are intrinsic language-design reasons and accidental language-design reasons for Rust's bad compile times. Those mostly can't be fixed ever, although they may be mitigated by compiler improvements, design patterns, and language evolution. There are also accidental compiler-architecture reasons for Rust's bad compile times, which can generally be fixed through enormous engineering effort and time.&lt;/p&gt;

&lt;p&gt;If fast compilation time was not a core Rust design principle, what were Rust's core design principles? Here are a few:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;em&gt;Practicality&lt;/em&gt; — it should be a language that can be and is used in the real world.&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;Pragmatism&lt;/em&gt; — it should admit concessions to human usability and integration into systems as they exist today.&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;Memory-safety&lt;/em&gt; — it must enforce memory safety, and not admit segmentation faults and other such memory-access violations.&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;Performance&lt;/em&gt; — it must be in the same performance class as C++.&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;Concurrency&lt;/em&gt; — it must provide modern solutions to writing concurrent code.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;But it's not like the designers didn't put &lt;em&gt;any&lt;/em&gt; consideration into fast compile times. For example, for any analysis Rust needs to do, the team tried to ensure reasonable bounds on computational complexity. Rust's design history though is one of increasingly being sucked into a swamp of poor compile-time performance.&lt;/p&gt;

&lt;p&gt;Story time.&lt;/p&gt;

&lt;h3&gt;
  
  
  Bootstrapping Rust
&lt;/h3&gt;

&lt;p&gt;I don't remember when I realized that Rust's bad compile times were a strategic problem for the language, potentially a fatal mistake in the face of competition from future low-level programming languages. For the first few years, hacking almost entirely on the Rust compiler itself, I wasn't too concerned, and I don't think most of my peers were either. I mostly remember that Rust compile time was always bad, and like, whatever, I can deal with that.&lt;/p&gt;

&lt;p&gt;When I worked daily on the Rust compiler, it was common for me to have at least three copies of the repository on the computer, hacking on one while all the others were building and testing. I would start building workspace 1, switch terminals, remember what's going on over here in workspace 2, hack on that for a while, start building in workspace 2, switch terminals, etc. Little flow, constant context switching.&lt;/p&gt;

&lt;p&gt;This was (and probably is) typical of other Rust developers too. I still do the same thing hacking on TiKV today.&lt;/p&gt;

&lt;p&gt;So, historically, how bad have Rust compile times been? A simple barometer here is to see how Rust's self-hosting times have changed over the years, which is the time it takes Rust to build itself. Rust building itself is not directly comparable to Rust building other projects, for a variety of reasons, but I think it will be illustrative.&lt;/p&gt;

&lt;p&gt;The &lt;a href="https://gist.github.com/brson/31b6f8c5467b050779ce9aa05d41aa84/edit" rel="noopener noreferrer"&gt;first Rust compiler&lt;/a&gt;, from 2010, called rustboot, was written in OCaml, and it's ultimate purpose was to build a second compiler, rustc, written in Rust, and begin the self-hosting bootstrap cycle. In addition to being written in Rust, rustc would also use &lt;a href="https://llvm.org/" rel="noopener noreferrer"&gt;LLVM&lt;/a&gt; as its backend for generating machine code, instead of rustboot’s hand-written x86 code-generator.&lt;/p&gt;

&lt;p&gt;Rust needed to become self-hosting as a means of "dog-fooding" the language — writing the Rust compiler in Rust meant that the Rust authors needed to use their own language to write practical software early in the language design process. It was hoped that self-hosting could lead to a useful and practical language.&lt;/p&gt;

&lt;p&gt;The first time Rust built itself was on April 20, 2011. &lt;a href="https://mail.mozilla.org/pipermail/rust-dev/2011-April/000330.html" rel="noopener noreferrer"&gt;It took one hour&lt;/a&gt;, which was a laughably long time. At least it was back then.&lt;/p&gt;

&lt;p&gt;That first super-slow bootstrap was an anomaly of bad code-generation and other easily fixable early bugs (probably, I don't exactly recall). rustc's performance quickly improved, and Graydon quickly &lt;a href="https://github.com/rust-lang/rust/commit/6997adf76342b7a6fe03c4bc370ce5fc5082a869" rel="noopener noreferrer"&gt;threw away the old rustboot compiler&lt;/a&gt; since there was nowhere near enough manpower and motivation to maintain parallel implementations.&lt;/p&gt;

&lt;p&gt;This is where the long, gruelling history of Rust's tragic compile times began, 11 months after it was initially released in June 2010.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;I wanted to share historic self-hosting times here, but after many hours and obstacles attempting to build Rust revisions from 2011, I finally gave up and decided I just had to publish this piece without them. Instead, here are some madeup numbers:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;em&gt;7 femto-bunnies&lt;/em&gt; - rustboot building Rust prior to being retired&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;49 kilo-hamsters&lt;/em&gt; - rustc building Rust immediately after rustboot’s retirement&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;188 giga-sloths&lt;/em&gt; - rustc building Rust in 2020&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;

&lt;p&gt;Anyway, last time I bootstrapped Rust a few months ago, it took over five hours.&lt;/p&gt;

&lt;p&gt;The Rust language developers became acclimated to Rust's poor self-hosting times and failed to recognize or address the severity of the problem of bad compile times during Rust's crucial early design phase.&lt;/p&gt;

&lt;h3&gt;
  
  
  (Un)virtuous cycles
&lt;/h3&gt;

&lt;p&gt;In the Rust project, we like processes that reinforce and build upon themselves. This is one of the keys to Rust's success, both as a language and a community.&lt;/p&gt;

&lt;p&gt;As an obvious, hugely-successful example, consider &lt;a href="https://github.com/servo/servo" rel="noopener noreferrer"&gt;Servo&lt;/a&gt;. Servo is a web browser built in Rust, and Rust was created with the explicit purpose of building Servo. Rust and Servo are sister-projects. They were created by the same team (initially), at roughly the same time, and they evolved together. Not only was Rust built to create Servo, but Servo was built to inform the design of Rust.&lt;/p&gt;

&lt;p&gt;The initial few years of both projects were extremely difficult, with both projects evolving in parallel. The often-used metaphor of the &lt;a href="https://en.wikipedia.org/wiki/Ship_of_Theseus" rel="noopener noreferrer"&gt;Ship of Theseus&lt;/a&gt; is apt - we were constantly rebuilding Rust in order to sail the seas of Servo. There is no doubt that the experience of building Servo with Rust while simultaneously building the language itself led directly to many of the good decisions that make Rust the practical language it is.&lt;/p&gt;

&lt;p&gt;Here are some cursory examples of the Servo-Rust feedback loop:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Labeled break and continue &lt;a href="https://github.com/rust-lang/rust/issues/2216" rel="noopener noreferrer"&gt;was implemented in order to auto-generate an HTML parser&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;Owned closures &lt;a href="https://github.com/rust-lang/rust/issues/2549#issuecomment-19588158" rel="noopener noreferrer"&gt;were implemented after analyzing closure usage in Servo&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;External function calls used to be considered safe. &lt;a href="https://github.com/rust-lang/rust/issues/2628#issuecomment-9384243" rel="noopener noreferrer"&gt;This changed in part due to experience in Servo&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;The migration from green-threading to native threading was informed by the experience of building Servo, observing the FFI overhead of Servo's SpiderMonkey integration, and profiling "hot splits", where the green thread stacks needed to be expanded and contracted.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The co-development of Rust and Servo created a &lt;a href="https://en.wikipedia.org/wiki/Virtuous_circle_and_vicious_circle" rel="noopener noreferrer"&gt;virtuous cycle&lt;/a&gt; that allowed both projects to thrive. Today, Servo components are deeply integrated into Firefox, ensuring that Rust cannot die while Firefox lives.&lt;/p&gt;

&lt;p&gt;Mission accomplished.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fslhdsr6ven9cih6f4olr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fslhdsr6ven9cih6f4olr.png" alt="Rust Mission Accomplished"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The previously-mentioned early self-hosting was similarly crucial to Rust's design, making Rust a superior language for building Rust compilers. Likewise, Rust and &lt;a href="https://webassembly.org/" rel="noopener noreferrer"&gt;WebAssembly&lt;/a&gt; were developed in close collaboration (the author of &lt;a href="https://github.com/emscripten-core/emscripten" rel="noopener noreferrer"&gt;Emscripten&lt;/a&gt;, the author of &lt;a href="https://github.com/CraneStation/cranelift" rel="noopener noreferrer"&gt;Cranelift&lt;/a&gt;, and I had desks next to each other for years), making WASM an excellent platform for running Rust, and Rust well-suited to target WASM.&lt;/p&gt;

&lt;p&gt;Sadly there was no such reinforcement to drive down Rust compile times. The opposite is probably true — the more Rust became known as a &lt;em&gt;fast&lt;/em&gt; language, the more important it was to be &lt;em&gt;the fastest&lt;/em&gt; language. And, the more Rust's developers got used to developing their Rust projects across multiple branches, context switching between builds, the less pressure was felt to address compile times.&lt;/p&gt;

&lt;p&gt;This only really changed once Rust 1.0 was released in 2015 and started to receive wider use.&lt;/p&gt;

&lt;p&gt;For years Rust &lt;a href="https://en.wikipedia.org/wiki/Boiling_frog" rel="noopener noreferrer"&gt;slowly boiled&lt;/a&gt; in its own poor compile times, not realizing how bad it had gotten until it was too late. It was 1.0. Those decisions were locked in.&lt;/p&gt;

&lt;p&gt;Too many tired metaphors in this section. Sorry about that.&lt;/p&gt;

&lt;h3&gt;
  
  
  Early decisions that favored run-time over compile-time
&lt;/h3&gt;

&lt;p&gt;If Rust is designed for poor compile time, then what are those designs specifically? I describe a few briefly here. The next episode in this series will go into further depth. Some have greater compile-time impact than others, but I assert that all of them cause more time to be spent in compilation than alternative designs.&lt;/p&gt;

&lt;p&gt;Looking at some of these in retrospect, I am tempted to think that "well, of course Rust &lt;em&gt;must&lt;/em&gt; have feature &lt;em&gt;foo&lt;/em&gt;", and it's true that Rust would be a completely different language without many of these features. However, language designs are tradeoffs and none of these were predestined to be part of Rust.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;Borrowing&lt;/em&gt; — Rust's defining feature. Its sophisticated pointer analysis spends compile-time to make run-time safe.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;Monomorphization&lt;/em&gt; — Rust translates each generic instantiation into its own machine code, creating code bloat and increasing compile time.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;Stack unwinding&lt;/em&gt; — stack unwinding after unrecoverable exceptions traverses the callstack backwards and runs cleanup code. It requires lots of compile-time book-keeping and code generation.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;Build scripts&lt;/em&gt; — build scripts allow arbitrary code to be run at compile-time, and pull in their own dependencies that need to be compiled. Their unknown side-effects and unknown inputs and outputs limit assumptions tools can make about them, which e.g. limits caching opportunities.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;Macros&lt;/em&gt; — macros require multiple passes to expand, expand to often surprising amounts of hidden code, and impose limitations on partial parsing. Procedural macros have negative impacts similar to build scripts.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;LLVM backend&lt;/em&gt; — LLVM produces good machine code, but runs relatively slowly.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;Relying too much on the LLVM optimizer&lt;/em&gt; — Rust is well-known for generating a large quantity of LLVM IR and letting LLVM optimize it away. This is exacerbated by duplication from monomorphization.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;Split compiler/package manager&lt;/em&gt; — although it is normal for languages to have a package manager separate from the compiler, in Rust at least this results in both cargo and rustc having imperfect and redundant information about the overall compilation pipeline. As more parts of the pipeline are short-circuited for efficiency, more metadata needs to be transferred between instances of the compiler, mostly through the filesystem, which has overhead.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;Per-compilation-unit code-generation&lt;/em&gt; — rustc generates machine code each time it compiles a crate, but it doesn't need to — with most Rust projects being statically linked, the machine code isn't needed until the final link step. There may be efficiencies to be achieved by completely separating analysis and code generation.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;Single-threaded compiler&lt;/em&gt; — ideally, all CPUs are occupied for the entire compilation. This is not close to true with Rust today. And with the original compiler being single-threaded, the language is not as friendly to parallel compilation as it might be. There are efforts going into parallelizing the compiler, but it may never use all your cores.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;Trait coherence&lt;/em&gt; — Rust's traits have a property called "coherence", which makes it impossible to define implementations that conflict with each other. Trait coherence imposes restrictions on where code is allowed to live. As such, it is difficult to decompose Rust abstractions into, small, easily-parallelizable compilation units.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;Tests next to code&lt;/em&gt; — Rust encourages tests to reside in the same codebase as the code they are testing. With Rust's compilation model, this requires compiling and linking that code twice, which is expensive, particularly for large crates.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Recent work on Rust compile times
&lt;/h2&gt;

&lt;p&gt;The situation isn't hopeless. Not at all. There is always work going on to improve Rust compile times, and there are still many avenues to be explored. I'm hopeful that we'll continue to see improvements. Here is a selection of the activities I'm aware of from the last year or two. Thanks to everybody who helps with this problem.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The Rust compile-time &lt;a href="https://github.com/rust-lang/rust/issues/48547" rel="noopener noreferrer"&gt;master issue&lt;/a&gt;

&lt;ul&gt;
&lt;li&gt;Tracks various work to improve compile times&lt;/li&gt;
&lt;li&gt;Contains a great overview of factors that affect Rust compilation performance and potential mitigation strategies&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;Pipelined compilation (&lt;a href="https://github.com/rust-lang/rust/issues/60988" rel="noopener noreferrer"&gt;1&lt;/a&gt;,&lt;a href="https://github.com/rust-lang/cargo/issues/6660" rel="noopener noreferrer"&gt;2&lt;/a&gt;,&lt;a href="https://internals.rust-lang.org/t/evaluating-pipelined-rustc-compilation/10199" rel="noopener noreferrer"&gt;3&lt;/a&gt;)&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Typechecks downstream crates in parallel with upstream codegen. Now on by default on the stable channel&lt;/li&gt;
&lt;li&gt;Developed by &lt;a href="https://github.com/alexcrichton" rel="noopener noreferrer"&gt;@alexcrichton&lt;/a&gt; and &lt;a href="https://github.com/nikomatsakis" rel="noopener noreferrer"&gt;@nikomatsakis&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;Parallel rustc (&lt;a href="https://internals.rust-lang.org/t/parallelizing-rustc-using-rayon/6606" rel="noopener noreferrer"&gt;1&lt;/a&gt;,&lt;a href="https://github.com/rust-lang/rust/issues/48685" rel="noopener noreferrer"&gt;2&lt;/a&gt;,&lt;a href="https://internals.rust-lang.org/t/help-test-parallel-rustc/11503/14" rel="noopener noreferrer"&gt;3&lt;/a&gt;)&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Runs analysis phases of the compiler in parallel. Not yet available on the stable channel&lt;/li&gt;
&lt;li&gt;Developed by &lt;a href="https://github.com/Zoxc" rel="noopener noreferrer"&gt;@Zoxc&lt;/a&gt;, &lt;a href="https://github.com/michaelwoerister" rel="noopener noreferrer"&gt;@michaelwoerister&lt;/a&gt;, &lt;a href="http://github.com/oli-obk" rel="noopener noreferrer"&gt;@oli-obk&lt;/a&gt;, and others&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;&lt;a href="https://blog.rust-lang.org/inside-rust/2019/12/02/const-prop-on-by-default.html" rel="noopener noreferrer"&gt;MIR-level constant propagation&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Performs constant propagation on MIR, which reduces duplicated LLVM work on monomorphized functions&lt;/li&gt;
&lt;li&gt;Developed by &lt;a href="https://github.com/wesleywiser" rel="noopener noreferrer"&gt;@wesleywiser&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;&lt;a href="https://github.com/rust-lang/rust/pulls?q=mir-opt" rel="noopener noreferrer"&gt;MIR optimizations&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Optimizing MIR should be faster than optimizeng monomorphized LLVM IR&lt;/li&gt;
&lt;li&gt;Not in stable compilers yet&lt;/li&gt;
&lt;li&gt;Developed by &lt;a href="https://github.com/wesleywiser" rel="noopener noreferrer"&gt;@wesleywiser&lt;/a&gt; and others&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;cargo build -Ztimings (&lt;a href="https://internals.rust-lang.org/t/exploring-crate-graph-build-times-with-cargo-build-ztimings/10975" rel="noopener noreferrer"&gt;1&lt;/a&gt;,&lt;a href="https://github.com/rust-lang/cargo/issues/7405" rel="noopener noreferrer"&gt;2&lt;/a&gt;)&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Collects and graphs information about cargo's parallel build timings&lt;/li&gt;
&lt;li&gt;Developed by &lt;a href="https://github.com/ehuss" rel="noopener noreferrer"&gt;@ehuss&lt;/a&gt; and &lt;a href="https://github.com/luser" rel="noopener noreferrer"&gt;@luser&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;rustc -Zself-profile (&lt;a href="https://rust-lang.github.io/rustc-guide/profiling.html" rel="noopener noreferrer"&gt;1&lt;/a&gt;,&lt;a href="https://github.com/rust-lang/rust/issues/58967" rel="noopener noreferrer"&gt;2&lt;/a&gt;,&lt;a href="https://github.com/rust-lang/rust/pull/51657" rel="noopener noreferrer"&gt;3&lt;/a&gt;)&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Generates detailed information about rustc's internal performance&lt;/li&gt;
&lt;li&gt;Developed by &lt;a href="https://github.com/wesleywiser" rel="noopener noreferrer"&gt;@wesleywiser&lt;/a&gt; and &lt;a href="https://github.com/michaelwoerister" rel="noopener noreferrer"&gt;@michaelwoerister&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;&lt;a href="https://github.com/rust-lang/rust/issues/47317" rel="noopener noreferrer"&gt;Shared monomorphizations&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Reduces code bloat by deduplicating monomorphizations that occur in multiple crates&lt;/li&gt;
&lt;li&gt;Enabled by default if the optimization level is less than 3.&lt;/li&gt;
&lt;li&gt;Developed by &lt;a href="https://github.com/michaelwoerister" rel="noopener noreferrer"&gt;@michaelwoerister&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;&lt;a href="https://www.reddit.com/r/rust/comments/enxgwh/cranelift_backend_for_rust/" rel="noopener noreferrer"&gt;Cranelift backend&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Reduced debug compile times by used &lt;a href="https://github.com/bytecodealliance/cranelift" rel="noopener noreferrer"&gt;cranelift&lt;/a&gt; for code generation.&lt;/li&gt;
&lt;li&gt;Developed by &lt;a href="https://github.com/bjorn3" rel="noopener noreferrer"&gt;@bjorn3&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;&lt;a href="https://perf.rust-lang.org/" rel="noopener noreferrer"&gt;perf.rust-lang.org&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Rust's compile-time performance is tracked in detail. Benchmarks continue to be added.&lt;/li&gt;
&lt;li&gt;Developed by&lt;a href="https://github.com/nrc" rel="noopener noreferrer"&gt;@nrc&lt;/a&gt;, &lt;a href="https://github.com/Mark-Simulacrum" rel="noopener noreferrer"&gt;@Mark-Simulacrum&lt;/a&gt;, &lt;a href="https://github.com/nnethercote" rel="noopener noreferrer"&gt;@nnethercote&lt;/a&gt; and many more&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;&lt;a href="https://github.com/RazrFalcon/cargo-bloat" rel="noopener noreferrer"&gt;cargo-bloat&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Finds what occupies the most space in binaries. Bloat is correlated with compile time&lt;/li&gt;
&lt;li&gt;Developed by &lt;a href="https://github.com/RazrFalcon" rel="noopener noreferrer"&gt;@RazrFalcon&lt;/a&gt; and others&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;&lt;a href="https://github.com/psinghal20/cargo-feature-analyst" rel="noopener noreferrer"&gt;cargo-feature-analyst&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Finds unused features&lt;/li&gt;
&lt;li&gt;Developed by &lt;a href="https://github.com/psinghal20" rel="noopener noreferrer"&gt;@psinghal20&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;&lt;a href="https://github.com/est31/cargo-udeps" rel="noopener noreferrer"&gt;cargo-udeps&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Finds unused crates&lt;/li&gt;
&lt;li&gt;Developed by &lt;a href="https://github.com/est31" rel="noopener noreferrer"&gt;@est31&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;&lt;a href="https://github.com/rustwasm/twiggy" rel="noopener noreferrer"&gt;twiggy&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Profiles code size, which is correlated with compile time&lt;/li&gt;
&lt;li&gt;Developed by &lt;a href="https://github.com/fitzgen" rel="noopener noreferrer"&gt;@fitzgen&lt;/a&gt;, &lt;a href="https://github.com/data-pup" rel="noopener noreferrer"&gt;@data-pup&lt;/a&gt;, and others&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;&lt;a href="https://github.com/rust-analyzer/rust-analyzer" rel="noopener noreferrer"&gt;rust-analyzer&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A new language server for Rust with faster response time than the original &lt;a href="https://github.com/rust-lang/rls" rel="noopener noreferrer"&gt;RLS&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Developed by &lt;a href="https://github.com/matklad" rel="noopener noreferrer"&gt;@matklad&lt;/a&gt;, &lt;a href="https://github.com/flodiebold" rel="noopener noreferrer"&gt;@flodiebold&lt;/a&gt;, &lt;a href="https://github.com/kjeremy" rel="noopener noreferrer"&gt;@kjeremy&lt;/a&gt;, and many others&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;&lt;a href="https://vfoley.xyz/rust-compile-speed-tips/" rel="noopener noreferrer"&gt;"How to alleviate the pain of Rust compile times"&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Blog post by vfoley&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;&lt;a href="https://raphlinus.github.io/rust/2019/08/21/rust-bloat.html" rel="noopener noreferrer"&gt;"Thoughts on Rust bloat"&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Blog post by &lt;a href="https://github.com/raphlinus" rel="noopener noreferrer"&gt;@raphlinus&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;Nicholas Nethercote's work on rustc optimization&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://blog.mozilla.org/nnethercote/2019/07/17/how-to-speed-up-the-rust-compiler-in-2019/" rel="noopener noreferrer"&gt;"How to speed up the Rust compiler in 2019"&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://blog.mozilla.org/nnethercote/2019/07/25/the-rust-compiler-is-still-getting-faster/" rel="noopener noreferrer"&gt;"The Rust compiler is still getting faster"&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://blog.mozilla.org/nnethercote/2019/10/10/visualizing-rust-compilation/" rel="noopener noreferrer"&gt;"Visualizing Rust compilation"&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://blog.mozilla.org/nnethercote/2019/10/11/how-to-speed-up-the-rust-compiler-some-more-in-2019/" rel="noopener noreferrer"&gt;"How to speed up the Rust compiler some more in 2019"&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://blog.mozilla.org/nnethercote/2019/12/11/how-to-speed-up-the-rust-compiler-one-last-time-in-2019/" rel="noopener noreferrer"&gt;"How to speed up the Rust compiler one last time in 2019"&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;I apologize to any person or project I didn't credit.&lt;/p&gt;

&lt;h2&gt;
  
  
  In the next episode
&lt;/h2&gt;

&lt;p&gt;So Rust dug itself deep into a corner over the years and will probably be digging itself back out until the end of time (or the end of Rust — same thing, really). Can Rust compile-time be saved from Rust's own run-time success? Will TiKV ever build fast enough to satisfy my managers?&lt;/p&gt;

&lt;p&gt;In the next episode, we'll deep-dive into the specifics of Rust's language design that cause it to compile slowly.&lt;/p&gt;

&lt;p&gt;Stay Rusty, friends.&lt;/p&gt;

&lt;h2&gt;
  
  
  Thanks
&lt;/h2&gt;

&lt;p&gt;A number of people helped with this blog series. Thanks especially to Niko Matsakis, Graydon Hoare, and Ted Mielczarek for their insights, and Calvin Weng for proofreading and editing.&lt;/p&gt;

</description>
      <category>rust</category>
      <category>compile</category>
      <category>tikv</category>
    </item>
  </channel>
</rss>
