-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Specialize slice::fill with Copy type and u8/i8/bool #81874
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
use crate::ptr::write_bytes; | ||
|
||
pub(super) trait SpecFill<T> { | ||
fn spec_fill(&mut self, value: T); | ||
} | ||
|
||
impl<T: Clone> SpecFill<T> for [T] { | ||
default fn spec_fill(&mut self, value: T) { | ||
if let Some((last, elems)) = self.split_last_mut() { | ||
for el in elems { | ||
el.clone_from(&value); | ||
} | ||
|
||
*last = value | ||
} | ||
} | ||
} | ||
|
||
impl<T: Copy> SpecFill<T> for [T] { | ||
default fn spec_fill(&mut self, value: T) { | ||
for item in self.iter_mut() { | ||
*item = value; | ||
} | ||
} | ||
} | ||
|
||
impl SpecFill<u8> for [u8] { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The only difference when not specialize for Maybe it is not worth to keep those specs for u8/i8/bool slice. |
||
fn spec_fill(&mut self, value: u8) { | ||
// SAFETY: this is slice of u8 | ||
unsafe { | ||
let ptr = self.as_mut_ptr(); | ||
let len = self.len(); | ||
write_bytes(ptr, value, len); | ||
} | ||
} | ||
} | ||
|
||
impl SpecFill<i8> for [i8] { | ||
fn spec_fill(&mut self, value: i8) { | ||
// SAFETY: this is slice of i8 | ||
unsafe { | ||
let ptr = self.as_mut_ptr(); | ||
let len = self.len(); | ||
write_bytes(ptr, value as u8, len); | ||
} | ||
} | ||
} | ||
|
||
impl SpecFill<bool> for [bool] { | ||
fn spec_fill(&mut self, value: bool) { | ||
// SAFETY: this is slice of bool | ||
unsafe { | ||
let ptr = self.as_mut_ptr(); | ||
let len = self.len(); | ||
write_bytes(ptr, value as u8, len); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Instead of specializing separately on
[u8]
below you can do aif mem::size_of::<T>() == 1
here and call memset for any type that's 1 byte wide.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.
Hm, that's another option. Will do that! Thanks.
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.
value
now has 1-byte size.ptr::write_bytes
only accepts u8 as second argument.Should I transmute
value
to u8 ? Is it sound ?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.
Even if I want to transmute, the compiler forbids me: https://rust.godbolt.org/z/a3bsYs
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.
transmute_copy
should workThere 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.
Neat! That's really work! But I wonder if that those specific specialization for [u8] matters much with #81874 (comment)
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.
The advantage is that it doesn't rely on the optimizer, so it'll work in debug mode too or with less powerful backends.
With the
size_of
approach it only has to be implemented once instead of one specialization for each 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.
I don't have strong opinion on this. I defer that to reviewer to decide.
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.
Hi @bjorn3. the8472 and I were discussing if other codegen backends could handle straight
for loop
to copy item and make it to memcpy when item is 1-byte size.Do you think cranelift could be benefited from this optimization ^^?