Skip to content

Commit

Permalink
Fix some performance regressions (#140).
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexhuszagh committed Sep 15, 2024
1 parent 7778b93 commit 222009e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 19 deletions.
30 changes: 13 additions & 17 deletions lexical-util/src/iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,19 +143,13 @@ pub unsafe trait Iter<'a> {
/// sensitivity.
#[inline(always)]
fn first_is(&mut self, value: u8, is_cased: bool) -> bool {
if let Some(&c) = self.first() {
if is_cased {
c == value
} else {
c.to_ascii_lowercase() == value.to_ascii_lowercase()
}
if is_cased {
self.first_is_cased(value)
} else {
false
self.first_is_uncased(value)
}
}

// TODO(ahuszagh) Change `first_is` to have cased or uncased

// STEP BY
// -------

Expand Down Expand Up @@ -344,14 +338,10 @@ pub trait DigitsIter<'a>: Iterator<Item = &'a u8> + Iter<'a> {
/// sensitivity.
#[inline(always)]
fn peek_is(&mut self, value: u8, is_cased: bool) -> bool {
if let Some(&c) = self.peek() {
if is_cased {
c == value
} else {
c.to_ascii_lowercase() == value.to_ascii_lowercase()
}
if is_cased {
self.peek_is_cased(value)
} else {
false
self.peek_is_uncased(value)
}
}

Expand All @@ -378,7 +368,13 @@ pub trait DigitsIter<'a>: Iterator<Item = &'a u8> + Iter<'a> {
/// Read a value if the value matches the provided one.
#[inline(always)]
fn read_if_value_cased(&mut self, value: u8) -> Option<u8> {
self.read_if(|x| x == value)
if self.peek() == Some(&value) {
// SAFETY: the slice cannot be empty because we peeked a value.
unsafe { self.step_unchecked() };
Some(value)
} else {
None
}
}

/// Read a value if the value matches the provided one without case
Expand Down
4 changes: 2 additions & 2 deletions lexical-util/src/noskip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ unsafe impl<'a, const __: u128> Iter<'a> for Bytes<'a, __> {
///
/// # Safety
///
/// Safe if `index <= selfbuffer_length()`.
/// Safe if `index <= self.buffer_length()`.
#[inline(always)]
unsafe fn set_cursor(&mut self, index: usize) {
debug_assert!(index <= self.buffer_length());
Expand Down Expand Up @@ -228,7 +228,7 @@ unsafe impl<'a: 'b, 'b, const __: u128> Iter<'a> for DigitsIterator<'a, 'b, __>
#[inline(always)]
unsafe fn set_cursor(&mut self, index: usize) {
debug_assert!(index <= self.buffer_length());
// SAFETY: safe if `index <= selfbuffer_length()`.
// SAFETY: safe if `index <= self.buffer_length()`.
unsafe { self.byte.set_cursor(index) };
}

Expand Down

0 comments on commit 222009e

Please sign in to comment.