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

Use a more lightweight cache for erase_regions #139240

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

Zoxc
Copy link
Contributor

@Zoxc Zoxc commented Apr 2, 2025

This changes erase_regions to use a global hashmap instead of a query to cache types with erased lifetimes. erase_regions_ty only depends on the input parameter so it doesn't need to be a query.

This is a rebase of #59505.

BenchmarkBeforeAfterBeforeAfterBeforeAfter
TimeTime%Physical MemoryPhysical Memory%Committed MemoryCommitted Memory%
🟣 clap:check1.3960s1.3883s -0.55%147.66 MiB147.76 MiB 0.07%199.83 MiB199.86 MiB 0.01%
🟣 hyper:check0.2346s0.2337s -0.38%80.35 MiB80.54 MiB 0.24%129.99 MiB130.12 MiB 0.10%
🟣 regex:check0.7811s0.7788s -0.30%107.75 MiB108.07 MiB 0.29%153.67 MiB153.99 MiB 0.21%
🟣 syn:check1.2999s1.2978s -0.17%140.24 MiB140.35 MiB 0.08%188.59 MiB188.58 MiB -0.01%
Total3.7117s3.6986s -0.35%476.00 MiB476.72 MiB 0.15%672.08 MiB672.55 MiB 0.07%
Summary1.0000s0.9965s -0.35%1 byte1.00 bytes 0.17%1 byte1.00 bytes 0.08%

@rustbot
Copy link
Collaborator

rustbot commented Apr 2, 2025

r? @petrochenkov

rustbot has assigned @petrochenkov.
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 A-query-system Area: The rustc query system (https://rustc-dev-guide.rust-lang.org/query.html) 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 2, 2025
@lqd
Copy link
Member

lqd commented Apr 2, 2025

Remember that you now have credentials for perf runs, which should be more accurate, and also more representative, with a LTOed/PGOed toolchain.

@@ -268,6 +268,14 @@ impl<D: Deps> DepGraph<D> {
D::with_deps(TaskDepsRef::Forbid, op)
}

/// This checks that no dependencies are registered in `op` if debug assertions are enabled.
pub fn debug_assert_no_deps<OP, R>(op: OP) -> R
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
pub fn debug_assert_no_deps<OP, R>(op: OP) -> R
pub fn debug_assert_no_deps<R>(op: impl FnOnce() -> R) -> R

@@ -1376,6 +1376,9 @@ pub struct GlobalCtxt<'tcx> {
/// Common consts, pre-interned for your convenience.
pub consts: CommonConsts<'tcx>,

/// A cache for the `erase_regions` function.
pub(in crate::ty) erased_region_cache: ShardedHashMap<Ty<'tcx>, Ty<'tcx>>,
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you add comments explaining why 1) the query cache is not used here and 2) a sharded hash map is used instead of a simple one.

}
impl<'tcx> TyCtxt<'tcx> {
/// Erases regions from `ty` to yield a new type.
pub fn erase_regions_ty(self, ty: Ty<'tcx>) -> Ty<'tcx> {
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you add a comment telling that this function is expected to be a pure function of its input, and that's why it cannot call any other queries.

@petrochenkov
Copy link
Contributor

@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 2, 2025
@petrochenkov petrochenkov removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Apr 2, 2025
bors added a commit to rust-lang-ci/rust that referenced this pull request Apr 2, 2025
Use a more lightweight cache for `erase_regions`

This changes `erase_regions` to use a global hashmap instead of a query to cache types with erased lifetimes. `erase_regions_ty` only depends on the input parameter so it doesn't need to be a query.

This is a rebase of rust-lang#59505.

<table><tr><td rowspan="2">Benchmark</td><td colspan="1"><b>Before</b></th><td colspan="2"><b>After</b></th><td colspan="1"><b>Before</b></th><td colspan="2"><b>After</b></th><td colspan="1"><b>Before</b></th><td colspan="2"><b>After</b></th></tr><tr><td align="right">Time</td><td align="right">Time</td><td align="right">%</th><td align="right">Physical Memory</td><td align="right">Physical Memory</td><td align="right">%</th><td align="right">Committed Memory</td><td align="right">Committed Memory</td><td align="right">%</th></tr><tr><td>🟣 <b>clap</b>:check</td><td align="right">1.3960s</td><td align="right">1.3883s</td><td align="right"> -0.55%</td><td align="right">147.66 MiB</td><td align="right">147.76 MiB</td><td align="right"> 0.07%</td><td align="right">199.83 MiB</td><td align="right">199.86 MiB</td><td align="right"> 0.01%</td></tr><tr><td>🟣 <b>hyper</b>:check</td><td align="right">0.2346s</td><td align="right">0.2337s</td><td align="right"> -0.38%</td><td align="right">80.35 MiB</td><td align="right">80.54 MiB</td><td align="right"> 0.24%</td><td align="right">129.99 MiB</td><td align="right">130.12 MiB</td><td align="right"> 0.10%</td></tr><tr><td>🟣 <b>regex</b>:check</td><td align="right">0.7811s</td><td align="right">0.7788s</td><td align="right"> -0.30%</td><td align="right">107.75 MiB</td><td align="right">108.07 MiB</td><td align="right"> 0.29%</td><td align="right">153.67 MiB</td><td align="right">153.99 MiB</td><td align="right"> 0.21%</td></tr><tr><td>🟣 <b>syn</b>:check</td><td align="right">1.2999s</td><td align="right">1.2978s</td><td align="right"> -0.17%</td><td align="right">140.24 MiB</td><td align="right">140.35 MiB</td><td align="right"> 0.08%</td><td align="right">188.59 MiB</td><td align="right">188.58 MiB</td><td align="right"> -0.01%</td></tr><tr><td>Total</td><td align="right">3.7117s</td><td align="right">3.6986s</td><td align="right"> -0.35%</td><td align="right">476.00 MiB</td><td align="right">476.72 MiB</td><td align="right"> 0.15%</td><td align="right">672.08 MiB</td><td align="right">672.55 MiB</td><td align="right"> 0.07%</td></tr><tr><td>Summary</td><td align="right">1.0000s</td><td align="right">0.9965s</td><td align="right"> -0.35%</td><td align="right">1 byte</td><td align="right">1.00 bytes</td><td align="right"> 0.17%</td><td align="right">1 byte</td><td align="right">1.00 bytes</td><td align="right"> 0.08%</td></tr></table>
@bors
Copy link
Collaborator

bors commented Apr 2, 2025

⌛ Trying commit bb40db8 with merge 35d98da...

@bors
Copy link
Collaborator

bors commented Apr 2, 2025

☀️ Try build successful - checks-actions
Build commit: 35d98da (35d98da4f0342063e91963b94fb43fa99e375349)

@rust-timer

This comment has been minimized.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (35d98da): 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.7% [0.2%, 1.1%] 4
Regressions ❌
(secondary)
0.5% [0.3%, 0.9%] 7
Improvements ✅
(primary)
-0.9% [-3.1%, -0.3%] 5
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) -0.2% [-3.1%, 1.1%] 9

Max RSS (memory usage)

Results (primary -0.7%, secondary -1.4%)

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)
1.4% [1.4%, 1.4%] 1
Improvements ✅
(primary)
-0.7% [-0.7%, -0.7%] 1
Improvements ✅
(secondary)
-2.3% [-3.1%, -1.4%] 3
All ❌✅ (primary) -0.7% [-0.7%, -0.7%] 1

Cycles

Results (primary -1.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)
- - 0
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-1.8% [-1.8%, -1.8%] 1
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) -1.8% [-1.8%, -1.8%] 1

Binary size

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

Bootstrap: 776.227s -> 774.214s (-0.26%)
Artifact size: 365.99 MiB -> 365.96 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 Apr 2, 2025
@petrochenkov
Copy link
Contributor

Not sure if it's worth it, the change is doing something unusual and non-obvious, but the results are mixed.
@rustbot author

@rustbot rustbot added the S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. label Apr 2, 2025
@rustbot
Copy link
Collaborator

rustbot commented Apr 2, 2025

Reminder, once the PR becomes ready for a review, use @rustbot ready.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-query-system Area: The rustc query system (https://rustc-dev-guide.rust-lang.org/query.html) perf-regression Performance regression. S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. 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.

None yet

6 participants