Skip to content

Conversation

@RalfJung
Copy link
Member

field_infos is an iterator that we execute multiple times. However, we usually ignore the unsuited field -- we only need it in the last iteration. So move the computation of that field to that iteration to avoid computing it multiple times. Computing unsuited involves a recursive traversal over the types of all non-trivial fields, so there can be non-trivial amounts of work here.

(I benchmarked this in #148243 and saw no changes, probably because we don't have a benchmark with many repr(transparent) types. But still, computing this each time just seemed silly.)

@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 Oct 30, 2025
@rustbot
Copy link
Collaborator

rustbot commented Oct 30, 2025

r? @spastorino

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

let layout = tcx.layout_of(typing_env.as_query_input(ty));
// We are currently checking the type this field came from, so it must be local
let span = tcx.hir_span_if_local(field.did).unwrap();
let trivial = layout.is_ok_and(|layout| layout.is_1zst());
Copy link
Member Author

Choose a reason for hiding this comment

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

There is still some code here we are running twice, in particular the layout_of query. The alternative would be to simply collect the results into a Vec, which will cost us an allocation but then avoids running the iterator twice.

if let Some(unsuited) = field.unsuited {
assert!(field.trivial);
if field.trivial
&& let Some(unsuited) = check_unsuited(tcx, typing_env, field.ty).break_value()
Copy link
Member

Choose a reason for hiding this comment

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

Pre-existing, but why does the function return a ControlFlow if its only call immediately calls break_value, and answering my own question: because it is used internally in recursive calls.

Perhaps this implementation detail should still be pushed inside with check_unsuited returning Option, while calling an internal function that returns ControlFlow?

Copy link
Member Author

Choose a reason for hiding this comment

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

This is already a purely internal helper. I don't see the point of having even more nested functions here.

Copy link
Member

Choose a reason for hiding this comment

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

Fair enough.

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