Skip to content

Is it possible to enable more niche optimizations for reference types by utilizing unaligned values? #134675

Closed
@EFanZh

Description

@EFanZh

To my understanding, currently, a reference type has only one niche that is the null value, but a reference type must be properly aligned, which means in theory we can also utilize those unaligned values as niches. The total possible niches of a type can be calculated as:

fn reference_niches<T>() -> usize {
    ((MEMORY_SPACE_SIZE - size_of::<T>()) / align_of::<T>()).wrapping_neg()
}

where MEMORY_SPACE_SIZE is the theoretical maximum size of the memory space in bytes.

Is it possible for Rust to enable this optimization?

Additionally, is it possible to add a new NonNullAligned<T> type that has the same niches as &T? We can use it to implement Box, Rc, Arc and Vec types, so they can also have the same niches.

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-discussionCategory: Discussion or questions that doesn't represent real issues.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions