Skip to content

Should Option<u128> use a smaller (maybe usize) tag? #138332

Open
@scottmcm

Description

@scottmcm

Looking at some examples in #137278 (comment) shows that Option<u128> is using a full u128 for its stored tag. That tag, of course, can only be 0 or 1.

As a result things like returning Some(…) need to write two qwords on x64 into the return value, even though of course one of them is only ever all-zeroes.

Could we maybe give it a layout equivalent to (usize, u128) instead?

Or, in general, can we cap the size of tags to a native machine word, given a small discriminant that could fit in just about any type?

(Aside: It's good for Option<u32> to still be (u32, MaybeUninit<u32>), since that has more niches than (u8, MaybeUninit<u32>). But going so big that it takes multiple instructions to write the tag seems like overkill, and for unconstrained repr(Rust) we should probably stop at something reasonable for the target.)

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-layoutArea: Memory layout of typesC-discussionCategory: Discussion or questions that doesn't represent real issues.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

    Issue actions