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

perf(parser): faster line comment #2327

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ ignore = { version = "0.4.22" }
itertools = { version = "0.12.1" }
jemallocator = { version = "0.5.4" }
lazy_static = { version = "1.4.0" }
memchr = { version = "2.7.1" }
miette = { version = "5.10.0", features = ["fancy-no-backtrace"] }
mimalloc = { version = "0.1.39" }
num-bigint = { version = "0.4.4" }
Expand Down
1 change: 1 addition & 0 deletions crates/oxc_parser/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ oxc_index = { workspace = true }
assert-unchecked = { workspace = true }
bitflags = { workspace = true }
rustc-hash = { workspace = true }
memchr = { workspace = true }
num-bigint = { workspace = true }

[dev-dependencies]
Expand Down
22 changes: 16 additions & 6 deletions crates/oxc_parser/src/lexer/comment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,25 @@ impl<'a> Lexer<'a> {
#[allow(clippy::cast_possible_truncation)]
pub(super) fn skip_single_line_comment(&mut self) -> Kind {
let start = self.token.start;
while let Some(c) = self.next_char() {
if is_line_terminator(c) {
self.token.is_on_new_line = true;
self.trivia_builder
.add_single_line_comment(start, self.offset() - c.len_utf8() as u32);
return Kind::Skip;

// The first byte of the UTF-8 encoding of U+2028 and U+2029.
let ps_ls_start_byte = 0xE2;

while let Some(byte) = self.source.eat_until_byte3(b'\n', b'\r', ps_ls_start_byte) {
// Handle ambiguity between PS/LS and some other UTF-8 character.
if byte == ps_ls_start_byte && !matches!(self.peek(), Some('\u{2028}' | '\u{2029}')) {
self.consume_char();
continue;
}

self.token.is_on_new_line = true;
self.trivia_builder.add_single_line_comment(start, self.offset());
self.consume_char();
return Kind::Skip;
}

// EOF
self.source.set_eof();
self.trivia_builder.add_single_line_comment(start, self.offset());
Kind::Skip
}
Expand Down
41 changes: 41 additions & 0 deletions crates/oxc_parser/src/lexer/source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,16 @@ impl<'a> Source<'a> {
}
}

/// Get remaining source text as `&[u8]`.
pub(super) fn remaining_bytes(&self) -> &'a [u8] {
let len = self.end as usize - self.ptr as usize;
// SAFETY:
// `start` and `end` are created from a `&str` in `Source::new` so span a single allocation.
// Invariant of `Source` is that `ptr` is always >= `start` and <= `end`,
// so a slice spanning `ptr` to `end` will always be part of of a single allocation.
unsafe { slice::from_raw_parts(self.ptr, len) }
}

/// Return whether at end of source.
#[inline]
pub(super) fn is_eof(&self) -> bool {
Expand Down Expand Up @@ -158,6 +168,11 @@ impl<'a> Source<'a> {
self.ptr = pos.ptr;
}

/// Set current position to end of source.
pub(super) fn set_eof(&mut self) {
self.ptr = self.end;
}

/// Get current position in source, relative to start of source.
#[allow(clippy::cast_possible_truncation)]
#[inline]
Expand Down Expand Up @@ -370,6 +385,32 @@ impl<'a> Source<'a> {
debug_assert!(self.ptr >= self.start && self.ptr < self.end);
read_u8(self.ptr)
}

/// Eat until any of the three bytes. Returns the byte that was found. If
/// none of the bytes are found, returns `None` without advancing the
/// position.
///
/// # Panic
/// Panics if:
/// * any of the provided bytes are a UTF-8 continuation byte (0x80-0xBF)
#[inline]
pub(super) fn eat_until_byte3(&mut self, b1: u8, b2: u8, b3: u8) -> Option<u8> {
let continuation_range = 0x80..=0xBF;
assert!(
!continuation_range.contains(&b1)
&& !continuation_range.contains(&b2)
&& !continuation_range.contains(&b3)
);

memchr::memchr3(b1, b2, b3, self.remaining_bytes()).map(|i| {
// SAFETY: The index must be in the slice's range and bytes can only
// be found on valid UTF-8 character boundaries.
self.ptr = unsafe { self.ptr.add(i) };

// SAFETY: There must be a byte at the index if memchr said so.
unsafe { self.peek_byte_unchecked() }
})
}
}

/// Wrapper around a pointer to a position in `Source`.
Expand Down