-
Notifications
You must be signed in to change notification settings - Fork 183
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
split_at
audit
#5970
base: main
Are you sure you want to change the base?
split_at
audit
#5970
Conversation
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 would love to have some description on what/how/why or a link to an issue in the PR.
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.
We should add a restriction lint upstream.
let Some((&[lead, trail], remainder)) = self | ||
.store | ||
.split_at_checked(2) | ||
.map(|(a, b)| (a.as_bytes(), b)) | ||
else { | ||
return Err(MultiNamedPlaceholderError::InvalidStore); | ||
}; |
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.
let Some((&[lead, trail], remainder)) = self | |
.store | |
.split_at_checked(2) | |
.map(|(a, b)| (a.as_bytes(), b)) | |
else { | |
return Err(MultiNamedPlaceholderError::InvalidStore); | |
}; | |
let (&[lead, trail], remainder) = self | |
.store | |
.split_at_checked(2) | |
.ok_or(MultiNamedPlaceholderError::InvalidStore) | |
.map(|(a, b)| (a.as_bytes(), b))?; |
let Some((placeholder_name, remainder)) = | ||
remainder.split_at_checked(placeholder_len as usize) | ||
else { | ||
return Err(MultiNamedPlaceholderError::InvalidStore); | ||
}; |
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.
let Some((placeholder_name, remainder)) = | |
remainder.split_at_checked(placeholder_len as usize) | |
else { | |
return Err(MultiNamedPlaceholderError::InvalidStore); | |
}; | |
let (placeholder_name, remainder) = | |
remainder.split_at_checked(placeholder_len as usize) | |
.ok_or(MultiNamedPlaceholderError::InvalidStore)?; |
// SAFETY: cmp_len is at most self.code_units.len() | ||
let (this, remainder) = unsafe { self.code_units.split_at_unchecked(cmp_len) }; |
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.
No need for unsafe here
// SAFETY: cmp_len is at most self.code_units.len() | |
let (this, remainder) = unsafe { self.code_units.split_at_unchecked(cmp_len) }; | |
let (this, remainder) = self.code_units.split_at_checked(other.len()) | |
.unwrap_or((self.code_units, &[])); |
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.
This is a very hot code path. split_at_checked
introduces a bounds check that might not be optimized out.
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.
We got by with the bounds check so far and had no apparent performance issues. I don't think we should introduce unsafe unless we have actual numbers on what this affects, and whether optimizations are indeed inhibited.
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.
My code snippet also makes cmp_len
unused, which removes the cmp::min
on the previous line, but GitHub wouldn't let me make a suggestion spanning a deleted line.
Co-authored-by: Shane F. Carr <[email protected]>
72c37f0
This reverts commit 72c37f0.
// SAFETY: cmp_len is at most self.code_units.len() | ||
let (this, remainder) = unsafe { self.code_units.split_at_unchecked(cmp_len) }; |
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.
My code snippet also makes cmp_len
unused, which removes the cmp::min
on the previous line, but GitHub wouldn't let me make a suggestion spanning a deleted line.
Why did you unaccept the suggestions from code review? I wrote them with the intention to illustrate what I was talking about, but I expected that they would probably need some coding and formatting to make them actually compile. Note: "requested changes" is because I am against adding new unsafe code. The other changes are cosmetic but reduce the number of lines of code. |
With the 1.80 MSRV we can use the non-panicky alternatives now.