Skip to content
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

Ergonomic ref counting: optimize away clones when possible #139088

Open
wants to merge 7 commits into
base: master
Choose a base branch
from

Conversation

spastorino
Copy link
Member

@spastorino spastorino commented Mar 28, 2025

This PR build on top of #134797. It optimizes codegen of ergonomic ref-counting when the type being used is only known to be copy after monomorphization. We avoid codening a clone and generate bitwise copy instead.

RFC: rust-lang/rfcs#3680
Tracking issue: #132290
Project goal: rust-lang/rust-project-goals#107

r? @nikomatsakis

This PR could better sit on top of #131650 but as it did not land yet I've decided to just do minimal changes. It may be the case that doing what I'm doing regress the performance and we may need to go the full route of #131650.
cc @saethlin in this regard.

@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 Mar 28, 2025
let mir = instance.instantiate_mir_and_normalize_erasing_regions(
tcx,
ty::TypingEnv::fully_monomorphized(),
ty::EarlyBinder::bind(mir.clone()),
Copy link
Member Author

Choose a reason for hiding this comment

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

We need to clone the mir body here, unsure how bad this could be.

@rust-log-analyzer

This comment has been minimized.

@spastorino spastorino changed the title Ergonomic ref counting: Optimize away clones when we know after monomorphization that the type can be copied Ergonomic ref counting: optimize away clones when possible Mar 29, 2025
@spastorino spastorino force-pushed the ergonomic-ref-counting-2 branch from 5592732 to 961717a Compare March 29, 2025 03:20
@rust-log-analyzer

This comment has been minimized.

@spastorino spastorino force-pushed the ergonomic-ref-counting-2 branch from 961717a to 07cc495 Compare March 29, 2025 13:39
@rust-log-analyzer

This comment has been minimized.

@spastorino spastorino force-pushed the ergonomic-ref-counting-2 branch from 07cc495 to 28551ae Compare March 31, 2025 13:01
@rustbot rustbot added the A-meta Area: Issues & PRs about the rust-lang/rust repository itself label Mar 31, 2025
@rust-log-analyzer

This comment has been minimized.

@spastorino spastorino force-pushed the ergonomic-ref-counting-2 branch from 28551ae to 792d2ce Compare March 31, 2025 13:35
@rust-log-analyzer

This comment has been minimized.

@@ -1,52 +0,0 @@
//@ known-bug: #129372
Copy link
Member Author

Choose a reason for hiding this comment

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

Would need to properly decide exactly what to do with this test. If just removing it is fine or if there's some variation that needs to be placed in tests/ui. Didn't bother to check this for now.

@rust-log-analyzer

This comment has been minimized.

@spastorino spastorino force-pushed the ergonomic-ref-counting-2 branch from 0a7a642 to f6c3c4b Compare March 31, 2025 15:39
@spastorino spastorino marked this pull request as ready for review March 31, 2025 15:41
@rustbot
Copy link
Collaborator

rustbot commented Mar 31, 2025

This PR changes a file inside tests/crashes. If a crash was fixed, please move into the corresponding ui subdir and add 'Fixes #' to the PR description to autoclose the issue upon merge.

This PR changes MIR

cc @oli-obk, @RalfJung, @JakobDegen, @davidtwco, @vakaras

Some changes occurred in compiler/rustc_codegen_ssa

cc @WaffleLapkin

Some changes occurred in src/tools/clippy

cc @rust-lang/clippy

@rust-log-analyzer

This comment has been minimized.

@RalfJung
Copy link
Member

RalfJung commented Mar 31, 2025

(Procedurally, it'd be nice to reference the tracking issue in every PR related to that feature. Makes it much easier to navigate around for someone who's not following every single PR in this project.)

@rust-log-analyzer

This comment has been minimized.

@spastorino spastorino force-pushed the ergonomic-ref-counting-2 branch from 082b0fa to 3640f45 Compare March 31, 2025 18:27
@rust-log-analyzer

This comment has been minimized.

@spastorino spastorino force-pushed the ergonomic-ref-counting-2 branch from 3640f45 to 3959c94 Compare April 1, 2025 14:34
@spastorino spastorino force-pushed the ergonomic-ref-counting-2 branch from 3959c94 to da1df90 Compare April 1, 2025 17:17
@spastorino
Copy link
Member Author

@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 1, 2025
bors added a commit to rust-lang-ci/rust that referenced this pull request Apr 1, 2025
… r=<try>

Ergonomic ref counting: optimize away clones when possible

This PR build on top of rust-lang#134797. It optimizes codegen of ergonomic ref-counting when the type being `use`d is only known to be copy after monomorphization. We avoid codening a clone and generate bitwise copy instead.

RFC: rust-lang/rfcs#3680
Tracking issue: rust-lang#132290
Project goal: rust-lang/rust-project-goals#107

r? `@nikomatsakis`

This PR could better sit on top of rust-lang#131650 but as it did not land yet I've decided to just do minimal changes. It may be the case that doing what I'm doing regress the performance and we may need to go the full route of rust-lang#131650.
cc `@saethlin` in this regard.
@bors
Copy link
Collaborator

bors commented Apr 1, 2025

⌛ Trying commit da1df90 with merge c5e50d7...

@bors
Copy link
Collaborator

bors commented Apr 1, 2025

☀️ Try build successful - checks-actions
Build commit: c5e50d7 (c5e50d7174504206bc4cc4c002f3a3cde8b5cbb3)

@rust-timer

This comment has been minimized.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (c5e50d7): 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.6% [0.2%, 1.3%] 36
Regressions ❌
(secondary)
3.1% [0.2%, 18.5%] 7
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-0.1% [-0.2%, -0.1%] 4
All ❌✅ (primary) 0.6% [0.2%, 1.3%] 36

Max RSS (memory usage)

Results (primary 8.1%, secondary 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)
8.1% [2.2%, 29.5%] 42
Regressions ❌
(secondary)
3.1% [2.9%, 3.4%] 3
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-2.0% [-2.4%, -1.5%] 2
All ❌✅ (primary) 8.1% [2.2%, 29.5%] 42

Cycles

Results (primary 1.5%, secondary 3.8%)

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)
1.5% [0.9%, 2.0%] 11
Regressions ❌
(secondary)
5.7% [1.4%, 18.3%] 4
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-3.7% [-3.7%, -3.7%] 1
All ❌✅ (primary) 1.5% [0.9%, 2.0%] 11

Binary size

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

Bootstrap: 774.818s -> 781.377s (0.85%)
Artifact size: 365.93 MiB -> 366.03 MiB (0.03%)

@rustbot rustbot added perf-regression Performance regression. and removed S-waiting-on-perf Status: Waiting on a perf run to be completed. labels Apr 1, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-meta Area: Issues & PRs about the rust-lang/rust repository itself 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.

9 participants