Skip to content

Avoid passing search path (-L) args when they are passed as --extern - #17410

Merged
rustbot merged 4 commits into
rust-lang:masterfrom
ranger-ross:deduplicate-rustc-args
Sep 1, 2026
Merged

rustbot merged 4 commits into
rust-lang:masterfrom
ranger-ross:deduplicate-rustc-args

Conversation

@ranger-ross

@ranger-ross ranger-ross commented Aug 30, 2026

Copy link
Copy Markdown
Member

What does this PR try to resolve?

This PR reduces the -L args passed rustc by omitting direct dependencies as they are passed via --extern. (which makes also passing via -L unnecessary)

See #t-cargo > Reducing number of arguments passed with the new build dir for context.

I tested this change on a handful of open source projects to see the results and this generally reduces the amount of args by 5-20%.

Notable results:

  • Zed reduced top level crate args from 1189 to 1136. A 5% reduction
  • Vector reduced top level crate args from 934 to 867. A 7% reduction

How to test and review this PR?

Added a new test to explicitly verify this behavior.

r? @weihanglo

cc: @Kobzol @petrochenkov

@rustbot rustbot added A-build-execution Area: anything dealing with executing the compiler S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Aug 30, 2026

@Kobzol Kobzol left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for working on this! The logic looks simple enough, though the double checks for proc macros/build scripts are a bit annoying.

This is more a question for Vadim, but I assume that the order of the -L flags is not important?

View changes since this review

Comment thread src/compiler/mod.rs
if v.target.proc_macro() {
continue;
}
if !visited.contains(v) {

@Kobzol Kobzol Aug 30, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With this contains:

  • Already visited: hashmap lookup
  • Not visited: hashmap lookup + stack push + clone of Unit + hashmap lookup in the next iter

Without the contains:

  • Already visited: stack push + clone of Unit + hashmap lookup in the next iter
  • Not visited: stack push + clone of Unit + hashmap lookup in the next iter

With switching this to insert, and removing the insert from start of the loop:

  • Already visited: hashmap lookup + clone of Unit
  • Not visited: hashmap lookup + clone of Unit

Probably there are more unvisited hits than visited hits, and the lookup isn't that expensive. Just wanted to note some alternatives. I can try benchmarking it later.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nevermind, doing the path cloning/sorting is way more expensive than this loop on Zed. No need to look into this further, IMO.

@petrochenkov

Copy link
Copy Markdown
Contributor

I assume that the order of the -L flags is not important?

The lookup is supposed to search the directories in the order of -L flags.
For native libraries it's important, but IIRC for Rust libraries there was an ambiguity error if there are several candidates in different directories, although I'm not sure, need to check the code.

@ranger-ross
ranger-ross force-pushed the deduplicate-rustc-args branch from 14a65d4 to afd71df Compare August 31, 2026 06:47
@rustbot

rustbot commented Aug 31, 2026

Copy link
Copy Markdown
Collaborator

This PR was rebased onto a different master commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

pull Bot pushed a commit to Mu-L/cargo that referenced this pull request Aug 31, 2026
Looked into this while profiling
rust-lang#17410
([context](https://rust-lang.zulipchat.com/#narrow/channel/246057-t-cargo/topic/Reducing.20number.20of.20arguments.20passed.20with.20the.20new.20build.20dir/with/620216426)).

Those two changes reduce the total duration of collecting dep dirs from
~56ms to ~40ms on Zed. Not a huge win, but for such a small change.. The
`format!` change is a bit annoying (there the win was ~5ms), but the
`build_dir` change is IMO straightforward and just worth it to reduce an
unnecessary `PathBuf` copy (there the win was ~10ms).

r? @ranger-ross

@weihanglo weihanglo left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment thread tests/testsuite/build.rs Outdated
//
// FIXME: Remove the `-L dependency=[ROOT]/foo/target/debug/build/my-direct-dep/[HASH]/out` once fixed
p.cargo("-v build")
.enable_mac_dsym()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I probably forgot. Why is enable_mac_dsym needed?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am also trying to remember why, I always add it because I copy/paste existing testing as a skeleton when writing.

If I recall correctly, the split debug info files on macos have really nasty names that make them not play nice with the build-dir file-system snapshot testing tool.
But its probably not needed here. If the commit message from a4f0988 is to believed it should be faster?
I'll remove it and see if CI passes

Comment thread src/compiler/mod.rs
// directly. So instead we only sort valid Utf8 paths as &str's which is a bit faster.
// We do not allow non utf8 paths when building, so we take advantage of that by treating them
// as a single value when sorting/deduplicating.
paths.sort_unstable_by(|a, b| match (a.to_str(), b.to_str()) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if we want to include some links and numbers here for future references.

(git blame helps that, though it is a bit cumbersome to use)

@weihanglo weihanglo left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ranger-ross feel free to @rustbot merge when you feel it is ready :)

View changes since this review

@ranger-ross
ranger-ross force-pushed the deduplicate-rustc-args branch from 6f618bd to 5334f1f Compare September 1, 2026 12:53
@ranger-ross

Copy link
Copy Markdown
Member Author

@rustbot merge

@rustbot
rustbot enabled auto-merge September 1, 2026 13:01
@rustbot
rustbot added this pull request to the merge queue Sep 1, 2026
Merged via the queue into rust-lang:master with commit 3167045 Sep 1, 2026
28 checks passed
@rustbot rustbot removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Sep 1, 2026
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
ved-merget pushed a commit to Pufferfish-Merget/bench-cargo that referenced this pull request Sep 15, 2026
Looked into this while profiling
rust-lang/cargo#17410
([context](https://rust-lang.zulipchat.com/#narrow/channel/246057-t-cargo/topic/Reducing.20number.20of.20arguments.20passed.20with.20the.20new.20build.20dir/with/620216426)).

Those two changes reduce the total duration of collecting dep dirs from
~56ms to ~40ms on Zed. Not a huge win, but for such a small change.. The
`format!` change is a bit annoying (there the win was ~5ms), but the
`build_dir` change is IMO straightforward and just worth it to reduce an
unnecessary `PathBuf` copy (there the win was ~10ms).

r? @ranger-ross
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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants