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/parser): Optimize macros #9100

Merged
merged 16 commits into from
Jun 23, 2024
6 changes: 4 additions & 2 deletions crates/swc_ecma_parser/src/lexer/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,10 @@ impl<'a> Lexer<'a> {
(skip.offset, skip.newline)
};

self.input.bump_bytes(offset);
self.state.had_line_break |= newline;
self.input.bump_bytes(offset as usize);
if newline {
self.state.had_line_break = true;
}

if LEX_COMMENTS && self.input.is_byte(b'/') {
if self.peek() == Some('/') {
Expand Down
10 changes: 5 additions & 5 deletions crates/swc_ecma_parser/src/lexer/whitespace.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/// Returns true if it's done
pub(super) type ByteHandler = Option<for<'aa> fn(&mut SkipWhitespace<'aa>) -> usize>;
pub(super) type ByteHandler = Option<for<'aa> fn(&mut SkipWhitespace<'aa>) -> u32>;

/// Lookup table for whitespace
static BYTE_HANDLERS: [ByteHandler; 256] = [
Expand Down Expand Up @@ -39,7 +39,7 @@ const SPC: ByteHandler = Some(|_| 1);
const UNI: ByteHandler = Some(|skip| {
let s = unsafe {
// Safety: `skip.offset` is always valid
skip.input.get_unchecked(skip.offset..)
skip.input.get_unchecked(skip.offset as usize..)
};

let c = unsafe {
Expand All @@ -60,15 +60,15 @@ const UNI: ByteHandler = Some(|skip| {
_ => return 0,
}

c.len_utf8()
c.len_utf8() as u32
});

/// API is taked from oxc by Boshen (https://github.com/Boshen/oxc/pull/26)
pub(super) struct SkipWhitespace<'a> {
pub input: &'a str,

/// Total offset
pub offset: usize,
pub offset: u32,

/// Found newline
pub newline: bool,
Expand All @@ -79,7 +79,7 @@ impl SkipWhitespace<'_> {
pub fn scan(&mut self) {
let mut byte;
loop {
byte = match self.input.as_bytes().get(self.offset).copied() {
byte = match self.input.as_bytes().get(self.offset as usize).copied() {
Some(v) => v,
None => return,
};
Expand Down
16 changes: 8 additions & 8 deletions crates/swc_ecma_parser/src/parser/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ impl<I: Tokens> Parser<I> {
return self.parse_yield_expr();
}

self.state.potential_arrow_start = match *cur!(self, true)? {
self.state.potential_arrow_start = match *cur!(self, true) {
Word(Word::Ident(..)) | tok!('(') | tok!("yield") => Some(cur_pos!(self)),
_ => None,
};
Expand All @@ -154,8 +154,8 @@ impl<I: Tokens> Parser<I> {
fn finish_assignment_expr(&mut self, start: BytePos, cond: Box<Expr>) -> PResult<Box<Expr>> {
trace_cur!(self, finish_assignment_expr);

match cur!(self, false) {
Ok(&Token::AssignOp(op)) => {
match self.input.cur() {
Some(&Token::AssignOp(op)) => {
let left = if op == AssignOp::Assign {
match AssignTarget::try_from(
self.reparse_expr_as_pat(PatType::AssignPat, cond)?,
Expand Down Expand Up @@ -882,7 +882,7 @@ impl<I: Tokens> Parser<I> {
type_params: None,
};
if let BlockStmtOrExpr::BlockStmt(..) = &*arrow_expr.body {
if let Ok(&Token::BinOp(..)) = cur!(self, false) {
if let Some(&Token::BinOp(..)) = self.input.cur() {
// ) is required
self.emit_err(self.input.cur_span(), SyntaxError::TS1005);
let errorred_expr =
Expand Down Expand Up @@ -1063,7 +1063,7 @@ impl<I: Tokens> Parser<I> {
pub(super) fn parse_tpl_element(&mut self, is_tagged_tpl: bool) -> PResult<TplElement> {
let start = cur_pos!(self);

let (raw, cooked) = match *cur!(self, true)? {
let (raw, cooked) = match *cur!(self, true) {
Token::Template { .. } => match bump!(self) {
Token::Template { raw, cooked, .. } => match cooked {
Ok(cooked) => (raw, Some(cooked)),
Expand Down Expand Up @@ -1558,7 +1558,7 @@ impl<I: Tokens> Parser<I> {
Either::Right(r) => r.into(),
}
}
match *cur!(self, true)? {
match *cur!(self, true) {
Token::JSXText { .. } => {
return self
.parse_jsx_text()
Expand Down Expand Up @@ -1707,7 +1707,7 @@ impl<I: Tokens> Parser<I> {
let is_async = is!(self, "async")
&& matches!(
peek!(self),
Ok(tok!('(') | tok!("function") | Token::Word(..))
Some(tok!('(') | tok!("function") | Token::Word(..))
);

let start = cur_pos!(self);
Expand Down Expand Up @@ -2016,7 +2016,7 @@ impl<I: Tokens> Parser<I> {
pub(super) fn parse_lit(&mut self) -> PResult<Lit> {
let start = cur_pos!(self);

let v = match cur!(self, true)? {
let v = match cur!(self, true) {
Word(Word::Null) => {
bump!(self);
let span = span!(self, start);
Expand Down
2 changes: 1 addition & 1 deletion crates/swc_ecma_parser/src/parser/expr/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ impl<I: Tokens> Parser<I> {
Err(err) => {
trace_cur!(self, parse_bin_expr__recovery_unary_err);

match cur!(self, true)? {
match cur!(self, true) {
&tok!("in") if ctx.include_in_expr => {
self.emit_err(self.input.cur_span(), SyntaxError::TS1109);

Expand Down
6 changes: 3 additions & 3 deletions crates/swc_ecma_parser/src/parser/ident.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,12 @@ impl<I: Tokens> Parser<I> {
let start = cur_pos!(self);

let w = match cur!(self, true) {
Ok(&Word(..)) => match bump!(self) {
Word(..) => match bump!(self) {
Word(w) => w.into(),
_ => unreachable!(),
},

Ok(&Token::JSXName { .. }) if in_type => match bump!(self) {
Token::JSXName { .. } if in_type => match bump!(self) {
Token::JSXName { name } => name,
_ => unreachable!(),
},
Expand Down Expand Up @@ -99,7 +99,7 @@ impl<I: Tokens> Parser<I> {

let word = self.parse_with(|p| {
let w = match cur!(p, true) {
Ok(&Word(..)) => match bump!(p) {
&Word(..) => match bump!(p) {
Word(w) => w,
_ => unreachable!(),
},
Expand Down
8 changes: 4 additions & 4 deletions crates/swc_ecma_parser/src/parser/jsx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ impl<I: Tokens> Parser<I> {
trace_cur!(self, parse_jsx_ident);

let ctx = self.ctx();
match *cur!(self, true)? {
match *cur!(self, true) {
Token::JSXName { .. } => match bump!(self) {
Token::JSXName { name } => {
let span = self.input.prev_span();
Expand Down Expand Up @@ -77,7 +77,7 @@ impl<I: Tokens> Parser<I> {

let start = cur_pos!(self);

match *cur!(self, true)? {
match *cur!(self, true) {
tok!('{') => {
let node = self.parse_jsx_expr_container(start)?;

Expand Down Expand Up @@ -313,7 +313,7 @@ impl<I: Tokens> Parser<I> {

if !self_closing {
'contents: loop {
match *cur!(p, true)? {
match *cur!(p, true) {
Token::JSXTagStart => {
let start = cur_pos!(p);

Expand Down Expand Up @@ -409,7 +409,7 @@ impl<I: Tokens> Parser<I> {
trace_cur!(self, parse_jsx_element);

debug_assert!(self.input.syntax().jsx());
debug_assert!({ matches!(*cur!(self, true)?, Token::JSXTagStart | tok!('<')) });
debug_assert!({ matches!(*cur!(self, true), Token::JSXTagStart | tok!('<')) });

let start_pos = cur_pos!(self);

Expand Down
Loading
Loading