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

Hardcode accepting 0 as a valid str char boundary #32456

Merged
merged 2 commits into from
Mar 26, 2016
Merged

Conversation

bluss
Copy link
Member

@bluss bluss commented Mar 23, 2016

If we check explicitly for index == 0, that removes the need to read the
byte at index 0, so it avoids a trip to the string's memory, and it
optimizes out the slicing index' bounds check whenever it is (a constant) zero.

@rust-highfive
Copy link
Collaborator

r? @alexcrichton

(rust_highfive has picked a reviewer for you, use r? to override)

@alexcrichton
Copy link
Member

Thanks! Could you add a comment as well indicating that it's redundant, but nice for perf? Other than that r=me

@bluss bluss changed the title Accept 0 as a valid str char boundary Hardcode accepting 0 as a valid str char boundary Mar 24, 2016
Index 0 must be a valid char boundary (invariant of str that it contains
valid UTF-8 data).

If we check explicitly for index == 0, that removes the need to read the
byte at index 0, so it avoids a trip to the string's memory, and it
optimizes out the slicing index' bounds check whenever it is zero.

With this change, the following examples all change from having a read of
the byte at 0 and a branch to possibly panicing, to having the bounds
checking optimized away.

```rust
pub fn split(s: &str) -> (&str, &str) {
    s.split_at(0)
}

pub fn both(s: &str) -> &str {
    &s[0..s.len()]
}

pub fn first(s: &str) -> &str {
    &s[..0]
}

pub fn last(s: &str) -> &str {
    &s[0..]
}
```
@bluss
Copy link
Member Author

bluss commented Mar 24, 2016

Sure, that's a good idea. Updated with the comment.

@bors r=alexcrichton

@bors
Copy link
Contributor

bors commented Mar 24, 2016

📌 Commit f621193 has been approved by alexcrichton

Manishearth added a commit to Manishearth/rust that referenced this pull request Mar 26, 2016
Hardcode accepting 0 as a valid str char boundary

If we check explicitly for index == 0, that removes the need to read the
byte at index 0, so it avoids a trip to the string's memory, and it
optimizes out the slicing index' bounds check whenever it is (a constant) zero.
bors added a commit that referenced this pull request Mar 26, 2016
Manishearth added a commit to Manishearth/rust that referenced this pull request Mar 26, 2016
Hardcode accepting 0 as a valid str char boundary

If we check explicitly for index == 0, that removes the need to read the
byte at index 0, so it avoids a trip to the string's memory, and it
optimizes out the slicing index' bounds check whenever it is (a constant) zero.
bors added a commit that referenced this pull request Mar 26, 2016
Rollup of 11 pull requests

- Successful merges: #32131, #32199, #32257, #32325, #32435, #32447, #32448, #32456, #32469, #32476, #32482
- Failed merges: #32240
@bors bors merged commit f621193 into rust-lang:master Mar 26, 2016
@bluss bluss deleted the str-zero branch March 26, 2016 19:11
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