18

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?

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
tnajdek
  • 542
  • 5
  • 14
  • Is cargo embedded as a static library? And if so, would embedding it as a dynamic library (at least in Debug) not speed up link time? – Matthieu M. Apr 11 '18 at 11:55

1 Answers1

7

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.

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
クリホ
  • 440
  • 4
  • 9
  • 4
    This does not seem like it will answer the question as it asks about fixing the *linking time*, which would not be changed with this solution. – Shepmaster Apr 09 '18 at 02:10
  • Sorry, if I misunderstood the question. I thought it might help because it looked like compiling servo is what takes OP so long. Does linking in Rust not happen after compiling? Should I delete my answer? – クリホ Apr 09 '18 at 05:34
  • @クリホ I'd wait for the OP's comment. – Boiethios Apr 13 '18 at 13:49
  • I suppose that a Divide-and-conquer solution is universal and the best. This is because it is universal and the only one. – Alex Koz. Apr 13 '18 at 16:41
  • Summon @Shepmaster . – Alex Koz. Apr 13 '18 at 16:43
  • @AlexKoz. if the solution *works*, then it's a good solution. I just don't understand how linking the same amount of code could be slower or faster. It makes me think that the problem isn't related to linking, but perhaps to something else. – Shepmaster Apr 13 '18 at 17:50
  • This solution speeds up _compile time_ including _linking time_. But _linking time_ **probably** does not decreasing. Anyway... I'll wait couple of hours and will give `+50`. – Alex Koz. Apr 13 '18 at 17:55