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.

@RalfJung
Copy link
Member Author

@rustbot reroll

@rustbot rustbot assigned nnethercote and unassigned spastorino Nov 16, 2025
}

FieldInfo { span, trivial, unsuited: check_unsuited(tcx, typing_env, ty).break_value() }
return FieldInfo { span, trivial, ty };
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
return FieldInfo { span, trivial, ty };
FieldInfo { span, trivial, ty }

@nnethercote
Copy link
Contributor

r=me with the nit fixed.

@nnethercote nnethercote added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Nov 18, 2025
@RalfJung RalfJung force-pushed the repr-transparent-check branch from 928cde5 to c23182e Compare November 18, 2025 12:53
@rustbot
Copy link
Collaborator

rustbot commented Nov 18, 2025

This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@RalfJung
Copy link
Member Author

@bors r=nnethercote rollup

@bors
Copy link
Collaborator

bors commented Nov 18, 2025

📌 Commit c23182e has been approved by nnethercote

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Nov 18, 2025
bors added a commit that referenced this pull request Nov 18, 2025
Rollup of 5 pull requests

Successful merges:

 - #147887 (Improve the documentation of atomic::fence)
 - #148281 (repr(transparent) check: do not compute check_unsuited more than once)
 - #148484 (Fix suggestion for the `cfg!` macro)
 - #149057 (`rust-analyzer` subtree update)
 - #149061 (debug-assert FixedSizeEncoding invariant)

r? `@ghost`
`@rustbot` modify labels: rollup
bors added a commit that referenced this pull request Nov 18, 2025
Rollup of 5 pull requests

Successful merges:

 - #147887 (Improve the documentation of atomic::fence)
 - #148281 (repr(transparent) check: do not compute check_unsuited more than once)
 - #148484 (Fix suggestion for the `cfg!` macro)
 - #149057 (`rust-analyzer` subtree update)
 - #149061 (debug-assert FixedSizeEncoding invariant)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 280a6c5 into rust-lang:main Nov 19, 2025
11 checks passed
@rustbot rustbot added this to the 1.93.0 milestone Nov 19, 2025
rust-timer added a commit that referenced this pull request Nov 19, 2025
Rollup merge of #148281 - RalfJung:repr-transparent-check, r=nnethercote

repr(transparent) check: do not compute check_unsuited more than once

`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.)
lnicola pushed a commit to lnicola/rust-analyzer that referenced this pull request Nov 19, 2025
Rollup of 5 pull requests

Successful merges:

 - rust-lang/rust#147887 (Improve the documentation of atomic::fence)
 - rust-lang/rust#148281 (repr(transparent) check: do not compute check_unsuited more than once)
 - rust-lang/rust#148484 (Fix suggestion for the `cfg!` macro)
 - rust-lang/rust#149057 (`rust-analyzer` subtree update)
 - rust-lang/rust#149061 (debug-assert FixedSizeEncoding invariant)

r? `@ghost`
`@rustbot` modify labels: rollup
@RalfJung RalfJung deleted the repr-transparent-check branch November 21, 2025 14:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. 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.

6 participants