-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Optimise vec![false; N] to zero-alloc #58628
Conversation
Nowadays booleans have a well-defined representation, so there is no reason not to optimise their allocation.
r? @kennytm (rust_highfive has picked a reviewer for you, use r? to override) |
This makes sense to me and I agree the change is correct, but should it perhaps have a test somewhere? Just a boring one that |
I think we could add a few more of these:
Also the
Is this going overboard? 😆 |
At the end of this PR, let's do a perf test :) |
Seems fine, but please leave comments re. what representation assumptions are actually a stable part of the language and which are just made because this is libstd... |
@oli-obk As for As for the other types, sure, they could be added, but, just like above, tried to keep scope of changes minimal. I can add them here, but they don't seem too common in vectors, and I'm more interested in a generic solution that would be applicable to newtypes and such. Right now wrapping any of these values into a newtype breaks optimisation. Unfortunately, the only way to make this generic, as mentioned on the original PR, would be to read bytes of type and check if they are all zero, but this is hard to do without reading the padding too, and that one is currently defined as UB. Hence, it will require waiting for a well-defined memory model. |
Actually, I juts recalled that I had another horrible idea for making this work on newtypes and any other types, although I'm not 100% sure if it's sound, but it shouldn't be UB. I can give it a try in this same PR if you'd like to take a look? |
Now you've made me curious, yes please
Makes sense. |
sigh looks like my idea, while works with LLVM, is currently also unsound / UB in Rust terms, so nevermind. As such, I guess I'd prefer to limit this PR to its current scope or could add more types as requested. |
Ok, it's not necessary to do these now, as you said, they are rather rare. @bors r+ rollup |
📌 Commit 9f58c5f has been approved by |
It's not gonna show up in perf I believe. Even if we are using |
I think he meant a generic benchmark results. Although, since boolean is essentially a
(c) #40409 (comment) |
…-obk Optimise vec![false; N] to zero-alloc Nowadays booleans have a well-defined representation, so there is no reason not to optimise their allocation.
…-obk Optimise vec![false; N] to zero-alloc Nowadays booleans have a well-defined representation, so there is no reason not to optimise their allocation.
…-obk Optimise vec![false; N] to zero-alloc Nowadays booleans have a well-defined representation, so there is no reason not to optimise their allocation.
…-obk Optimise vec![false; N] to zero-alloc Nowadays booleans have a well-defined representation, so there is no reason not to optimise their allocation.
…-obk Optimise vec![false; N] to zero-alloc Nowadays booleans have a well-defined representation, so there is no reason not to optimise their allocation.
Rollup of 16 pull requests Successful merges: - #58100 (Transition librustdoc to Rust 2018) - #58122 (RangeInclusive internal iteration performance improvement.) - #58199 (Add better error message for partial move) - #58227 (Updated RELEASES.md for 1.33.0) - #58353 (Check the Self-type of inherent associated constants) - #58453 (SGX target: fix panic = abort) - #58476 (Remove `LazyTokenStream`.) - #58526 (Special suggestion for illegal unicode curly quote pairs) - #58595 (Turn duration consts into associated consts) - #58609 (Allow Self::Module to be mutated.) - #58628 (Optimise vec![false; N] to zero-alloc) - #58643 (Don't generate minification variables if minification disabled) - #58648 (Update tests to account for cross-platform testing and miri.) - #58654 (Do not underflow after resetting unmatched braces count) - #58658 (Add expected/provided byte alignments to validation error message) - #58667 (Reduce Miri-related Code Repetition `like (n << amt) >> amt`) Failed merges: r? @ghost
Nowadays booleans have a well-defined representation, so there is no reason not to optimise their allocation.