-
Notifications
You must be signed in to change notification settings - Fork 13.2k
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
core: use compare_bytes
for more slice element types
#128495
Conversation
rustbot has assigned @Mark-Simulacrum. Use |
This comment has been minimized.
This comment has been minimized.
The Miri subtree was changed cc @rust-lang/miri |
slices of all of these seem fairly rare. Not sure how I feel about adding more specialization stuff for them. Is there any motivation for this beyond "can be done"? |
It's mostly "cause I can" but I can imagine that this is really beneficial when comparing ASCII-only strings. |
I think it would be a great shame if But also I wonder if this optimization is better delegated to the compiler. The strategy here does improve |
(Note that since this is talking about I don't know how feasible this would be to do in LLVM instead. Certainly it'll replace loops with calls to |
I wasn't thinking of LLVM specifically, just that it would be better if we could automate the detection of types eligible for this optimization by inspecting their layout, instead of attempting to enumerate all of them. |
I think the problem is that we need more than just their layout, because But maybe we could detect when it's from |
r? scottmcm I tend to agree that a new specialization trait doesn't make me feel enthusiastic about this. |
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.
One small thing above, then I think this is good to go.
I'm less convinced about things like [bool]
, but being able to get a [NonZero<u8>]
to the before-the-terminator in a CString
for example would make sense, and it would be unfortunate if that meant a worse Ord
.
So once we're doing it for two types, might as well do it for bool
(and ascii::Char
and …) as well.
library/core/src/slice/cmp.rs
Outdated
@@ -182,19 +183,31 @@ impl<A: Ord> SliceOrd for A { | |||
} | |||
} | |||
|
|||
// The type should be treated as an unsigned byte for comparisons. | |||
#[rustc_specialization_trait] | |||
unsafe trait UnsignedByte {} |
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 know it's a module-private thing, but can you elaborate on the name and add # Safety
for the trait? Just "unsigned byte" could accidentally be interpreted as being more a layout thing, where say Rev<u8>
is arguably an "unsigned byte" but definitely shouldn't be implementing this.
I see above it's BytewiseEq
, so maybe UnsignedBytewiseOrd
here or something?
@rustbot ready |
Thanks! @bors r+ |
…iaskrgr Rollup of 8 pull requests Successful merges: - rust-lang#128495 (core: use `compare_bytes` for more slice element types) - rust-lang#128641 (refactor: standardize duplicate processes in parser) - rust-lang#129207 (Lint that warns when an elided lifetime ends up being a named lifetime) - rust-lang#129493 (Create opaque definitions in resolver.) - rust-lang#129619 (Update stacker to 0.1.17) - rust-lang#129672 (Make option-like-enum.rs UB-free and portable) - rust-lang#129780 (add crashtests for several old unfixed ICEs) - rust-lang#129832 (Remove stray dot in `std::char::from_u32_unchecked` documentation) r? `@ghost` `@rustbot` modify labels: rollup
Rollup merge of rust-lang#128495 - joboet:more_memcmp, r=scottmcm core: use `compare_bytes` for more slice element types `bool`, `NonZero<u8>`, `Option<NonZero<u8>>` and `ascii::Char` can be compared the same way as `u8`.
bool
,NonZero<u8>
,Option<NonZero<u8>>
andascii::Char
can be compared the same way asu8
.