perf(html/parser): Improve performance (#4934)

This commit is contained in:
Alexander Akait 2022-06-11 07:37:01 +03:00 committed by GitHub
parent 953453ca8c
commit f993a52679
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 12 deletions

View File

@ -1,4 +1,4 @@
use std::{char::REPLACEMENT_CHARACTER, mem::take}; use std::{char::REPLACEMENT_CHARACTER, collections::VecDeque, mem::take};
use swc_atoms::JsWord; use swc_atoms::JsWord;
use swc_common::{collections::AHashSet, input::Input, BytePos, Span}; use swc_common::{collections::AHashSet, input::Input, BytePos, Span};
@ -136,7 +136,6 @@ pub(crate) type LexResult<T> = Result<T, ErrorKind>;
// TODO improve `raw` for all tokens (linting + better codegen) // TODO improve `raw` for all tokens (linting + better codegen)
#[derive(Clone)]
pub struct Lexer<I> pub struct Lexer<I>
where where
I: Input, I: Input,
@ -150,7 +149,7 @@ where
return_state: State, return_state: State,
errors: Vec<Error>, errors: Vec<Error>,
last_start_tag_name: Option<JsWord>, last_start_tag_name: Option<JsWord>,
pending_tokens: Vec<TokenAndSpan>, pending_tokens: VecDeque<TokenAndSpan>,
current_doctype_token: Option<Doctype>, current_doctype_token: Option<Doctype>,
current_comment_token: Option<String>, current_comment_token: Option<String>,
current_tag_token: Option<Tag>, current_tag_token: Option<Tag>,
@ -178,7 +177,7 @@ where
return_state: State::Data, return_state: State::Data,
errors: vec![], errors: vec![],
last_start_tag_name: None, last_start_tag_name: None,
pending_tokens: Vec::with_capacity(32), pending_tokens: VecDeque::new(),
current_doctype_token: None, current_doctype_token: None,
current_comment_token: None, current_comment_token: None,
current_tag_token: None, current_tag_token: None,
@ -327,7 +326,7 @@ where
); );
self.last_token_pos = self.input.cur_pos(); self.last_token_pos = self.input.cur_pos();
self.pending_tokens.push(TokenAndSpan { span, token }); self.pending_tokens.push_back(TokenAndSpan { span, token });
} }
#[inline(always)] #[inline(always)]
@ -802,20 +801,20 @@ where
self.emit_token(Token::Character { self.emit_token(Token::Character {
value: normalized_c, value: normalized_c,
raw: Some(raw.into()), raw: None,
}); });
} }
fn read_token_and_span(&mut self) -> LexResult<TokenAndSpan> { fn read_token_and_span(&mut self) -> LexResult<TokenAndSpan> {
if self.finished { if self.finished {
return Err(ErrorKind::Eof); return Err(ErrorKind::Eof);
} } else {
while self.pending_tokens.is_empty() {
while self.pending_tokens.is_empty() && !self.finished {
self.run()?; self.run()?;
} }
}
let token_and_span = self.pending_tokens.remove(0); let token_and_span = self.pending_tokens.pop_front().unwrap();
match token_and_span.token { match token_and_span.token {
Token::Eof => { Token::Eof => {

View File

@ -6,7 +6,7 @@ use swc_html_ast::{Token, TokenAndSpan};
use super::PResult; use super::PResult;
use crate::{error::Error, lexer::State}; use crate::{error::Error, lexer::State};
pub trait ParserInput: Clone + Iterator<Item = TokenAndSpan> { pub trait ParserInput: Iterator<Item = TokenAndSpan> {
fn start_pos(&mut self) -> BytePos; fn start_pos(&mut self) -> BytePos;
fn last_pos(&mut self) -> BytePos; fn last_pos(&mut self) -> BytePos;