Skip to content

Commit

Permalink
Avoid unwrap_or_else in str indexing
Browse files Browse the repository at this point in the history
This provides a small reduction of generated LLVM IR, and leads to a
simpler assembly code.
  • Loading branch information
tmiasko committed Aug 3, 2020
1 parent 829d69b commit 427634b
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions library/core/src/str/mod.rs
Expand Up @@ -1923,7 +1923,10 @@ mod traits {
#[inline]
fn index(self, slice: &str) -> &Self::Output {
let (start, end) = (self.start, self.end);
self.get(slice).unwrap_or_else(|| super::slice_error_fail(slice, start, end))
match self.get(slice) {
Some(s) => s,
None => super::slice_error_fail(slice, start, end),
}
}
#[inline]
fn index_mut(self, slice: &mut str) -> &mut Self::Output {
Expand Down Expand Up @@ -1995,7 +1998,10 @@ mod traits {
#[inline]
fn index(self, slice: &str) -> &Self::Output {
let end = self.end;
self.get(slice).unwrap_or_else(|| super::slice_error_fail(slice, 0, end))
match self.get(slice) {
Some(s) => s,
None => super::slice_error_fail(slice, 0, end),
}
}
#[inline]
fn index_mut(self, slice: &mut str) -> &mut Self::Output {
Expand Down Expand Up @@ -2068,7 +2074,10 @@ mod traits {
#[inline]
fn index(self, slice: &str) -> &Self::Output {
let (start, end) = (self.start, slice.len());
self.get(slice).unwrap_or_else(|| super::slice_error_fail(slice, start, end))
match self.get(slice) {
Some(s) => s,
None => super::slice_error_fail(slice, start, end),
}
}
#[inline]
fn index_mut(self, slice: &mut str) -> &mut Self::Output {
Expand Down

0 comments on commit 427634b

Please sign in to comment.