Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to speed up Rust compilation when linking a large library?

I'm experiencing long consecutive build times when embedding Servo as part of my binary. For example, using this tutorial for embedding Servo, after the initial build is done, any modification to my code will require 40s+ to rebuild the binary on 4th gen quad-core i7:

Here is sample output from Cargo:

❯ cargo build
    Finished dev [unoptimized + debuginfo] target(s) in 0.1 secs

❯ touch src/main.rs
❯ cargo build
    Compiling servo-embedding-example v0.1.0
     Finished dev [unoptimized + debuginfo] target(s) in 57.9 secs

I'm running a very recent nightly toolchain:

❯ rustc --version
rustc 1.25.0-nightly (15a1e2844 2018-01-20)
❯ cargo --version
cargo 0.26.0-nightly (6a8eb71f6 2018-01-13)

And incremental build is on:

❯ echo $CARGO_INCREMENTAL
1

I'm not planning on updating Servo often but I need it my application. Is there any way to speed up the build time?

like image 544
tnajdek Avatar asked Mar 12 '18 13:03

tnajdek


People also ask

Is Rust compilation slow?

But rustc is not a slow compiler — it implements the most advanced incremental compilation in industrial compilers, it takes advantage of compilation model based on proper modules (crates), and it has been meticulously optimized. Fast to compile Rust projects are a reality, even if they are not common.

Why does rust compile time slowly?

That is because the languages are different and choose different tradeoffs. The tradeoffs might make a language inapplicable in your domain. Rust has always been marketed as a "systems programming language". C++ is Rust's closes competitor in this domain space and it suffers from terribly slow compiling as well.


1 Answers

One thing you can do is separating your code that directly interacts with Servo from the rest of your project into its own library within your project.

project-rs
├── Cargo.toml
├── src
│   ├── bin
│   │   └── main.rs
│   ├── this_needs_servo
│   │   └── Cargo.toml
│   │   └── lib.rs
│   └── ui
│       └── Cargo.toml
│       └── lib.rs
└── tests
    └── tests.rs

You would add

[dependencies.this_needs_servo]
path = "src/this_needs_servo"

to the Cargo.toml in your projects root dir and move the dependency of servo into the Cargo.toml under your this_needs_servo lib.

To use this library you can add extern crate this_needs_servo; plus all the required use statements to your main.rs.

Now servo should only get compiled when you change something in the this_needs_servo lib.

like image 97
クリホ Avatar answered Sep 18 '22 15:09

クリホ