-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Invalid
trait for space optimization of enums.
#41
Conversation
Add `Invalid` trait and use it for `Option<T>` space optimization, instead of hardcoding "special" types in the compiler. | ||
|
||
# Motivation | ||
|
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.
*T
is nullable. &T
and ~T
are not.
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.
Internally, they are (you can do NPO on them too).
It's not clear to me how this would actually work. How would the |
With associated items (and const-expr sizeof) something like trait Invalid {
static BITPATTERN: [u8, .. sizeof Self];
}
impl<T> Invalid for ~T {
static BITPATTERN: [u8, .. 8] = [0, .. 8];
}
// ... might work, although it seems a little peculiar. |
@sfackler, how is it different from compiler using other built-in traits, such as Deref or Drop? |
The compiler doesn't need to be able to execute an implementation of |
|
struct MyStruct {
...
}
impl Invalid for MyStruct {
...
}
static FOO: Option<MyStruct> = None; |
Grr, that's annoying. I suppose we'd have to prohibit statics with |
I think this feature should be implemented using a private, associated compile-time constant of the
And, to the question of what exactly are the semantics of a "private" trait method, an associated constant or an associated type, I think the answer is that it should work like this:
Also, I don't think that this |
I think this feature should be implemented with a type such as enum OptimizedPtr {
NotNull(uint),
invalid NotNull(0)
} An alternative: enum OptimizedPtr<T: RawPtr> {
NotNull(T),
invalid NullPtr = T::null()
} |
This is an intriguing idea. I have to stew on it a bit, but I like it in principle. |
great idea, could this implement a -1 invalid array index type; and in turn operator[] overloaded to take a potentially invalid array index safely returning Some(T) or None |
Somehow it doesn't feel right to actually instantiate the "invalid" bit pattern as an instance of T. Especially, if T implements Drop (would destructor be invoked on the invalid value? hmm...)
Doesn't this suffer from the same problem as my proposal, in that it requires compile-time evaluation of functions? (or, alternatively, it requires "life-before-main" in order to initialize statics). |
At most recent meeting, we decided that while this idea is promising, the RFC is insufficiently fleshed out and hence we are going to close. Most importantly, it seems like the problems of how to handle static bitpatterns needs to be worked out and specified in detail. If you have a revised proposal that handles statics, feel free to re-open. In general, while the problem this RFC addresses is important, we don't feel it's so urgent that we have to draft a design THIS SECOND, and hence we can take some time to work through the details. |
More Option -> Poll
The improvements to `byte_pair_merge` are: - Changing the `parts` vector to avoid repetition of data. This vector used to store ranges for which the invariant `parts[i].end == parts[i + 1].start` holds, which makes the vector twice as big as it needs to be. Keeping this vector small improves CPU-cache efficiency. - Using `usize::MAX` as a sentinel in lieu of `Optional` for the computation of the minimum rank. This change removes branching from the loop to compute the minimum rank, generating assembly that uses conditional moves instead. Ideally, we could keep the `Optional` and inform it of the sentinel much like `Optional<NonZeroUsize>`. As far as I could tell, specifying custom sentinels for `Optional` has an old Rust [RFC](rust-lang/rfcs#41) that has stalled, so we don't get to have nice things. - Minimizing the number of lookups into `ranks` by looking up ranks once and iteratively updating them after each merge. This reduces the number of rank lookups from `n*m` to `n + O(m)`
Proposing to add
Invalid
trait and use it forOption<T>
space optimization. This is an alternative to RFC #36.