Skip to content

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

Open
@alercah

Description

@alercah
Contributor

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

Activity

matthiaskrgr

matthiaskrgr commented on Mar 2, 2018

@matthiaskrgr
Member

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

added
C-enhancementCategory: An issue proposing an enhancement or a PR with one.
T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
on Mar 2, 2018
alercah

alercah commented on Mar 2, 2018

@alercah
ContributorAuthor

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

steveklabnik

steveklabnik commented on May 14, 2020

@steveklabnik
Member

Triage: no change

added
C-optimizationCategory: An issue highlighting optimization opportunities or PRs implementing such
on Oct 8, 2023
adwinwhite

adwinwhite commented on Sep 19, 2024

@adwinwhite
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

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-enhancementCategory: An issue proposing an enhancement or a PR with one.C-optimizationCategory: An issue highlighting optimization opportunities or PRs implementing suchT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @steveklabnik@kennytm@matthiaskrgr@alercah@workingjubilee

        Issue actions

          missed optimization: fat pointers in two-variant enums with small second variants · Issue #48654 · rust-lang/rust