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(es/lexer): Use string searcher for lexing line comments #7043

Merged
merged 12 commits into from
Mar 10, 2023
Merged
Show file tree
Hide file tree
Changes from 7 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
45 changes: 29 additions & 16 deletions crates/swc_common/src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub type SourceFileInput<'a> = StringInput<'a>;
/// Implementation of [Input].
#[derive(Clone)]
pub struct StringInput<'a> {
cur_pos: BytePos,
start_pos_of_iter: BytePos,
last_pos: BytePos,
/// Current cursor
iter: str::CharIndices<'a>,
Expand All @@ -31,13 +31,26 @@ impl<'a> StringInput<'a> {
assert!(start <= end);

StringInput {
cur_pos: start,
start_pos_of_iter: start,
last_pos: start,
orig: src,
iter: src.char_indices(),
orig_start: start,
}
}

#[inline(always)]
pub fn as_str(&self) -> &str {
self.iter.as_str()
}

pub fn bump_bytes(&mut self, n: usize) {
let s = self.iter.as_str();

self.iter = s[n..].char_indices();
self.last_pos.0 += n as u32;
self.start_pos_of_iter.0 += n as u32;
}
}

/// Creates an [Input] from [SourceFile]. This is an alias for
Expand Down Expand Up @@ -84,7 +97,7 @@ impl<'a> Input for StringInput<'a> {
#[inline]
fn bump(&mut self) {
if let Some((i, c)) = self.iter.next() {
self.last_pos = self.cur_pos + BytePos((i + c.len_utf8()) as u32);
self.last_pos = self.start_pos_of_iter + BytePos((i + c.len_utf8()) as u32);
} else {
unsafe {
debug_unreachable!("bump should not be called when cur() == None");
Expand Down Expand Up @@ -120,7 +133,7 @@ impl<'a> Input for StringInput<'a> {

self.iter = s[end_idx..].char_indices();
self.last_pos = end;
self.cur_pos = end;
self.start_pos_of_iter = end;

ret
}
Expand All @@ -142,7 +155,7 @@ impl<'a> Input for StringInput<'a> {
let ret = &s[..last];

self.last_pos = self.last_pos + BytePos(last as _);
self.cur_pos = self.last_pos;
self.start_pos_of_iter = self.last_pos;
self.iter = s[last..].char_indices();

ret
Expand All @@ -166,7 +179,7 @@ impl<'a> Input for StringInput<'a> {
}

self.last_pos = self.last_pos + BytePos(last as _);
self.cur_pos = self.last_pos;
self.start_pos_of_iter = self.last_pos;
self.iter = s[last..].char_indices();

Some(self.last_pos)
Expand All @@ -179,7 +192,7 @@ impl<'a> Input for StringInput<'a> {

let s = &orig[idx..];
self.iter = s.char_indices();
self.cur_pos = to;
self.start_pos_of_iter = to;
self.last_pos = to;
}

Expand All @@ -197,7 +210,7 @@ impl<'a> Input for StringInput<'a> {
fn eat_byte(&mut self, c: u8) -> bool {
if self.is_byte(c) {
if let Some((i, _)) = self.iter.next() {
self.last_pos = self.cur_pos + BytePos((i + 1) as u32);
self.last_pos = self.start_pos_of_iter + BytePos((i + 1) as u32);
} else {
unsafe {
debug_unreachable!(
Expand Down Expand Up @@ -304,13 +317,13 @@ mod tests {
with_test_sess("foo/d", |mut i| {
assert_eq!(i.slice(BytePos(1), BytePos(2)), "f");
assert_eq!(i.last_pos, BytePos(2));
assert_eq!(i.cur_pos, BytePos(2));
assert_eq!(i.start_pos_of_iter, BytePos(2));
assert_eq!(i.cur(), Some('o'));

assert_eq!(i.slice(BytePos(2), BytePos(4)), "oo");
assert_eq!(i.slice(BytePos(1), BytePos(4)), "foo");
assert_eq!(i.last_pos, BytePos(4));
assert_eq!(i.cur_pos, BytePos(4));
assert_eq!(i.start_pos_of_iter, BytePos(4));
assert_eq!(i.cur(), Some('/'));
});
}
Expand All @@ -320,13 +333,13 @@ mod tests {
with_test_sess("load", |mut i| {
assert_eq!(i.slice(BytePos(1), BytePos(3)), "lo");
assert_eq!(i.last_pos, BytePos(3));
assert_eq!(i.cur_pos, BytePos(3));
assert_eq!(i.start_pos_of_iter, BytePos(3));
assert_eq!(i.cur(), Some('a'));
i.reset_to(BytePos(1));

assert_eq!(i.cur(), Some('l'));
assert_eq!(i.last_pos, BytePos(1));
assert_eq!(i.cur_pos, BytePos(1));
assert_eq!(i.start_pos_of_iter, BytePos(1));
});
}

Expand All @@ -335,12 +348,12 @@ mod tests {
with_test_sess("foo/d", |mut i| {
assert_eq!(i.cur_pos(), BytePos(1));
assert_eq!(i.last_pos, BytePos(1));
assert_eq!(i.cur_pos, BytePos(1));
assert_eq!(i.start_pos_of_iter, BytePos(1));
assert_eq!(i.uncons_while(|c| c.is_alphabetic()), "foo");

// assert_eq!(i.cur_pos(), BytePos(4));
assert_eq!(i.last_pos, BytePos(4));
assert_eq!(i.cur_pos, BytePos(4));
assert_eq!(i.start_pos_of_iter, BytePos(4));
assert_eq!(i.cur(), Some('/'));

i.bump();
Expand All @@ -358,10 +371,10 @@ mod tests {
with_test_sess("foo/d", |mut i| {
assert_eq!(i.cur_pos(), BytePos(1));
assert_eq!(i.last_pos, BytePos(1));
assert_eq!(i.cur_pos, BytePos(1));
assert_eq!(i.start_pos_of_iter, BytePos(1));

assert_eq!(i.find(|c| c == '/'), Some(BytePos(5)));
assert_eq!(i.cur_pos, BytePos(5));
assert_eq!(i.start_pos_of_iter, BytePos(5));
assert_eq!(i.last_pos, BytePos(5));
assert_eq!(i.cur(), Some('d'));
});
Expand Down
1 change: 1 addition & 0 deletions crates/swc_ecma_parser/src/lexer/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,7 @@ impl<'a> Lexer<'a> {
// bar
//
let is_for_next = self.state.had_line_break || !self.state.can_have_trailing_line_comment();

let mut end = self.cur_pos();

while let Some(c) = self.cur() {
Expand Down