From 1020242946cb9f235790a0d0c940b2cfc2126123 Mon Sep 17 00:00:00 2001 From: overlookmotel Date: Tue, 30 Jul 2024 22:58:11 +0100 Subject: [PATCH] perf(parser): use `-` not `saturating_sub` --- crates/oxc_parser/src/lexer/source.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/crates/oxc_parser/src/lexer/source.rs b/crates/oxc_parser/src/lexer/source.rs index 8c108ba6d2494b..88b0fe6e8a47ca 100644 --- a/crates/oxc_parser/src/lexer/source.rs +++ b/crates/oxc_parser/src/lexer/source.rs @@ -501,9 +501,11 @@ impl<'a> Source<'a> { /// Peek next two bytes of source without consuming them. #[inline] pub(super) fn peek_2_bytes(&self) -> Option<[u8; 2]> { - if (self.end as usize).saturating_sub(self.ptr as usize) >= 2 { + // `end` is always >= `ptr` so `end - ptr` cannot wrap around. + // No need to use checked/saturating subtraction here. + if (self.end as usize) - (self.ptr as usize) >= 2 { // SAFETY: The check above ensures that there are at least 2 bytes to - // read from `self.ptr` without overflowing past `self.end`. + // read from `self.ptr` without reading past `self.end` let bytes = unsafe { self.position().read2() }; Some(bytes) } else {