-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
Enum layout optimizations #19765
Enum layout optimizations #19765
Conversation
👯 |
\o/ |
Woo! |
awesome 🍰 |
6bb5e2b
to
b8eb099
Compare
f? @jld |
|
||
impl<T> RawPtr<T> for NonZero<*const T> { | ||
#[inline] | ||
fn null() -> NonZero<*const T> { NonZero(null()) } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this is safe at all, even if right now I can't come up with a memory safety violation in safe code using it.
Though someone more motivated could potentially use it to transmute arbitrary values.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The existence of NonZero::null
does seem like a bit of an oxymoron in any case.
// &T/&mut T/Box<T> could either be a thin or fat pointer depending on T | ||
ty::ty_rptr(_, ty::mt { ty, .. }) | ty::ty_uniq(ty) => match ty.sty { | ||
// &[T] and &str are a pointer and length pair | ||
ty::ty_vec(_, None) | ty::ty_str => Some(vec![FAT_PTR_ADDR]), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why special case these things? why not just call type_is_sized
all the time? seems like it just makes the code less DRY
So, this looks basically good to me, the big thing that seems to be missing are tests that do matches and so forth on values that contain |
44ba89c
to
56503a4
Compare
@nikomatsakis Updated. |
@luqmana I'm assuming there were only minor changes here? (r+ under that assumption) |
Actually, revoking r+. The main thing I wanted added was unit tests that test matching against a |
|
||
fn find_discr_field_candidate<'tcx>(tcx: &ty::ctxt<'tcx>, | ||
ty: Ty<'tcx>, | ||
path: &mut DiscrField) -> bool { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: could this return Option<DiscrField>
? (And likewise for find_ptr
.) That seems to be what it's doing anyway, and it would be a little clearer to read.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jld It did originally but I changed it to this way to get rid of all the extraneous allocation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I expessed that badly: I meant returning an optional vector in reverse order, like it's doing now; the recursive cases would move the Vec, push
onto it, and then return it. It's not a big deal in any case.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep, that makes sense. I've updated the code.
49773d8
to
c0badcd
Compare
c0badcd
to
766a719
Compare
This extends the nullable enum opt to traverse beyond just the first level to find possible fields to use as the discriminant. So now, it'll work through structs, tuples, and fixed sized arrays. This also introduces a new lang item, NonZero, that you can use to wrap raw pointers or integral types to indicate to rustc that the underlying value is known to never be 0/NULL. We then use this in Vec, Rc and Arc to have them also benefit from the nullable enum opt. As per rust-lang/rfcs#499 NonZero is not exposed via the `libstd` facade. ``` x86_64 Linux: T Option<T> (Before) Option<T> (After) ---------------------------------------------------------------------------------- Vec<int> 24 32 24 String 24 32 24 Rc<int> 8 16 8 Arc<int> 8 16 8 [Box<int>, ..2] 16 24 16 (String, uint) 32 40 32 ``` Fixes #19419. Fixes #13194. Fixes #9378. Fixes #7576.
Nice wins. |
#[lang="non_zero"] | ||
#[deriving(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Show)] | ||
#[experimental] | ||
pub struct NonZero<T: Zeroable>(T); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@luqmana: Is there any reason this couldn't have been pub struct NonZero<T: Zeroable>(pub T)
?
The way it's currently defined, NonZero cannot be used to initialize statics and consts. :-(
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because we don't have unsafe fields, this has the same issue as UnsafeCell
(public safely modifiable fields can lead to unsafety).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this issue explained anywhere? I must have missed that discussion.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@vadimcn I don't think there's an extended documentation/discussion written anywhere about this but this falls into the safety guarantees. As @eddyb mentioned, there's no support for unsafe fields, therefore we don't have a way to tell the user that accessing a certain field is considered an unsafe
operation.
In the case of UnsafeCell
, the field is public but, as the docstring in the file says, it shouldn't be.
Now, I wonder if it'd be fair to allow calls to constructors on static items by requiring them to be in an unsafe
block.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why accessing the inner value of NonZero is unsafe?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think creating, not accessing, the value is unsafe in this case: NonZero(0)
is bad!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In addition to what @huonw said: NonZero
guarantees the wrapped raw pointer will never be NULL
or 0. If public access to the wrapped pointer is allowed, it would be possible to zero the value out.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@huonw, @flaper87: I can still do these things, since NonZero::new() does not perform any input validation. So what's gained? That I have to wrap it in an unsafe {} block?
This extends the nullable enum opt to traverse beyond just the first level to find possible fields to use as the discriminant. So now, it'll work through structs, tuples, and fixed sized arrays. This also introduces a new lang item, NonZero, that you can use to wrap raw pointers or integral types to indicate to rustc that the underlying value is known to never be 0/NULL. We then use this in Vec, Rc and Arc to have them also benefit from the nullable enum opt.
As per rust-lang/rfcs#499 NonZero is not exposed via the
libstd
facade.Fixes #19419.
Fixes #13194.
Fixes #9378.
Fixes #7576.