-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Implement alloc::vec::IsZero
for Option<$NUM>
types
#106989
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
Conversation
r? @cuviper (rustbot has picked a reviewer for you, use r? to override) |
Hey! It looks like you've submitted a new PR for the library teams! If this PR contains changes to any Examples of
|
2b75594
to
254f8c9
Compare
We should reflect that in the trait docs if we make this change. Rather than "this value's representation is all zeros," we'd now be saying something like "this value is equivalent to a value of all zeros." |
254f8c9
to
b94a29a
Compare
Thanks! This looks good to me. @bors r+ rollup=iffy (it's an optimization, but probably not one that will materially affect compiler perf) (Though I still hope that one day safe-transmute will let us do this more generally without needing to enumerate all the things here.) |
We don't really need safe-transmute for this, we need the freeze intrinsic, then we can read all the bytes, not worry about padding and check that they're 0. |
What if we had fallible CTFE? fn is_zero<T: StructuralEq>(x: &T) -> bool {
match try_const { unsafe { core::mem::MaybeUninit::<T>::zeroed().assume_init() } } {
Some(zero) => *x == zero,
None => false,
}
}
We don't really care if there are non-zero bytes in padding though, only that "significant" bytes are zero. I'm not sure how you would "not worry about padding" in general if we're looking at raw bytes. |
transmute T -> MaybeUninit<[u8]> and then freeze to [u8]. For types without padding this always works, which is a lot more than we currently have! For types with padding it depends on what the freeze op gives us for uninitialized bytes. If it plays nice we'll get zeroes too. If not then it's just a lost optimization. |
Or maybe the |
I think even looking at this PR, you're unlikely to get zeros in a frozen
That sounds doable! |
I don't think we need full freeze here for the important parts. Freeze might even be suboptimal, since It'd be enough to have a |
☀️ Test successful - checks-actions |
Finished benchmarking commit (705a96d): comparison URL. Overall result: no relevant changes - no action needed@rustbot label: -perf-regression Instruction countThis benchmark run did not return any relevant results for this metric. Max RSS (memory usage)ResultsThis is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.
CyclesResultsThis is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.
|
Fixes #106911
Mirrors the
NonZero$NUM
implementations with an additionalassert_zero_valid
.None::<i32>
doesn't stricly satisfyIsZero
but for the purpose of allocating we can produce more efficient codegen.