Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

missed optimization: fat pointers in two-variant enums with small second variants #48654

Open
alercah opened this issue Mar 2, 2018 · 4 comments
Labels
C-enhancement Category: An issue proposing an enhancement or a PR with one. C-optimization Category: An issue highlighting optimization opportunities or PRs implementing such T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@alercah
Copy link
Contributor

alercah commented Mar 2, 2018

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 0x0 is never a valid pointer value to optimize the discriminant into the address: 0x0 is 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

@matthiaskrgr
Copy link
Member

@alercah hi, is the playground link correct (seems empty)?

@kennytm kennytm added C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Mar 2, 2018
@alercah
Copy link
Contributor Author

alercah commented Mar 2, 2018

My bad, the correct link is https://play.rust-lang.org/?gist=7398c28c7f05bc76a06a7fb6d4af40fa&version=nightly (updated the original comment as well).

@steveklabnik
Copy link
Member

Triage: no change

@workingjubilee workingjubilee added the C-optimization Category: An issue highlighting optimization opportunities or PRs implementing such label Oct 8, 2023
@adwinwhite
Copy link
Contributor

This case is optimized now, probably by #94075.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-enhancement Category: An issue proposing an enhancement or a PR with one. C-optimization Category: An issue highlighting optimization opportunities or PRs implementing such T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

6 participants