Skip to content

mono collector: Reduce # of locking while walking the graph #140442

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

osiewicz
Copy link
Contributor

@osiewicz osiewicz commented Apr 29, 2025

While profiling Zed's dev build I've noticed that while most of the time upstream_monomorphizations takes a lot of time in monomorpization_collector, in some cases (e.g. build of editor itself) the rest of monomorphization_collector_graph_walk dominates it. Most of the time is spent in collect_items_rec.

This PR aims to reduce the number of locks taking place; instead of locking output MonoItems once per children of current node, we do so once per current node. We also get to reuse locks for mentioned and used items. While this commit does not reduce Wall time of Zed's build, it does shave off CPU time (measured with cargo build -j1) from 48s to 47s. I've also tested it with parallel frontend against Zed and ripgrep and found no regressions.

While profiling Zed's dev build I've noticed that while most of the time `upstream_monomorphizations` takes a lot of time in monomorpization_collector, in some cases (e.g. build of `editor` itself)
the rest of monomorphization_collector_graph_walk dominates it. Most of the time is spent in collect_items_rec.

This PR aims to reduce the number of locks taking place; instead of locking output MonoItems once per children of current node, we do so once per *parent*. We also get to reuse locks for mentioned and used items.
While this commit does not reduce Wall time of Zed's build, it does shave off `cargo build -j1` from 43s to 41.5s.
@rustbot
Copy link
Collaborator

rustbot commented Apr 29, 2025

r? @wesleywiser

rustbot has assigned @wesleywiser.
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

@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 Apr 29, 2025
@lqd
Copy link
Member

lqd commented Apr 29, 2025

@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Apr 29, 2025
bors added a commit to rust-lang-ci/rust that referenced this pull request Apr 29, 2025
…ained-locking, r=<try>

mono collector: Reduce # of locking while walking the graph

While profiling Zed's dev build I've noticed that while most of the time `upstream_monomorphizations` takes a lot of time in monomorpization_collector, in some cases (e.g. build of `editor` itself) the rest of monomorphization_collector_graph_walk dominates it. Most of the time is spent in collect_items_rec.

This PR aims to reduce the number of locks taking place; instead of locking output MonoItems once per children of current node, we do so once per *parent*. We also get to reuse locks for mentioned and used items. While this commit does not reduce Wall time of Zed's build, it does shave off CPU time (measured with `cargo build -j1`) from 48s to 47s. I've also tested it with parallel frontend against Zed and ripgrep and found no regressions.
@bors
Copy link
Collaborator

bors commented Apr 29, 2025

⌛ Trying commit 578ea26 with merge f3a2b0e...

@bors
Copy link
Collaborator

bors commented Apr 29, 2025

☀️ Try build successful - checks-actions
Build commit: f3a2b0e (f3a2b0ecccb63c2cbcca1daf45b27db4bd25cbba)

@rust-timer

This comment has been minimized.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (f3a2b0e): comparison URL.

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

Benchmarking this pull request likely means that it is perf-sensitive, so we're automatically marking it as not fit for rolling up. While you can manually mark this PR as fit for rollup, we strongly recommend not doing so since this PR may lead to changes in compiler perf.

Next Steps: If you can justify the regressions found in this try perf run, please indicate this with @rustbot label: +perf-regression-triaged along with sufficient written justification. If you cannot justify the regressions please fix the regressions and do another perf run. If the next run shows neutral or positive results, the label will be automatically removed.

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

Instruction count

This is the most reliable metric that we have; it was used to determine the overall result at the top of this comment. However, even this metric can sometimes exhibit noise.

mean range count
Regressions ❌
(primary)
0.2% [0.2%, 0.2%] 1
Regressions ❌
(secondary)
0.7% [0.7%, 0.7%] 1
Improvements ✅
(primary)
-2.9% [-2.9%, -2.9%] 1
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) -1.3% [-2.9%, 0.2%] 2

Max RSS (memory usage)

Results (primary -0.5%)

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
0.7% [0.7%, 0.7%] 1
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-0.6% [-1.0%, -0.4%] 11
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) -0.5% [-1.0%, 0.7%] 12

Cycles

Results (primary -0.3%, secondary 6.7%)

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
0.5% [0.4%, 0.5%] 4
Regressions ❌
(secondary)
6.7% [6.7%, 6.7%] 1
Improvements ✅
(primary)
-1.2% [-2.7%, -0.4%] 3
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) -0.3% [-2.7%, 0.5%] 7

Binary size

Results (primary -1.1%)

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

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

Bootstrap: 763.233s -> 764.766s (0.20%)
Artifact size: 365.38 MiB -> 365.24 MiB (-0.04%)

@rustbot rustbot added perf-regression Performance regression. and removed S-waiting-on-perf Status: Waiting on a perf run to be completed. labels Apr 29, 2025
@wesleywiser
Copy link
Member

Thanks @osiewicz! Just out of curiosity, do you see this as unblocking subsequent wins in collect_items_rec?

@bors r+

@bors
Copy link
Collaborator

bors commented May 1, 2025

📌 Commit 578ea26 has been approved by wesleywiser

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels May 1, 2025
VlaDexa added a commit to VlaDexa/rust that referenced this pull request May 2, 2025
…grained-locking, r=wesleywiser

