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

Slicing after str::starts_with performs unnecessary checks #72558

Open
jhpratt opened this issue May 25, 2020 · 2 comments
Open

Slicing after str::starts_with performs unnecessary checks #72558

jhpratt opened this issue May 25, 2020 · 2 comments
Labels
A-str Area: str and String C-enhancement Category: An issue proposing an enhancement or a PR with one. C-optimization Category: An issue highlighting optimization opportunities or PRs implementing such I-slow Issue: Problems and improvements with respect to performance of generated code. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@jhpratt
Copy link
Member

jhpratt commented 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.

@jonas-schievink jonas-schievink added C-enhancement Category: An issue proposing an enhancement or a PR with one. I-slow Issue: Problems and improvements with respect to performance of generated code. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels May 25, 2020
@the8472
Copy link
Member

the8472 commented May 25, 2020

Assuming you don't need a &str pointing into the original string you could you could use strip_prefix once it becomes stable. And for now split_at could at least offer one less bound check.

@jhpratt
Copy link
Member Author

jhpratt commented May 25, 2020

I don't strictly need the returned value to be a slice of the original, but I tested it and it generated slightly less assembly.

I just created this issue because the bounds & Unicode boundary checks aren't necessary, given we already know the partial contents. I presume this would be an optimization done in MIR.

@workingjubilee workingjubilee added the A-str Area: str and String label Jul 22, 2023
@workingjubilee workingjubilee added the C-optimization Category: An issue highlighting optimization opportunities or PRs implementing such label Oct 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-str Area: str and String C-enhancement Category: An issue proposing an enhancement or a PR with one. C-optimization Category: An issue highlighting optimization opportunities or PRs implementing such I-slow Issue: Problems and improvements with respect to performance of generated code. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

4 participants