Exploring Crate Graph Build Times with `cargo build -Ztimings`

Using lld and no debug info really helped, thank you so much! I went from ~5.5 seconds with a hot cache to ~4 seconds with a cold cache and ~.5 seconds with a hot cache, that makes small changes much more pleasant. It didn't help as much with tests, maybe because they don't usually have a hot cache? Still very helpful, thank you.


I am interested in trying this on rustc itself, but rustc uses the x.py script for the top level build commands, rather than cargo itself. So how should I invoke this tool for rustc? Thanks!


You could place a cargo.arg("-Ztimings"); after

That will pass -Ztimings to every cargo invocation performed by ./x.py.

It seems no cargo-timing.html file is generated when the build fails.

1 Like

I have reduced clean compilation time of cg_clif with more than 2min with help of -Ztimings:


This is a great feature to have. Thank you :heart:

I almost missed this as it was already off of the front page of irlo, luckily the weekly summary email had it in there. Here’s a bump for anyone else who missed this.

1 Like

This looks great!

Here is Servo build from cargo clean on a 28-thread machine, with "Min unit time" set to 10 seconds. (There are 658 units in total.) We already knew parallelism opportunities were low at the end of the build, but it’s striking to see this "CPU Usage" graph!


I tried that and did ./x.py build --stage 2 src/rustc and it didn't work. At least, no output was produced.

Will x.py be using the installed nightly Cargo, or does it use its own version?

x.py default to downloading and using the compiler specified in src/stage0.txt. You can copy config.toml.example to config.toml and specify cargo and rustc keys in the [build] section to replace it. (I think there’s a command-line equivalent, but I don’t know where it’s documented.)


Would it be possible to output SVG instead of using a canvas? One of my concerns is being able to quickly find (as in Ctrl-F) crates in the build timeline.


It was originally written with SVG, but the rendering performance was unbearable.


From a naive look, it seems that this "script" crate should be your next build optimization target :wink:

(And if it cannot be broken down in smaller crates easily, it also seems like an excellent test case for the ongoing rustc parallelization work)

Thank you for this observation. I’ll just leave this here: https://github.com/servo/servo/issues/1799


Time to give that poor abandoned issue the attention that it deserves, then :slight_smile:

Ugh. If I have to spell it out…

That the script crate takes a long time to compile is not news. As the issue shows, we were discussing it five years ago. -Ztimings helps quantify exactly how bad it is, but Cargo has always printed Compiling… lines and you don’t need a precise timer to feel that multiples minutes is long, especially when you’re in the middle of the usual edit-compile-test cycle.

Everyone who contributes to Servo feels this pain. To suggest that it didn’t occur to us to try to improve the situation or that we merely failed to give it a little attention is, frankly, a bit insulting.

The reason this is still bad is that actually fixing it is really hard. Crates cannot have a dependency cycle, but items within a crate can. We had a GSOC student work multiple month on a possible approach, but that did not turn out as well as we’d hoped.


I tried this and again failed. If anyone gets it working and can give specific instructions, I would be grateful. I opened a PR to get this directly supported in x.py.

Do you have a link to the SVG version? I have some people who would be interested in that perf problem.


I'm sorry, I never got a notification for your response. (For some reason irlo discourse notifications are very spotty for me.)

Here's one using SVG: https://gistpreview.github.io/?dcccceb93a87316fcf455f7786ddb333

Here's one using Canvas: https://gistpreview.github.io/?65f4961cf2ac77e96f456ce02d1fddb2

If you scroll around, the SVG one is often blank and then pops into place, sometimes taking a second or two. It depends on which browser/os/gpu you have, but even on somewhat fast machines it was very noticeable to me.

You can check out commit 095f154f3753a1e9c724ec6b6d0abc6ce4ef2dc6 of the cargo repo to get the last version that had SVG support (it was removed in 77a47b3e3282c549192e96e71c4f535baccb9e0b).

1 Like

Oh, I love the SVG version. It draws instantly on both Firefox and Chromium with my iGPU, and it's searchable. True, the Firefox Dev Tools seem to barf on it, but that also happens on the canvas version.

1 Like

I see the rendering delays, thanks. I'll pass this on to some people I know to try to improve the Chromium performance, at least. :slight_smile: