Skip to content

Conversation

@dianne
Copy link
Contributor

@dianne dianne commented Nov 8, 2025

This accomplishes two things:

  • Makes the THIR slightly smaller by not attaching a full TempLifetime to every expression.
  • Reduces the number of traversals of the ScopeTree by only calling ScopeTree::temporary_scope when building MIR for something that needs to be dropped in a temporary scope.

@rustbot rustbot added 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. labels Nov 8, 2025
@dianne
Copy link
Contributor Author

dianne commented Nov 8, 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 8, 2025
…try>

compute temporary scopes when building MIR, not THIR
@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Nov 8, 2025
@rust-bors
Copy link

rust-bors bot commented Nov 8, 2025

☀️ Try build successful (CI)
Build commit: 9de26a5 (9de26a5918b5655a37eefd1b02cdb1ff0cdcdc74, parent: 87f9dcd5e28a301fce81515bd4bea792444dcf29)

@rust-timer

This comment has been minimized.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (9de26a5): comparison URL.

Overall result: ✅ improvements - no action needed

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.

@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)
1.2% [1.2%, 1.2%] 1
Regressions ❌
(secondary)
0.7% [0.2%, 1.2%] 2
Improvements ✅
(primary)
-0.4% [-2.0%, -0.1%] 56
Improvements ✅
(secondary)
-0.9% [-2.5%, -0.0%] 81
All ❌✅ (primary) -0.4% [-2.0%, 1.2%] 57

Max RSS (memory usage)

Results (primary -0.5%, secondary -2.8%)

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

mean range count
Regressions ❌
(primary)
1.4% [1.4%, 1.4%] 1
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-1.5% [-1.9%, -1.2%] 2
Improvements ✅
(secondary)
-2.8% [-4.8%, -1.3%] 28
All ❌✅ (primary) -0.5% [-1.9%, 1.4%] 3

Cycles

Results (primary -2.3%, secondary -4.0%)

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

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
4.9% [4.2%, 5.5%] 2
Improvements ✅
(primary)
-2.3% [-2.4%, -2.2%] 3
Improvements ✅
(secondary)
-4.6% [-8.7%, -2.3%] 29
All ❌✅ (primary) -2.3% [-2.4%, -2.2%] 3

Binary size

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

Bootstrap: 475.809s -> 473.629s (-0.46%)
Artifact size: 390.93 MiB -> 390.83 MiB (-0.03%)

@rustbot rustbot removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Nov 8, 2025
Comment on lines -259 to +262
/// The lifetime of this expression if it should be spilled into a
/// temporary
pub temp_lifetime: TempLifetime,
/// The id of the HIR expression whose [temporary scope] should be used for this expression.
///
/// [temporary scope]: https://doc.rust-lang.org/reference/destructors.html#temporary-scopes
pub temp_scope_id: hir::ItemLocalId,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This could be removed entirely from thir::Expr, and we could use thir::ExprKind::Scopes to set up temporary scopes, but it's a little subtle/inconvenient for certain THIR lowerings (looking at &pin mut here) so for now I've opted to keep it simple.

Copy link
Contributor

Choose a reason for hiding this comment

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

The temp_scope_id annotations here are even more obscure than the previous version. Should we dump the region tree alongside THIR for debugging purposes?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That might help, yeah. I'm not sure a raw Debug dump of the scope tree would be that legible either, but maybe for the -Zunpretty=thir-tree output it'd make sense to inline the relevant bits? I worry a little it could be confusing to have information in there that isn't actually part of the THIR, but it'd also let us print scopes for binding patterns (which aren't straightforward for super let).

I think my ideal though would be to add a new way of pretty-printing the THIR that contains the actual drop scopes used to build the MIR (and maybe also get rid of temp_scope_id entirely). It'd be more work to implement, but it might be more useful? Speaking for myself at least, currently when I want to know drop scopes I read a promoted MIR dump since the THIR doesn't tell me at a glance which expressions correspond to places/operands/temporaries/constants/etc. (and since THIR dumps are verbose enough that they're not particularly more readable than MIR, but I'm not sure how fixable that is).

@dianne dianne force-pushed the slightly-lazier-temporary-scoping branch from d186978 to cb42731 Compare November 13, 2025 09:12
@dianne
Copy link
Contributor Author

dianne commented Nov 13, 2025

Opening for review now that #148658 is merged. r? cjgillot if you don't mind

@dianne dianne marked this pull request as ready for review November 13, 2025 09:14
@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Nov 13, 2025
@rustbot
Copy link
Collaborator

rustbot commented Nov 13, 2025

Some changes occurred in match lowering

cc @Nadrieril

@rustbot rustbot removed the S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. label Nov 13, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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.

4 participants