Repository navigation
Tracking Issue for making incremental compilation the default for Release Builds #57968
Description
Since incremental compilation supports being used in conjunction with ThinLTO the runtime performance of incrementally built artifacts is (presumably) roughly on par with non-incrementally built code. At the same time, building things incrementally often is significantly faster ((1.4-5x according to perf.rlo). As a consequence it might be a good idea to make Cargo default to incremental compilation for release builds.
Possible caveats that need to be resolved:
- The initial build is slightly slower with incremental compilation, usually around 10%. We need to decide if this is a worthwhile tradeoff. For
debugandcheckbuilds everybody seems to be fine with this already. - Some crates, like
style-servo, are always slower to compile with incr. comp., even if there is just a small change. In the case ofstyle-servothat is 62 seconds versus 64-69 seconds on perf.rlo. It is unlikely that this would improve before we make incr. comp. the default. We need to decide if this is a justifiable price to pay for improvements in other projects. - Even if incremental compilation becomes the default, one can still always opt out of it via the
CARGO_INCREMENTALflag or a local Cargo config. However, this might not be common knowledge, the same as it isn't common knowledge that one can improve runtime performance by forcing the compiler to use just one codegen unit. - It still needs to be verified that runtime performance of compiled artifacts does not suffer too much from switching to incremental compilation (see below).
Data on runtime performance of incrementally compiled release artifacts
Apart from anectodal evidence that runtime performance is "roughly the same" there have been two attempts to measure this in a more reliable way:
- PR [experiment] Benchmark incremental ThinLTO'd compiler. #56678 did an experiment where we compiled the compiler itself incrementally and then tested how the compiler's runtime performance was affected by this. The results are twofold:
- In general performance drops by 1-2% (compare results for
cleanbuilds) - For two of the small test cases (
helloworld,unify-linearly) performance drops by 30%. It is known that these test cases are very sensitive to LLVM making the right inlining decisions, which we already saw when switching from single-CGU to non-incremental ThinLTO. This is indicative that microbenchmarks may see performance drops unless the author of the benchmark takes care of marking bottleneck functions with#[inline].
- In general performance drops by 1-2% (compare results for
- For a limited period of time we made incremental compilation the default in Cargo (Make incremental compilation the default for all profiles. cargo#6564) in order to see how this affected measurements on lolbench.rs. It is not yet clear if the experiment succeeded and how much useful data it collected since we had to cut it short because of a regression (Nightly regression: Can't perform LTO when compiling incrementally #57947). The initial data looks promising: only a handful of the ~600 benchmarks showed performance losses (see https://lolbench.rs/#nightly-2019-01-27). But we need further investigation on how reliable the results are. We might also want to re-run the experiment since the regression can easily be avoided.
One more experiment we should do is compiling Firefox because it is a large Rust codebase with an excellent benchmarking infrastructure (cc @nnethercote).
cc @rust-lang/core @rust-lang/cargo @rust-lang/compiler
Activity
joshtriplett commentedon Jan 29, 2019
michaelwoerister commentedon Jan 29, 2019
I'm not sure. The current default already has a quite significant runtime performance cost because it's using ThinLTO instead of
-Ccodegen-units=1.alexcrichton commentedon Jan 29, 2019
We've had a ton of discussions before about comiple time and runtime tradeoffs, see #45320 and #44941 for just a smattering. We are very intentionally not enabling the fastest compilation mode with
cargo build --releaseby default today, and an issue like this is a continuation of that.joshtriplett commentedon Jan 30, 2019
@alexcrichton To avoid ambiguity, what do you mean by "fastest compilation mode" here?
I certainly think we don't need to worry about compiling as fast as possible, but I don't think our default compile should pay a runtime performance penalty like this.
alexcrichton commentedon Jan 30, 2019
Ah by that I mean that producing the fastest code possible. Producing the fastest code by default for
--releasewould mean things like LTO, enabling PGO, customizing the LLVM pass manager to just rerun itself to either a fixed point or until some amount of time lapses, etc.Lokathor commentedon Feb 2, 2019
So if
releaseis a "best effort at being fast while still finishing the build sometime today", can we just add a different profile for "really the fastest but it'll take a day to build".CryZe commentedon Feb 2, 2019
Yeah I'm honestly thinking that it may be time for a profile between debug and release, such that there is these use cases:
At the moment I'm seeing lots of people either sacrifice the debug profile for that "Development" use case (bumping optimization levels, but reducing the debugability of the project) or sacrifice the release profile by reducing optimizations, both are kind of suboptimal.
lnicola commentedon Feb 2, 2019
rust-lang/cargo#2007
rust-lang/cargo#5326 (comment)
rust-lang/rfcs#2282
This came up a lot of times, but for some reason was never implemented. The discussions about it turned into talk about "workflows" and "profile overrides", although it's not very clear to me why:
9 remaining items
eddyb commentedon Mar 29, 2019
@michaelwoerister Could NTFS' infamous low performance on many small files be at fault here?
Any chance we can coalesce some of those files together?
mati865 commentedon Mar 29, 2019
I'd rather blame Windows Defender which is enabled by default on Windows 10.
If it's the case you will see it in task manager squishing everything out of single core.
lnicola commentedon Mar 29, 2019
Re Defender, I've seen someone investigating parallel extraction of the installation packages to split the scanning across multiple cores, since it's synchronous.
Does the compiler use multiple threads when writing the files? Or, going the other way, would sticking then in a SQLite database help?
wesleywiser commentedon Jun 24, 2022
Visited during the compiler team's backlog bonanza. Reading over the discussion, it seems like we still haven't decided if it makes sense to enable incremental compilation by default for release builds.
michaelwoerister commentedon Jun 27, 2022
At this time, I am strongly against making incr. comp. the default for release builds because that's the mode that's usually being used for production builds -- and incr. comp. is not a good choice for that because of increased code size, less effective optimizations, and the added risks for running into incr. comp. specific compiler bugs.
That being said, optimized incremental builds certainly have their use cases. My recommendation would be for people to use Cargo custom profiles in those cases.
In my opinion the stabilization of Cargo custom profiles makes this issue obsolete.
eddyb commentedon Jun 27, 2022
I think the conflation of "debug" and "dev" in Cargo was a bit of a mistake, especially since
devhas more interesting implications.Having some kind of "devopt" profile by default would've been great, especially if it includes some minimal amount of debuginfo (which would help with e.g. profiling, something one might want to do for local development with optimizations turned on).
I'm personally fine with adding stuff like this to all my workspace-level
Cargo.toml, but tbh I'd even forgotten about turning on incremental in release mode:eddyb commentedon Aug 9, 2022
Parts of what I said in my last comment above (#57968 (comment)) keep coming up for me.
Most recently, a coworker was wondering why some backtrace was missing almost any useful information (there's even some frames there that make no sense, so the frame pointer is probably not used consistently either).
You may ask "what does debuginfo have to do with incremental"? But that's not why I bring it up - the connection to this issue is that both "missing incremental while developing" and "missing debuginfo while developing" are side-effects of
--release."Development" vs "Release"
Cargo came with two profiles (until the recent custom ones):
dev(the default)-C opt-level=0,-C debuginfo=2, incremental enabledrelease(opt-in via--release)-C opt-level=3,-C debuginfo=0, incremental disabledThree different aspects of compilation, toggled with one convenient switch.
But if we were to treat those aspects as independent, we may get this:
0(disabled): sometimes acceptable (when none of the below usecases apply)1("line info" only):2("full", incl. types and values, on top of the above):cargo install, distros, companies, etc.)cargo install) is0gdbto use build-id to show up a package manager install command, or even havegdbget everything on the fly itself (which might not even require patching nowadays)Instead, we fossilized
dev= "debug mode" and "optimizations" = "releasemode".Two major "unaddressed usecases" jump out at me right away, from the above presentation:
dev-opt: development with more optimizations, less debuginfo, but still incrementalrel-dbg: release with (split) debuginfoHowever, I'll focus on
dev-opt(more relevant to this issue and what to do aboutrel-dbgis less clear to me), which is also what prompted me to write this analysis after a rant on twitter.My informal proposal (
cargo build -O)Since custom Cargo profiles were stabilized, people can do this themselves:
And this is, as per the above analysis, close to the ideal development experience with optimizations (e.g.
debug = 2is more rarely needed and less useful given optimizations).But
--profile dev-optis still not as convenient as--release(on top of changing yourCargo.toml), and it's more likely that some projects will just redefine--releaseinstead of making a new profile (or they may have done it before custom profiles and don't see a good reason to switch now).What I want to pass by @rust-lang/cargo, as a "vibe check", is introducing an
-Oflag for Cargo (vaguely analogous to similar flags in other tooling, indicating "enable optimizations"), that is a shorthand profile selector (like--release) but for the abovedev-optprofile.Off the top of my head, some potentially relevant details:
cargo build -Ocould be likerustc -Oin being-C opt-level=2instead of3, but if that puts some people off I don't think it's a big deal to "diverge" theredev-optcustom profile (with theinheritskey), it could be backwards-compatible to not allow-Ousage and/or warn about the conflict without impacting--profile dev-opt(i.e. the only way they would be using it today)--releasedebug = 1might limit impact of incremental (relative todebug = 0), but at the same time, most people use incremental today withdebug = 2, and either way I'm not sure how big of a deal this is currentlyI'm not yet considering writing a full proposal, so if you're reading this in the future and wondering what happened with this idea ("fizzled out", most likely), get in touch.
ehuss commentedon Aug 30, 2022
We discussed a little about having a
-Oflag as a shorter alias, and I don't think it is something we will would like to add at this time. There are some concerns, such as the exact semantics not being clear (if it replaces the profile, or augments the current one), how it is quite a bit different fromrustc -O, how it relates to-r(both are "optimized"?), and how common such a workflow would be where people would need at least three different profiles. Aliases provide a workaround for the concern about the command-line length, and I would encourage using that as needed.eddyb commentedon Aug 31, 2022
I would argue you basically would never write
--releaseagain outside of some CI job, distro packaging, etc.(
cargo installwould still default to it but you also don't have to write it today either)The development workflow isn't "three profiles", it's two (i.e. the development ones, with "release" being "not development") - or just one, really (I almost always end up using debug mode to avoid typing
--release- I'll also be honest and say I have never seen anyone suggest using-r, even if it's right there in the--helpoutput).The summary makes me worried the discussion overfocused on the
-Owhich was my "quick hack" to plausibly get quicker adoption (since it's both shorter than--release- assuming most people don't know about/use-r- and much clearer in the intent of "I want optimizations"), but the real problem IMO is the conflation of "optimized development" w/ "release".If I had the time and energy I would probably make an RFC, but also this may be one of those things where nothing short of a time machine will really fix it. OTOH we have editions, and the Cargo resolver 2.0 saga is a good precedent - it might be nice to deprecate
-r/--releaseand havecargo run -O¹ vscargo run --profile release.¹ (or any other way to spell "optimized development", I'm not that attached to
-Ospeficially)Sadly there's no good way to do this for profiles without ending up with
cargo run-O,cargo build-O,cargo rustc-O,cargo test-Oetc. (also, while I can do all sorts of weird things locally to improve my experience, I can't share a command without manually expanding it first, and it can't really be taught as a workflow)Lokathor commentedon Aug 31, 2022
if you want development with optimizations, why not just set an optimization level? Similarly, if you want release with split debug, isn't that something you can set in the profile? (i admit I've never had to set split debug, on windows it's always split).
eddyb commentedon Aug 31, 2022
I go into far more detail above, but the obvious choices today are:
RUSTFLAGS(big hammer, won't really work well as it with fight w/ Cargo for many flags)devprofile (losing access to the unoptimized default)Cargo.toml/.cargo/config.toml, and then you have to pass--profile ..., but at least you get to keep the original profile)One trick I've never seen used, would be to either set
CARGO_PROFILE_dev_OPT_LEVEL=3or pass--config profile.dev.opt-level=3- but the semantics of that are that of "modify thedevprofile" and will trash the build artifacts any time you forget to pass it (or intentionally don't) - just like messing withRUSTFLAGS.And for all of those options (unless you start with the
releaseprofile and "add back" incremental and maybe a bit of debuginfo, to it) will leave you with other settings (like debuginfo level) that you have to also override to get what you want, so it's not as simple as "optimization level".The closest thing to what I want is a custom profile, except for it not already existing in everyone's config.
Not to sound like a broken record but my point was that we screwed up with the "dev=debug vs opt=release" split, and that if we want to address that, we have to switch people off
--releasesomehow.With a
-Pshorthand for--profile, I could see-Pdev-optworking (same length to--release, too), but then either everyone has to define it in their.cargo/config.toml, or in each workspaceCargo.tomls (that would reduce the effort but we'd likely end up with baroque per-project profiles and more confusion), or we provide it by default.Of course there's nobody stopping anyone from taking that to
-Poptor even-POkinds of extremes, if they're willing to define their own profiles, and I might personally do that if we get anything like a-Pshorthand for--profile(and nothing for thedev-optusecase).Not something I've ever done personally, so I can't answer this very well, but AFAIK this is generally handled by the release pipeline you have around the
cargo build --release- you don't just run that command and then zip uptargetto ship it to users, there's several things you need to also do. (I'm not sure this is the best example, but thesentry.iodocs have some additional details though they only seem to explicit describe the thing I was looking for, for wasm)Lokathor commentedon Aug 31, 2022
Sorry, the post with the
cargo build -Oproposal didn't seem to explain why a person wouldn't turn on optimizations in the dev profile when I read it.I would suggest to turn
devinto the optimized + debug and make an extra profile for "ultra slow" for the rare cases when you need the ultra slow build. My own experience is that it's usually not needed. That may be my own style of programming, I don't use the step debugger, I forgot how to even turn it on.I agree with you that cargo's two-profile system is inadequate. I just set
devto "whatever i want cargo build to do", which is usually full optimization + incremental, and then leave release as the "what should cargo install do?" profile and basically never use it myself.apiraino commentedon Mar 20, 2025
Note for self: issue was visited during a T-compiler meeting: we're thinking maybe incremental compilation not as a default. In any case, we will revisit this issue during triaging.