Skip to content
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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open

Conversation

robertbastian
Copy link
Member

@robertbastian robertbastian commented Jan 9, 2025

With the 1.80 MSRV we can use the non-panicky alternatives now.

@robertbastian robertbastian requested review from hsivonen and removed request for zbraniecki, hsivonen, echeran and nciric January 9, 2025 19:22
nciric
nciric previously approved these changes Jan 9, 2025
Copy link
Contributor

@nciric nciric left a 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.

Manishearth
Manishearth previously approved these changes Jan 9, 2025
Copy link
Member

@Manishearth Manishearth left a 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.

Comment on lines +437 to 443
let Some((&[lead, trail], remainder)) = self
.store
.split_at_checked(2)
.map(|(a, b)| (a.as_bytes(), b))
else {
return Err(MultiNamedPlaceholderError::InvalidStore);
};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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))?;

Comment on lines +449 to 453
let Some((placeholder_name, remainder)) =
remainder.split_at_checked(placeholder_len as usize)
else {
return Err(MultiNamedPlaceholderError::InvalidStore);
};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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)?;

Comment on lines +22 to +23
// SAFETY: cmp_len is at most self.code_units.len()
let (this, remainder) = unsafe { self.code_units.split_at_unchecked(cmp_len) };
Copy link
Member

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

Suggested change
// 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, &[]));

Copy link
Member Author

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.

Copy link
Member

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.

Copy link
Member

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.

@robertbastian robertbastian dismissed stale reviews from Manishearth and nciric via 72c37f0 January 10, 2025 08:09
@robertbastian robertbastian requested a review from sffc January 10, 2025 08:09
Comment on lines +22 to +23
// SAFETY: cmp_len is at most self.code_units.len()
let (this, remainder) = unsafe { self.code_units.split_at_unchecked(cmp_len) };
Copy link
Member

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.

@sffc
Copy link
Member

sffc commented Jan 10, 2025

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants