mirror of
https://github.com/swc-project/swc.git
synced 2024-12-25 22:56:11 +03:00
perf(html/parser): Improve performance (#4934)
This commit is contained in:
parent
953453ca8c
commit
f993a52679
@ -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_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)
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct Lexer<I>
|
||||
where
|
||||
I: Input,
|
||||
@ -150,7 +149,7 @@ where
|
||||
return_state: State,
|
||||
errors: Vec<Error>,
|
||||
last_start_tag_name: Option<JsWord>,
|
||||
pending_tokens: Vec<TokenAndSpan>,
|
||||
pending_tokens: VecDeque<TokenAndSpan>,
|
||||
current_doctype_token: Option<Doctype>,
|
||||
current_comment_token: Option<String>,
|
||||
current_tag_token: Option<Tag>,
|
||||
@ -178,7 +177,7 @@ where
|
||||
return_state: State::Data,
|
||||
errors: vec![],
|
||||
last_start_tag_name: None,
|
||||
pending_tokens: Vec::with_capacity(32),
|
||||
pending_tokens: VecDeque::new(),
|
||||
current_doctype_token: None,
|
||||
current_comment_token: None,
|
||||
current_tag_token: None,
|
||||
@ -327,7 +326,7 @@ where
|
||||
);
|
||||
|
||||
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)]
|
||||
@ -802,20 +801,20 @@ where
|
||||
|
||||
self.emit_token(Token::Character {
|
||||
value: normalized_c,
|
||||
raw: Some(raw.into()),
|
||||
raw: None,
|
||||
});
|
||||
}
|
||||
|
||||
fn read_token_and_span(&mut self) -> LexResult<TokenAndSpan> {
|
||||
if self.finished {
|
||||
return Err(ErrorKind::Eof);
|
||||
} else {
|
||||
while self.pending_tokens.is_empty() {
|
||||
self.run()?;
|
||||
}
|
||||
}
|
||||
|
||||
while self.pending_tokens.is_empty() && !self.finished {
|
||||
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 {
|
||||
Token::Eof => {
|
||||
|
@ -6,7 +6,7 @@ use swc_html_ast::{Token, TokenAndSpan};
|
||||
use super::PResult;
|
||||
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 last_pos(&mut self) -> BytePos;
|
||||
|
Loading…
Reference in New Issue
Block a user