Skip to content

Cargo profiling improvements - #17411

Merged
weihanglo merged 3 commits into
rust-lang:masterfrom
ranger-ross:improve-cargo-profiling
Aug 30, 2026
Merged

Cargo profiling improvements#17411
weihanglo merged 3 commits into
rust-lang:masterfrom
ranger-ross:improve-cargo-profiling

Conversation

@ranger-ross

Copy link
Copy Markdown
Member

What does this PR try to resolve?

This PR makes a couple of small improvements to our profiling by:

  1. Removing/modifying trace!() calls that log large objects (UnitGraph) or all called from a hot loop (in the case of build_sbom_graph)
  2. Added more detailed instrumentation to the fn compile() while trying to avoid instrumenting leaf functions.

Below are some tracing examples from using profiling on the Zed repository.

Before

image

After

image

How to test and review this PR?

CARGO_LOG_PROFILE=true CARGO_LOG_PROFILE_CAPTURE_ARGS=true cargo build on your favorite repo :)

On large workspaces, the formatting `UnitGraph` can take a long time.
Notably on Zed, it was taking over 300ms on my machine, meaningfully
skewing the profiling results.

This commit makes dumping the unit graph opt-in with `__CARGO_DUMP_UNIT_DEP_GRAPH`
This trace! was in a hot loop skewing the timings when cargo profiling
is enabled.
As a side note `build_sbom_graph` does appear to take up a meaninful
amount of time in `prepare_rust` due to needing to walk the dependency
graph.
Added profiling instrumentation on more profiler related functions
to give more visiblity on where we spend time while preparing rustc
jobs.
@rustbot rustbot added A-build-execution Area: anything dealing with executing the compiler A-rebuild-detection Area: rebuild detection and fingerprinting S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Aug 30, 2026
@rustbot

rustbot commented Aug 30, 2026

Copy link
Copy Markdown
Collaborator

r? @epage

rustbot has assigned @epage.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: @epage, @weihanglo
  • @epage, @weihanglo expanded to epage, weihanglo
  • Random selection from epage, weihanglo

@weihanglo
weihanglo added this pull request to the merge queue Aug 30, 2026
Merged via the queue into rust-lang:master with commit 0c507b7 Aug 30, 2026
28 checks passed
@rustbot rustbot removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Aug 30, 2026
github-merge-queue Bot pushed a commit that referenced this pull request Aug 31, 2026
### What does this PR try to resolve?

Before this PR, the SBOM would be generated in memory and be thrown away
instead of written to a file. For projects with a large dependency
graph, this can add noticeable delay.

Below is a profiling run on Zed.

<img width="2065" height="908" alt="image"
src="https://github.com/user-attachments/assets/8ca00cc0-3498-4161-8c2b-0415f2006d9e"
/>

On a cold build, this is a very small part of the work, but we have to
pay this price on every build.
But when trying to do repeated builds while iterating this time starts
to add up.

### How to test and review this PR?

I primarily was doing profiling on Zed when I found this. (also see:
#17411)
```sh
CARGO_LOG_PROFILE=true CARGO_LOG_PROFILE_CAPTURE_ARGS=true cargo build
```
I did not add any additional tests since there aren't really any outputs
to compare against.
Hoping the existing SBOM tests are good enough, but happy to add a test
for this if there is a way I am missing :)

r? @arlosi

cc: @Shnatsel

cc: #16565
rust-bors Bot pushed a commit to rust-lang/rust that referenced this pull request Sep 2, 2026
Update cargo submodule



26 commits in e8cb624d5701824f46a2ec5873cfd59ee3d2f66c..b2e9d5f9db3fb1c454ab84f10c16508984a266e2
2026-08-22 00:23:45 +0000 to 2026-09-02 14:49:16 +0000
- fix(parser): Resolve theoretical use-after-free (rust-lang/cargo#17428)
- fix(trim-paths)!: remove default scope from release profile (rust-lang/cargo#17424)
- fix(git): Use git's 429 retry, when available (rust-lang/cargo#17422)
- Avoid passing search path (-L) args when they are passed as --extern (rust-lang/cargo#17410)
- chore(deps): update crate-ci/typos action to v1.50.0 (rust-lang/cargo#17417)
- test: Move -Z onto its own line (rust-lang/cargo#17416)
- chore(triagebot): enable `@rustbot merge/delegate` (rust-lang/cargo#17415)
- Micro-optimize two package dir functions (rust-lang/cargo#17413)
- perf: Do not build SBOM if user has not set build.sbom (rust-lang/cargo#17412)
- feat(manifest)!: implement feature-metadata RFC3416 (rust-lang/cargo#15056)
- Cargo profiling improvements (rust-lang/cargo#17411)
- test(git): Remove gix override run in CI and the mode in code (rust-lang/cargo#17405)
- perf(git): Reduce extra work when using git-cli (rust-lang/cargo#17406)
- feat(resolver): Stabilize min-publish-age (rust-lang/cargo#17335)
- fix(git): Remove ref status update when showing progress  (rust-lang/cargo#17400)
- revert: refactor: move sysroot lookup to GlobalContext (rust-lang/cargo#17401)
- fix(run): Printing a new line to avoid overwriting error code after \r (rust-lang/cargo#17373)
- fix(trim-paths): custom workspace-relative member paths remap (rust-lang/cargo#17366)
- fix(home): rustdoc lint (rust-lang/cargo#17394)
- feat(diag): Stabilize cargo-lints  (rust-lang/cargo#17298)
- chore(deps): Update partial_ref to v0.3.4 (rust-lang/cargo#17392)
- refactor: remove ad-hoc `subslice_range` (rust-lang/cargo#17390)
- docs(changelog): move build-dir new layout to Changed (rust-lang/cargo#17387)
- chore(deps): update msrv (1 version) to v1.98 (rust-lang/cargo#17386)
- docs: Use mdbook admonitions (rust-lang/cargo#17384)
- chore(ci): exclude resolver-tests from intra doc link checks (rust-lang/cargo#17385)
@rustbot rustbot added this to the 1.100.0 milestone Sep 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-build-execution Area: anything dealing with executing the compiler A-rebuild-detection Area: rebuild detection and fingerprinting

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants