-
-
Notifications
You must be signed in to change notification settings - Fork 14.8k
missed optimization: fat pointers in two-variant enums with small second variants #48654
Copy link
Copy link
Open
Labels
C-enhancementCategory: An issue proposing an enhancement or a PR with one.Category: An issue proposing an enhancement or a PR with one.C-optimizationCategory: An issue highlighting optimization opportunities or PRs implementing suchCategory: An issue highlighting optimization opportunities or PRs implementing suchT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.
Metadata
Metadata
Assignees
Labels
C-enhancementCategory: An issue proposing an enhancement or a PR with one.Category: An issue proposing an enhancement or a PR with one.C-optimizationCategory: An issue highlighting optimization opportunities or PRs implementing suchCategory: An issue highlighting optimization opportunities or PRs implementing suchT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.
Type
Fields
Give feedbackNo fields configured for issues without a type.
Currently, there's a reasonably well-known optimization for two-variant enums, where one variant is a single pointer and the other has no fields. In this case, Rust uses its knowledge that
0x0is never a valid pointer value to optimize the discriminant into the address:0x0is taken to mean that the non-pointer variant is the correct one.There's a missed optimization, however, when the pointer is a fat one (e.g. a slice or a trait object) and the second variant is small (specifically,
<= usize). In this case, the enum could be optimized by having the address field zero still indicate the second variant, with the fields being stored where the size or vtable pointer would be stored on a trait object.https://play.rust-lang.org/?gist=7398c28c7f05bc76a06a7fb6d4af40fa&version=nightly