Skip to content
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

Extend the "nullable pointer optimization" to two different size variants #43507

Closed
ordian opened this issue Jul 27, 2017 · 3 comments
Closed

Comments

@ordian
Copy link

ordian commented Jul 27, 2017

Play:

#[allow(dead_code)]
enum StrOrChar<'a> {
    Str(&'a str),
    Char(char),
}

fn main() {
    println!("{}", std::mem::size_of::<&str>());
    println!("{}", std::mem::size_of::<Option<&str>>()); // current optimization
    println!("{}", std::mem::size_of::<StrOrChar>());   // proposed optimization
}

currently prints (on x86_64) 16, 16 and 24. But it could potentially be 16, 16 and 16.

This optimization should be possible if we have an enum with two variants a and b, the a variant contains non-nullable field and the size of a is greater than the size of b (probably size of b should be a divisor of size of a or 0 for alignment).

The current "nullable pointer optimization" is a special case with size of b equal to 0.

src link

@ordian ordian changed the title Extend the "nullable pointer optimization" to two one-field variants Extend the "nullable pointer optimization" to two different size variants Jul 27, 2017
@oli-obk
Copy link
Contributor

oli-obk commented Jul 27, 2017

So basically the char would just occupy the same memory as the length field of the slice? I think the example should use a u32, because a char enables so many other optimizations it could cause confusion.

Also I think this needs an RFC. Also note the related RFC issue: rust-lang/rfcs#1230

@ordian
Copy link
Author

ordian commented Jul 27, 2017

So basically the char would just occupy the same memory as the length field of the slice? I think the example should use a u32, because a char enables so many other optimizations it could cause confusion.

Right.

Also note the related RFC issue: rust-lang/rfcs#1230

Thanks for the link. Should I close this issue in favor of rust-lang/rfcs#1230? This optimization was mentioned in the comment.

@Mark-Simulacrum
Copy link
Member

Yes, I think this is best tracked there -- we need an RFC to make changes in this area, I believe.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants