rust-lang / rust-lang/rust

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

Open
#138,332 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-layout C-discussion C-optimization T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

Looking at some examples in https://github.com/rust-lang/rust/pull/137278#issue-2863728153 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.)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reviewing the examples in the linked pull request and the Rust compiler's type-layout handling for Option. Determine whether discriminant sizing can be bounded without losing niche optimizations; done means reaching a clear implementation or design conclusion, backed by relevant compiler tests if the change is feasible.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.