perf(es/parser): Improve performance by using #[cold] (#4215)

This commit is contained in:
Donny/강동윤 2022-04-01 15:20:29 +09:00 committed by GitHub
parent 1068561800
commit 164556290b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 73 additions and 101 deletions

View File

@ -102,7 +102,7 @@ impl Scope {
}
}
#[inline]
#[inline(never)]
fn can_rename(&self, id: &Id, symbol: &JsWord, renamed: &RenameMap) -> bool {
if let Some(lefts) = renamed.get_by_right(symbol) {
for left in lefts {

View File

@ -13,7 +13,7 @@ use crate::token::Token;
/// Note: this struct is 8 bytes.
#[derive(Debug, Clone, PartialEq)]
pub struct Error {
pub(crate) error: Box<(Span, SyntaxError)>,
error: Box<(Span, SyntaxError)>,
}
impl Spanned for Error {
@ -23,6 +23,13 @@ impl Spanned for Error {
}
impl Error {
#[cold]
pub(crate) fn new(span: Span, error: SyntaxError) -> Self {
Self {
error: Box::new((span, error)),
}
}
pub fn kind(&self) -> &SyntaxError {
&self.error.1
}

View File

@ -554,7 +554,7 @@ impl<'a, I: Input> Lexer<'a, I> {
// read unicode escape sequences
'u' => match self.read_unicode_escape(raw) {
Ok(chars) => return Ok(Some(chars)),
Err(err) => self.error(start, err.error.1)?,
Err(err) => self.error(start, err.into_kind())?,
},
// octal escape sequences

View File

@ -341,9 +341,7 @@ impl<'a, I: Input> Lexer<'a, I> {
.and_then(|v| v.checked_add(val as u32))
.ok_or_else(|| {
let span = Span::new(start, start, SyntaxContext::empty());
Error {
error: Box::new((span, SyntaxError::InvalidUnicodeEscape)),
}
Error::new(span, SyntaxError::InvalidUnicodeEscape)
})?;
Ok((Some(total), count != len))

View File

@ -130,9 +130,7 @@ impl WithSpan for AssignOpToken {
fn module_legacy_decimal() {
assert_eq!(
lex_module_errors(Syntax::default(), "08"),
vec![Error {
error: Box::new((sp(0..2), SyntaxError::LegacyDecimal)),
}]
vec![Error::new(sp(0..2), SyntaxError::LegacyDecimal)]
);
}
@ -140,9 +138,7 @@ fn module_legacy_decimal() {
fn module_legacy_comment_1() {
assert_eq!(
lex_module_errors(Syntax::default(), "<!-- foo oo"),
vec![Error {
error: Box::new((sp(0..11), SyntaxError::LegacyCommentInModule)),
}]
vec![Error::new(sp(0..11), SyntaxError::LegacyCommentInModule)]
)
}
@ -150,9 +146,7 @@ fn module_legacy_comment_1() {
fn module_legacy_comment_2() {
assert_eq!(
lex_module_errors(Syntax::default(), "-->"),
vec![Error {
error: Box::new((sp(0..3), SyntaxError::LegacyCommentInModule)),
}]
vec![Error::new(sp(0..3), SyntaxError::LegacyCommentInModule)]
)
}
@ -263,18 +257,16 @@ fn tpl_invalid_unicode_escape() {
vec![
tok!('`'),
Token::Template {
cooked: Err(Error {
error: Box::new((
Span {
lo: BytePos(1),
hi: BytePos(3),
ctxt: SyntaxContext::empty(),
},
SyntaxError::BadCharacterEscapeSequence {
expected: "4 hex characters"
}
))
}),
cooked: Err(Error::new(
Span {
lo: BytePos(1),
hi: BytePos(3),
ctxt: SyntaxContext::empty(),
},
SyntaxError::BadCharacterEscapeSequence {
expected: "4 hex characters"
}
)),
raw: "\\unicode".into(),
},
tok!('`'),
@ -285,18 +277,16 @@ fn tpl_invalid_unicode_escape() {
vec![
tok!('`'),
Token::Template {
cooked: Err(Error {
error: Box::new((
Span {
lo: BytePos(1),
hi: BytePos(4),
ctxt: SyntaxContext::empty(),
},
SyntaxError::BadCharacterEscapeSequence {
expected: "1-6 hex characters"
}
))
}),
cooked: Err(Error::new(
Span {
lo: BytePos(1),
hi: BytePos(4),
ctxt: SyntaxContext::empty(),
},
SyntaxError::BadCharacterEscapeSequence {
expected: "1-6 hex characters"
}
)),
raw: "\\u{".into(),
},
tok!('`'),
@ -307,18 +297,16 @@ fn tpl_invalid_unicode_escape() {
vec![
tok!('`'),
Token::Template {
cooked: Err(Error {
error: Box::new((
Span {
lo: BytePos(1),
hi: BytePos(3),
ctxt: SyntaxContext::empty(),
},
SyntaxError::BadCharacterEscapeSequence {
expected: "2 hex characters"
}
))
}),
cooked: Err(Error::new(
Span {
lo: BytePos(1),
hi: BytePos(3),
ctxt: SyntaxContext::empty(),
},
SyntaxError::BadCharacterEscapeSequence {
expected: "2 hex characters"
}
)),
raw: "\\xhex".into(),
},
tok!('`'),

View File

@ -106,9 +106,7 @@ impl<'a, I: Input> Lexer<'a, I> {
#[cold]
#[inline(never)]
pub(super) fn error_span<T>(&mut self, span: Span, kind: SyntaxError) -> LexResult<T> {
Err(Error {
error: Box::new((span, kind)),
})
Err(Error::new(span, kind))
}
#[cold]
@ -126,9 +124,7 @@ impl<'a, I: Input> Lexer<'a, I> {
}
warn!("Lexer error at {:?}", span);
let err = Error {
error: Box::new((span, kind)),
};
let err = Error::new(span, kind);
self.errors.borrow_mut().push(err);
}
@ -147,9 +143,7 @@ impl<'a, I: Input> Lexer<'a, I> {
return;
}
let err = Error {
error: Box::new((span, kind)),
};
let err = Error::new(span, kind);
self.add_module_mode_error(err);
}
@ -166,9 +160,7 @@ impl<'a, I: Input> Lexer<'a, I> {
#[cold]
#[inline(never)]
pub(super) fn emit_module_mode_error_span(&mut self, span: Span, kind: SyntaxError) {
let err = Error {
error: Box::new((span, kind)),
};
let err = Error::new(span, kind);
self.add_module_mode_error(err);
}

View File

@ -247,14 +247,13 @@ macro_rules! cur {
Some(c) => Ok(c),
None => {
if $required {
let err = crate::error::Error {
error: Box::new((last, crate::error::SyntaxError::Eof)),
};
let err = crate::error::Error::new(last, crate::error::SyntaxError::Eof);
return Err(err);
}
Err(crate::error::Error {
error: Box::new((last, crate::error::SyntaxError::Eof)),
})
Err(crate::error::Error::new(
last,
crate::error::SyntaxError::Eof,
))
}
}
}};
@ -274,9 +273,7 @@ Current token is {:?}",
match $p.input.peek() {
Some(c) => Ok(c),
None => {
let err = crate::error::Error {
error: Box::new((last, crate::error::SyntaxError::Eof)),
};
let err = crate::error::Error::new(last, crate::error::SyntaxError::Eof);
Err(err)
}
}
@ -349,9 +346,7 @@ macro_rules! span {
macro_rules! make_error {
($p:expr, $span:expr, $err:expr) => {{
crate::error::Error {
error: Box::new(($span, $err)),
}
crate::error::Error::new($span, $err)
}};
}

View File

@ -221,9 +221,7 @@ impl<I: Tokens> Parser<I> {
return;
}
self.emit_error(Error {
error: Box::new((span, error)),
})
self.emit_error(Error::new(span, error))
}
#[cold]
@ -240,9 +238,7 @@ impl<I: Tokens> Parser<I> {
if self.ctx().ignore_error {
return;
}
let error = Error {
error: Box::new((span, error)),
};
let error = Error::new(span, error);
self.input_ref().add_module_mode_error(error);
}
}

View File

@ -314,16 +314,14 @@ fn illegal_language_mode_directive1() {
let errors = p.take_errors();
assert_eq!(
errors,
vec![Error {
error: Box::new((
Span {
lo: BytePos(20),
hi: BytePos(33),
ctxt: swc_common::SyntaxContext::empty()
},
crate::parser::SyntaxError::IllegalLanguageModeDirective
))
}]
vec![Error::new(
Span {
lo: BytePos(20),
hi: BytePos(33),
ctxt: swc_common::SyntaxContext::empty()
},
crate::parser::SyntaxError::IllegalLanguageModeDirective
)]
);
Ok(program)
@ -341,16 +339,14 @@ fn illegal_language_mode_directive2() {
let errors = p.take_errors();
assert_eq!(
errors,
vec![Error {
error: Box::new((
Span {
lo: BytePos(21),
hi: BytePos(34),
ctxt: swc_common::SyntaxContext::empty()
},
crate::parser::SyntaxError::IllegalLanguageModeDirective
))
}]
vec![Error::new(
Span {
lo: BytePos(21),
hi: BytePos(34),
ctxt: swc_common::SyntaxContext::empty()
},
crate::parser::SyntaxError::IllegalLanguageModeDirective
)]
);
Ok(program)