mono collector: Reduce # of locking while walking the graph

While profiling Zed's dev build I've noticed that while most of the time `upstream_monomorphizations` takes a lot of time in monomorpization_collector, in some cases (e.g. build of `editor` itself) the rest of monomorphization_collector_graph_walk dominates it. Most of the time is spent in collect_items_rec.

This PR aims to reduce the number of locks taking place; instead of locking output MonoItems once per children of current node, we do so once per *current node*. We also get to reuse locks for mentioned and used items. While this commit does not reduce Wall time of Zed's build, it does shave off CPU time (measured with `cargo build -j1`) from 48s to 47s. I've also tested it with parallel frontend against Zed and ripgrep and found no regressions.
@bors
Copy link
Collaborator

bors commented May 3, 2025

⌛ Testing commit 578ea26 with merge 2ad5f86...

@bors
Copy link
Collaborator

bors commented May 3, 2025

☀️ Test successful - checks-actions
Approved by: wesleywiser
Pushing 2ad5f86 to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label May 3, 2025
@bors bors merged commit 2ad5f86 into rust-lang:master May 3, 2025
7 checks passed
@rustbot rustbot added this to the 1.88.0 milestone May 3, 2025
Copy link

github-actions bot commented May 3, 2025

What is this? This is an experimental post-merge analysis report that shows differences in test outcomes between the merged PR and its parent PR.

Comparing 2d5ffc5 (parent) -> 2ad5f86 (this PR)

Test differences

Show 6 test diffs

6 doctest diffs were found. These are ignored, as they are noisy.

Test dashboard

Run

cargo run --manifest-path src/ci/citool/Cargo.toml -- \
    test-dashboard 2ad5f8607d0e192b60b130e5cc416b477b351c18 --output-dir test-dashboard

And then open test-dashboard/index.html in your browser to see an overview of all executed tests.

Job duration changes

  1. dist-aarch64-linux: 5573.8s -> 7611.6s (36.6%)
  2. aarch64-apple: 4360.9s -> 3648.7s (-16.3%)
  3. x86_64-apple-2: 5816.6s -> 4901.1s (-15.7%)
  4. dist-apple-various: 6080.3s -> 6561.6s (7.9%)
  5. x86_64-apple-1: 6823.9s -> 7309.2s (7.1%)
  6. x86_64-msvc-2: 6911.5s -> 6618.1s (-4.2%)
  7. x86_64-gnu-llvm-20-2: 5962.7s -> 5716.0s (-4.1%)
  8. dist-armv7-linux: 5528.0s -> 5731.3s (3.7%)
  9. i686-msvc-2: 6985.9s -> 7236.1s (3.6%)
  10. x86_64-msvc-ext2: 5819.7s -> 5633.0s (-3.2%)
How to interpret the job duration changes?

Job durations can vary a lot, based on the actual runner instance
that executed the job, system noise, invalidated caches, etc. The table above is provided
mostly for t-infra members, for simpler debugging of potential CI slow-downs.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (2ad5f86): comparison URL.

Overall result: ❌ regressions - please read the text below

Our benchmarks found a performance regression caused by this PR.
This might be an actual regression, but it can also be just noise.

Next Steps:

  • If the regression was expected or you think it can be justified,
    please write a comment with sufficient written justification, and add
    @rustbot label: +perf-regression-triaged to it, to mark the regression as triaged.
  • If you think that you know of a way to resolve the regression, try to create
    a new PR with a fix for the regression.
  • If you do not understand the regression or you think that it is just noise,
    you can ask the @rust-lang/wg-compiler-performance working group for help (members of this group
    were already notified of this PR).

@rustbot label: +perf-regression
cc @rust-lang/wg-compiler-performance

Instruction count

This is the most reliable metric that we have; it was used to determine the overall result at the top of this comment. However, even this metric can sometimes exhibit noise.

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

Max RSS (memory usage)

Results (primary 0.2%, secondary 2.0%)

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
0.9% [0.4%, 2.4%] 18
Regressions ❌
(secondary)
2.0% [2.0%, 2.0%] 1
Improvements ✅
(primary)
-1.0% [-2.0%, -0.4%] 10
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 0.2% [-2.0%, 2.4%] 28

Cycles

Results (primary 0.2%)

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
0.7% [0.4%, 1.3%] 29
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-1.8% [-3.7%, -0.5%] 6
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 0.2% [-3.7%, 1.3%] 35

Binary size

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

Bootstrap: 769.328s -> 769.364s (0.00%)
Artifact size: 365.53 MiB -> 365.54 MiB (0.00%)

@Mark-Simulacrum Mark-Simulacrum removed the perf-regression Performance regression. label May 5, 2025
@Mark-Simulacrum
Copy link
Member

Both changes look within noise thresholds (bimodality). Dropping regression.

@osiewicz
Copy link
Contributor Author

osiewicz commented May 8, 2025

@wesleywiser I am not quite sure to be fair; in Zed's case we are mostly bottlenecked on incremental query result deserialization, so (imho) it makes sense to make the overhead of these functions as low as possible. I did not dig beyond this PR yet though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
merged-by-bors This PR was explicitly merged by bors. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. 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.

7 participants