perf(parser): faster offset calculation (#2215)

A faster way to calculate offset in the lexer.

This only moves the needle because it's on the hottest path in the lexer
- `Lexer::offset` is called for every token in `Lexer::read_next_token`.
This commit is contained in:
overlookmotel 2024-01-30 10:49:31 +00:00 committed by GitHub
parent cd5026c015
commit 81e33a3701
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -252,7 +252,14 @@ impl<'a> Lexer<'a> {
#[inline] #[inline]
#[allow(clippy::cast_possible_truncation)] #[allow(clippy::cast_possible_truncation)]
fn offset(&self) -> u32 { fn offset(&self) -> u32 {
(self.source.len() - self.current.chars.as_str().len()) as u32 // Offset = current position of `chars` relative to start of `source`.
// Previously was `self.source.len() - self.current.chars.as_str().len()`,
// but that was slower because `std::str::Chars` internally is a current pointer + end pointer,
// whereas `&str` internally is a start pointer and len.
// So comparing `len()` of the two requires an extra memory read, and addition operation.
// https://godbolt.org/z/v46MWddTM
// This function is on hot path, so saving even a single instruction makes a measurable difference.
(self.current.chars.as_str().as_ptr() as usize - self.source.as_ptr() as usize) as u32
} }
/// Get the current unterminated token range /// Get the current unterminated token range