Use a set when checking visited workspace members - #17180
Conversation
There was a problem hiding this comment.
This looks generally good to me, though I guess after #17169 we should leverage IndexSet directly.
There was a problem hiding this comment.
I assume you meant HashSet? :) Because we don't need to deal with iteration order here. Changed it to HashSet.
There was a problem hiding this comment.
Sorry not being clearer. I meant instead of Vec we use IndexSet for storing members.
There was a problem hiding this comment.
Ah, I see! Did that, and confirmed that the performance is essentially the same as the previous version of this PR, at least on Zed. The second run is the previous version, the third run is IndexSet.
Benchmark 1 (4 runs): /projects/personal/rust/cargo/cargo-baseline check
measurement mean ± σ min … max outliers delta
wall_time 1.47s ± 36.0ms 1.44s … 1.52s 0 ( 0%) 0%
peak_rss 268MB ± 436KB 267MB … 268MB 0 ( 0%) 0%
cpu_cycles 2.85G ± 74.9M 2.78G … 2.96G 0 ( 0%) 0%
instructions 5.23G ± 174K 5.23G … 5.23G 0 ( 0%) 0%
cache_references 197M ± 676K 197M … 198M 0 ( 0%) 0%
cache_misses 57.8M ± 380K 57.4M … 58.2M 0 ( 0%) 0%
branch_misses 17.8M ± 106K 17.7M … 17.9M 1 (25%) 0%
Benchmark 2 (4 runs): ./cargo-set check
measurement mean ± σ min … max outliers delta
wall_time 1.43s ± 11.8ms 1.42s … 1.45s 0 ( 0%) - 2.8% ± 3.2%
peak_rss 268MB ± 203KB 268MB … 268MB 0 ( 0%) + 0.1% ± 0.2%
cpu_cycles 2.74G ± 9.39M 2.73G … 2.75G 0 ( 0%) - 3.8% ± 3.2%
instructions 5.02G ± 9.14M 5.02G … 5.04G 0 ( 0%) ⚡- 3.9% ± 0.2%
cache_references 194M ± 1.39M 193M … 196M 0 ( 0%) - 1.6% ± 1.0%
cache_misses 55.8M ± 544K 55.2M … 56.4M 0 ( 0%) ⚡- 3.5% ± 1.4%
branch_misses 17.7M ± 167K 17.5M … 17.9M 0 ( 0%) - 1.0% ± 1.4%
Benchmark 3 (4 runs): ./cargo-perf check
measurement mean ± σ min … max outliers delta
wall_time 1.42s ± 4.44ms 1.41s … 1.42s 0 ( 0%) - 3.7% ± 3.0%
peak_rss 268MB ± 275KB 268MB … 268MB 0 ( 0%) + 0.0% ± 0.2%
cpu_cycles 2.73G ± 9.98M 2.72G … 2.74G 0 ( 0%) - 4.2% ± 3.2%
instructions 5.02G ± 67.0K 5.02G … 5.02G 1 (25%) ⚡- 4.0% ± 0.0%
cache_references 195M ± 634K 194M … 196M 1 (25%) - 1.1% ± 0.6%
cache_misses 56.9M ± 147K 56.7M … 57.0M 0 ( 0%) - 1.7% ± 0.9%
branch_misses 17.4M ± 70.2K 17.3M … 17.5M 0 ( 0%) ⚡- 2.6% ± 0.9%
a0cada3 to
78f22a2
Compare
|
r? @weihanglo rustbot has assigned @weihanglo. Use Why was this reviewer chosen?The reviewer was selected based on:
|
78f22a2 to
a4b3574
Compare
|
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. |
Update cargo submodule 4 commits in 2f0e7011e0e9cb30cb772bf2ec1d69dce4dff4f3..59800466c5c41c444d264b1010b4d57e85a7117f 2026-07-05 12:10:34 +0000 to 2026-07-07 15:52:22 +0000 - Revert "feat: Stablize build-dir layout v2" (rust-lang/cargo#17187) - Use a set when checking visited workspace members (rust-lang/cargo#17180) - Stabilize `build-dir` layout v2 (rust-lang/cargo#16807) - Change HashMaps and HashSets in Cargo to use Fxhasher (rust-lang/cargo#17169)
While profiling Cargo, I started gathering a set of micro-optimizations on the side that help for stress tests, e.g. Zed. Most of those did not seem worth landing on their own, but this one provided non-trivial improvements, while being a reasonably simple change, so I thought I'd post it as a PR to see what you think.
When finding workspace members, the
self.members.containscheck can be quite hot. Not necessarily because of traversing through aVec, because that itself is quite fast, butEqforPathBufis not necessarily so fast. The same result can be gained by switching the workspacemembersfield to a set, but that would haveOn Zed, this was a ~1.5% wall-time and ~4% icount improvement, on bors it was also a ~1.5% wall-time win.
I think that it shouldn't be behavior altering, but I'll defer to CI and tests.