Skip to content

Optimize dependency file search#153131

Open
Kobzol wants to merge 4 commits intorust-lang:mainfrom
Kobzol:filesearch-opt
Open

Optimize dependency file search#153131
Kobzol wants to merge 4 commits intorust-lang:mainfrom
Kobzol:filesearch-opt

Conversation

@Kobzol
Copy link
Member

@Kobzol Kobzol commented Feb 26, 2026

I tried to look into the slowdown reported in rust-lang/cargo#16665.

I created a Rust hello world program, and used this Python script to create a directory containing 200k files:

from pathlib import Path

dir = Path("deps")
dir.mkdir(parents=True, exist_ok=True)
for i in range(200000):
    path = dir / f"file{i:07}.o"
    with open(path, "w") as f:
        f.write("\n")

Then I tried to do various small microoptimalizations and simplifications to the code that iterates the search directories. Each individual commit improved performance, with the third one having the biggest effect.

Here are the results on main vs the last commit with the stage1 compiler on Linux, using hyperfine "rustc +stage1 src/main.rs -L deps" -r 30 (there's IO involved, so it's good to let it run for a while):

Benchmark 1: rustc +stage1 src/main.rs -L deps
  Time (mean ± σ):     299.4 ms ±   2.7 ms    [User: 161.9 ms, System: 144.9 ms]
  Range (min … max):   294.8 ms … 307.1 ms    30 runs

Benchmark 1: rustc +stage1 src/main.rs -L deps
  Time (mean ± σ):     208.1 ms ±   4.5 ms    [User: 87.3 ms, System: 128.7 ms]
  Range (min … max):   202.4 ms … 219.6 ms    30 runs

Would be cool if someone could try this on macOS (maybe @ehuss - not sure if you have macOS or you only commented about its behavior on the Cargo issue :) ).

I also tried to prefilter the paths (not in this PR); right now we load everything and then we filter files with given prefixes, that's wasteful. Filtering just files starting with lib would get us down to ~150ms here. (The baseline without -L is ~80ms on my PC). The rest of the 70ms is essentially allocations from iterating the directory entries and sorting. That would be very hard to change - iterating the directory entries (de)allocates a lot of intermediate paths :( We'd have to implement the iteration by hand with either arena allocation, or at least some better management of memory.

r? @nnethercote

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Feb 26, 2026
@Kobzol
Copy link
Member Author

Kobzol commented Feb 26, 2026

Not expecting any big wins there, but let's check:

@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

@rust-bors

This comment has been minimized.

@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Feb 26, 2026
rust-bors bot pushed a commit that referenced this pull request Feb 26, 2026
Optimize dependency file search
@rust-bors
Copy link
Contributor

rust-bors bot commented Feb 26, 2026

☀️ Try build successful (CI)
Build commit: 71d4456 (71d445654a32204fbf86bfec11e657fb97adcd22, parent: 69b78537fac74de40f009b076bcbbf54b77683ad)

@rust-timer

This comment has been minimized.

@ehuss
Copy link
Contributor

ehuss commented Feb 26, 2026

Unfortunately I am unable to test because of #153077, I am unable to get rustc to build.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (71d4456): comparison URL.

Overall result: ❌✅ regressions and improvements - please read the text below

Benchmarking this pull request means it may be perf-sensitive – we'll automatically label it not fit for rolling up. You can override this, but we strongly advise not to, due to possible changes in compiler perf.

Next Steps: If you can justify the regressions found in this try perf run, please do so in sufficient writing along with @rustbot label: +perf-regression-triaged. If not, please fix the regressions and do another perf run. If its results are neutral or positive, the label will be automatically removed.

@bors rollup=never
@rustbot label: -S-waiting-on-perf +perf-regression

Instruction count

Our most reliable metric. Used to determine the overall result above. However, even this metric can be noisy.

mean range count
Regressions ❌
(primary)
0.3% [0.2%, 0.3%] 4
Regressions ❌
(secondary)
0.0% [0.0%, 0.0%] 1
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-0.4% [-0.6%, -0.1%] 7
All ❌✅ (primary) 0.3% [0.2%, 0.3%] 4

Max RSS (memory usage)

Results (secondary 0.1%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
6.8% [6.8%, 6.8%] 1
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-6.7% [-6.7%, -6.7%] 1
All ❌✅ (primary) - - 0

Cycles

This benchmark run did not return any relevant results for this metric.

Binary size

This benchmark run did not return any relevant results for this metric.

Bootstrap: 480.132s -> 480.258s (0.03%)
Artifact size: 395.80 MiB -> 395.77 MiB (-0.01%)

@rustbot rustbot added perf-regression Performance regression. and removed S-waiting-on-perf Status: Waiting on a perf run to be completed. labels Feb 26, 2026
@ehuss
Copy link
Contributor

ehuss commented Feb 26, 2026

OK, got some data:

With this PR:

Benchmark 1: ~/Proj/rust/rust/build/aarch64-apple-darwin/stage1/bin/rustc src/main.rs -L deps
  Time (mean ± σ):     197.4 ms ±   3.9 ms    [User: 143.7 ms, System: 117.9 ms]
  Range (min … max):   192.4 ms … 207.7 ms    30 runs

Without:

Benchmark 1: ~/Proj/rust/rust/build/aarch64-apple-darwin/stage1/bin/rustc src/main.rs -L deps
  Time (mean ± σ):     248.9 ms ±   5.1 ms    [User: 189.5 ms, System: 124.2 ms]
  Range (min … max):   241.1 ms … 262.6 ms    30 runs

The tests are super noisy, though (I get significantly different results between runs). But it seems like a pretty good win.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

perf-regression Performance regression. S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants