Skip to content

Conversation

@dianne
Copy link
Contributor

@dianne dianne commented Nov 7, 2025

This gets rid of RvalueCandidate, inlines the definition of RvalueScopes into ScopeTree, and removes two RvalueScopes-specific modules, consolidating the scoping logic a bit. Removing the extra step of going from RvalueCandidates to RvalueScopes and removing the duplication between them should also hopefully improve perf.

I've also taken the liberty of doing a bit of renaming and comment updates, changing some "rvalue scope"s to "extended temporary scope"s. This is a bit closer to the Reference's terminology and makes it clearer that it's specific to temporary lifetime extension. This isn't comprehensive. In particular, I've left record_rvalue_scope_if_borrow_expr untouched since #146098 gets rid of it.

Pulled out from #146098.

r? BoxyUwU as the reviewer of #146098 (though feel free to reassign/claim! this is just cleanup)

cc @dingxiangfei2009

@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 Nov 7, 2025
@dianne
Copy link
Contributor Author

dianne commented Nov 7, 2025

@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

@rust-bors

This comment has been minimized.

rust-bors bot added a commit that referenced this pull request Nov 7, 2025
cleanup: merge `RvalueScopes` into `ScopeTree`
@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Nov 7, 2025
@rust-log-analyzer

This comment has been minimized.

@rust-bors
Copy link

rust-bors bot commented Nov 7, 2025

💥 Test timed out after 21600s

This removes some unneeded indirection and consolidates the logic for
scope resolution.
@dianne dianne force-pushed the cleanup-rvalue-scopes branch from dc5405c to 0e50d5a Compare November 8, 2025 02:44
@dianne
Copy link
Contributor Author

dianne commented Nov 8, 2025

Retrying CI since #t-infra > CI keeps failing because of npm error seems to be fixed. Not sure what's up with the bors2 timeout but let's try that again too I guess?

@bors try

@rust-bors

This comment has been minimized.

rust-bors bot added a commit that referenced this pull request Nov 8, 2025
cleanup: merge `RvalueScopes` into `ScopeTree`
@rust-bors
Copy link

rust-bors bot commented Nov 8, 2025

☀️ Try build successful (CI)
Build commit: 03a6515 (03a6515b1ba7e8b2d5a4165ce136197e7d22feb3, parent: ceb7df7e6f17c92c7d49f7e4f02df0e68bc9b38b)

@rust-timer

This comment has been minimized.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (03a6515): 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
Regressions ❌
(secondary)
0.4% [0.0%, 0.6%] 4
Improvements ✅
(primary)
-0.1% [-0.2%, -0.1%] 4
Improvements ✅
(secondary)
-0.0% [-0.1%, -0.0%] 5
All ❌✅ (primary) -0.1% [-0.2%, -0.1%] 4

Max RSS (memory usage)

Results (primary 4.3%, secondary -1.5%)

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

mean range count
Regressions ❌
(primary)
4.3% [1.3%, 7.2%] 2
Regressions ❌
(secondary)
2.2% [2.2%, 2.2%] 1
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-2.4% [-3.5%, -2.0%] 4
All ❌✅ (primary) 4.3% [1.3%, 7.2%] 2

Cycles

Results (primary 2.6%, secondary 6.0%)

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

mean range count
Regressions ❌
(primary)
2.6% [2.1%, 3.0%] 4
Regressions ❌
(secondary)
6.0% [5.6%, 6.3%] 2
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 2.6% [2.1%, 3.0%] 4

Binary size

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

Bootstrap: 475.105s -> 475.535s (0.09%)
Artifact size: 390.80 MiB -> 390.76 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 Nov 8, 2025
@dianne
Copy link
Contributor Author

dianne commented Nov 8, 2025

So that's where the deep-vector regression is from! To be honest, I'm not confident I know what's going on. I tried profiling locally to get a better idea of where the instruction count difference is, and it looks like the biggest diff by far is malloc_default with 11,972,450 more instructions(?), and the second biggest is emap_try_acquire_edata_neighbor with 2,270,678 more instructions(?). So apparently it's spending more instructions in the allocator? Maybe changing when the ScopeTree is built happens to be worse for deep-vector, somehow? For pathological cases like deep-vector, the scope tree's parent scope map is very large and it's not initialized with a capacity or anything so it probably needs a lot of reallocations; being later in compilation is probably bad for that? This also slightly changed the sizes of ScopeTree and TypeckResults, but that seems less likely to be it.

I do have an couple ideas that could improve perf both here and for realistic code, but they're not pretty. Reifying the scope hierarchy isn't really necessary, and there's a few ways to avoid it; e.g.:

  • Currently, the ScopeTree's parent scope map is effectively a copy of the HIR for a typeck root but allocated in a hash map rather than an arena and more convenient for doing upwards traversals on. But we could do upwards traversals directly on the HIR. Upwards traversals on the HIR are unpleasant though, so I don't know whether it would be worth it.
  • We could avoid both the upwards traversals and building a parent map if we do scope resolution in the same pass as building the THIR. We'd just be doing more bookkeeping on the way down. This would probably have the best perf, but it's not good for separating concerns. It's also a bit inconvenient for a few shadowing-related diagnostics/lints, which simpler to write when there's an easy way to inspect the scope hierarchy; getting rid of upwards traversals means they'd have to implement their own most likely.

Another option: we remove the TempLifetime from thir::Expr. The MIR Builder has access to the ScopeTree for getting variables' scopes, so why not get temporary scopes that way too? There may be a reason for this, but I'm not aware of it if so. Making thir::Expr smaller and doing less work per expression node would probably help. I may try this.

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.

5 participants