-
Notifications
You must be signed in to change notification settings - Fork 13k
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
librustc: Apply null pointer optimization to slices, closures & trait objects. #15406
Conversation
1 as *const T | ||
static PTR_MARKER: u8 = 0; | ||
if self.ptr.is_null() { | ||
&PTR_MARKER as *const _ as *const 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.
My stalled-out PR #14436 has a commit that relied on changing this to always return self.ptr
. The reason given is that otherwise Vec::from_raw_parts(v.len(), v.cap(), v.as_mut_ptr())
will be incorrect. If at all possible, it would be nice to make that work while you're changing things.
My preference would be to make as_ptr()
and as_mut_ptr()
return null whenever appropriate, and instead do the null-test when constructing the slice.
The |
@thestinger That can't be true, because a brand new |
As I said, the only required change is to the constructor functions. It can use an arbitrary non-null value because it will never pass it to |
@thestinger What benefit is there to having
That said, having |
The non-null enum optimization can and should be extended to library types like |
// pointer as the first element in the vector, but this will end up | ||
// being Some(NULL) which is optimized to None. So instead we set ptr | ||
// to some arbitrary non-null value which is fine since we never call | ||
// deallocate on the ptr if cap is 0. |
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.
This comment is misleading. It suggests the non-null pointer is purely because of 0-sized types (not 0-sized vectors!), but one would expect a size_of()
test in that case. It should be rewritten to explain that Slice wants to never have a null pointer so it can benefit from null pointer optimization, and the easiest way to do that is to make Vec never have a null pointer.
Extend the null ptr optimization to work with slices, closures, procs, & trait objects by using the internal pointers as the discriminant. This decreases the size of `Option<&[int]>` (and similar) by one word.
…r=lnicola Don't provide `generate_default_from_new` when impl self ty is missing Also don't provide the assist when the `Default` trait can't be found. Part of rust-lang#15398
Extend the null ptr optimization to work with slices, closures, procs, & trait objects by using the internal pointers as the discriminant.
This decreases the size of
Option<&[int]>
(and similar) by one word.