Skip to content

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

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

Closed
EFanZh opened this issue Dec 23, 2024 · 1 comment
Labels
C-discussion Category: Discussion or questions that doesn't represent real issues.

Comments

@EFanZh
Copy link
Contributor

EFanZh commented Dec 23, 2024

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.

@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Dec 23, 2024
@EFanZh EFanZh changed the title Is it possible to utilize unaligned values for niche optimizations for reference types? Is it possible to utilize unaligned values for niche optimization for reference types? Dec 23, 2024
@EFanZh EFanZh changed the title Is it possible to utilize unaligned values for niche optimization for reference types? Is it possible to enable more optimizations for reference types by utilize unaligned values? Dec 23, 2024
@EFanZh EFanZh changed the title Is it possible to enable more optimizations for reference types by utilize unaligned values? Is it possible to enable more niche optimizations for reference types by utilize unaligned values? Dec 23, 2024
@EFanZh EFanZh changed the title Is it possible to enable more niche optimizations for reference types by utilize unaligned values? Is it possible to enable more niche optimizations for reference types by utilizing unaligned values? Dec 23, 2024
@clubby789 clubby789 added C-discussion Category: Discussion or questions that doesn't represent real issues. and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Dec 23, 2024
@the8472
Copy link
Member

the8472 commented Dec 23, 2024

See rust-lang/rfcs#3204

@EFanZh EFanZh closed this as completed Dec 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-discussion Category: Discussion or questions that doesn't represent real issues.
Projects
None yet
Development

No branches or pull requests

4 participants