Slicing after str::starts_with
performs unnecessary checksΒ #72558
Open
Description
opened on May 25, 2020
Without unsafe (playground)
pub struct Foo<'a> {
string: &'a str,
}
impl Foo<'_> {
pub fn bar(&mut self) -> Option<&str> {
if self.string.starts_with("[[") {
let bracket = &self.string[..1];
self.string = &self.string[2..];
Some(bracket)
} else {
None
}
}
}
With unsafe (playground)
pub struct Foo<'a> {
string: &'a str,
}
impl Foo<'_> {
pub fn bar(&mut self) -> Option<&str> {
if self.string.starts_with("[[") {
let bracket = unsafe { self.string.get_unchecked(..1) };
self.string = unsafe { self.string.get_unchecked(2..) };
Some(bracket)
} else {
None
}
}
}
I'm not terribly familiar with assembly, but the compiler is clearly performing checks where they aren't necessary. Given the knowledge that the string starts with a literal (in this case two ASCII bytes), we should be able to slice without any additional performance costs.
Metadata
Assignees
Labels
Area: str and StringCategory: An issue proposing an enhancement or a PR with one.Category: An issue highlighting optimization opportunities or PRs implementing suchIssue: Problems and improvements with respect to performance of generated code.Relevant to the compiler team, which will review and decide on the PR/issue.
Activity