perf(css/parser): Use AtomStore (#8238)

**Related issue:**

 - Closes #8237.
This commit is contained in:
Donny/강동윤 2023-11-07 21:38:54 +09:00 committed by GitHub
parent 4c2547adf2
commit a3c03b30fb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 69 additions and 40 deletions

View File

@ -1,6 +1,6 @@
use std::{cell::RefCell, char::REPLACEMENT_CHARACTER, rc::Rc};
use std::{borrow::Cow, cell::RefCell, char::REPLACEMENT_CHARACTER, rc::Rc};
use swc_atoms::{Atom, JsWord};
use swc_atoms::{Atom, AtomStore, JsWord};
use swc_common::{
comments::{Comment, CommentKind, Comments},
input::Input,
@ -36,6 +36,7 @@ where
raw_buf: Rc<RefCell<String>>,
sub_buf: Rc<RefCell<String>>,
errors: Rc<RefCell<Vec<Error>>>,
atoms: Rc<RefCell<AtomStore>>,
}
impl<'a, I> Lexer<'a, I>
@ -58,6 +59,7 @@ where
sub_buf: Rc::new(RefCell::new(String::with_capacity(32))),
errors: Default::default(),
pending_leading_comments: Default::default(),
atoms: Default::default(),
}
}
@ -177,6 +179,10 @@ where
Some(self.input.last_pos())
}
fn atom(&self, s: Cow<str>) -> JsWord {
self.atoms.borrow_mut().atom(s)
}
}
impl<I> Lexer<'_, I>
@ -270,7 +276,7 @@ where
}
return Ok(Token::WhiteSpace {
value: (&**buf).into(),
value: l.atoms.borrow_mut().atom(&**buf),
});
}),
// U+0022 QUOTATION MARK (")
@ -506,7 +512,7 @@ where
self.pending_leading_comments.push(Comment {
kind: CommentKind::Block,
span: (self.start_pos, last_pos).into(),
text: text.into(),
text: self.atoms.borrow_mut().atom(text),
});
}
@ -552,7 +558,7 @@ where
self.pending_leading_comments.push(Comment {
kind: CommentKind::Line,
span: (self.start_pos, last_pos).into(),
text: text.into(),
text: self.atoms.borrow_mut().atom(text),
});
}
break;
@ -642,7 +648,7 @@ where
}
}
Ok((&**buf).into())
Ok(buf.to_string())
})?;
match self.next() {
@ -724,9 +730,10 @@ where
None => {
l.emit_error(ErrorKind::UnterminatedString);
let mut atoms = l.atoms.borrow_mut();
return Ok(Token::String {
value: (&**buf).into(),
raw: (&**raw).into(),
value: atoms.atom(&**buf),
raw: atoms.atom(&**raw),
});
}
@ -738,7 +745,7 @@ where
l.reconsume();
return Ok(Token::BadString {
raw: (&**raw).into(),
raw: l.atoms.borrow_mut().atom(&**raw),
});
}
@ -778,9 +785,10 @@ where
}
}
let mut atoms = l.atoms.borrow_mut();
Ok(Token::String {
value: (&**buf).into(),
raw: (&**raw).into(),
value: atoms.atom(&**buf),
raw: atoms.atom(&**raw),
})
})
}
@ -809,9 +817,10 @@ where
// U+0029 RIGHT PARENTHESIS ())
// Return the <url-token>.
Some(')') => {
let mut atoms = l.atoms.borrow_mut();
return Ok(Token::Url {
value: (&**out).into(),
raw: Box::new(UrlKeyValue(name.1, (&**raw).into())),
value: atoms.atom(&**out),
raw: Box::new(UrlKeyValue(name.1, atoms.atom(&**raw))),
});
}
@ -820,9 +829,10 @@ where
None => {
l.emit_error(ErrorKind::UnterminatedUrl);
let mut atoms = l.atoms.borrow_mut();
return Ok(Token::Url {
value: (&**out).into(),
raw: Box::new(UrlKeyValue(name.1, (&**raw).into())),
value: atoms.atom(&**out),
raw: Box::new(UrlKeyValue(name.1, atoms.atom(&**raw))),
});
}
@ -842,7 +852,7 @@ where
}
}
Ok((&**buf).into())
Ok(buf.to_string())
})?;
// if the next input code point is U+0029 RIGHT PARENTHESIS ()) or EOF,
@ -854,9 +864,10 @@ where
raw.push_str(&whitespaces);
let mut atoms = l.atoms.borrow_mut();
return Ok(Token::Url {
value: (&**out).into(),
raw: Box::new(UrlKeyValue(name.1, (&**raw).into())),
value: atoms.atom(&**out),
raw: Box::new(UrlKeyValue(name.1, atoms.atom(&**raw))),
});
}
None => {
@ -864,9 +875,10 @@ where
raw.push_str(&whitespaces);
let mut atoms = l.atoms.borrow_mut();
return Ok(Token::Url {
value: (&**out).into(),
raw: Box::new(UrlKeyValue(name.1, (&**raw).into())),
value: atoms.atom(&**out),
raw: Box::new(UrlKeyValue(name.1, atoms.atom(&**raw))),
});
}
_ => {}
@ -1191,7 +1203,8 @@ where
}
}
Ok(((&**buf).into(), (&**raw).into()))
let mut atoms = l.atoms.borrow_mut();
Ok((atoms.atom(&**buf), atoms.atom(&**raw)))
})
}
@ -1297,7 +1310,7 @@ where
}
// Return value and type.
Ok(((&**out).into(), type_flag))
Ok((l.atoms.borrow_mut().atom(&**out), type_flag))
})?;
// Convert repr to a number, and set the value to the returned value.

View File

@ -1,5 +1,6 @@
use std::{borrow::Cow, fmt::Debug, mem::take};
use std::{borrow::Cow, cell::RefCell, fmt::Debug, mem::take, rc::Rc};
use swc_atoms::{Atom, AtomStore};
use swc_common::{BytePos, Span, Spanned, SyntaxContext};
use swc_css_ast::{ComponentValue, FunctionName, ListOfComponentValues, Token, TokenAndSpan};
@ -19,6 +20,8 @@ pub trait ParserInput: Clone + Iterator<Item = TokenAndSpan> {
/// Returns `last_pos`
fn skip_ws(&mut self) -> Option<BytePos>;
fn atom(&self, s: Cow<'_, str>) -> Atom;
}
#[derive(Debug, Clone)]
@ -47,6 +50,10 @@ where
}
}
pub fn atom<'a>(&self, s: impl Into<Cow<'a, str>>) -> Atom {
self.input.atom(s.into())
}
pub fn last_pos(&mut self) -> BytePos {
self.cur();
self.last_pos
@ -210,6 +217,7 @@ pub struct Input<'a> {
input: InputType<'a>,
idx: Vec<usize>,
balance_stack: Option<Vec<BalanceToken>>,
atoms: Rc<RefCell<AtomStore>>,
}
#[derive(Debug, Clone)]
@ -253,6 +261,7 @@ impl<'a> Input<'a> {
input,
idx,
balance_stack,
atoms: Default::default(),
}
}
@ -383,7 +392,10 @@ impl<'a> Input<'a> {
_ => (name.value.clone(), name.value),
},
FunctionName::DashedIdent(name) => match name.raw {
Some(raw) => (format!("--{}", name.value).into(), raw),
Some(raw) => (
self.atoms.borrow_mut().atom(format!("--{}", name.value)),
raw,
),
_ => (name.value.clone(), name.value),
},
};
@ -478,6 +490,10 @@ impl<'a> ParserInput for Input<'a> {
last_pos
}
fn atom(&self, s: Cow<str>) -> Atom {
self.atoms.borrow_mut().atom(s)
}
}
impl<'a> Iterator for Input<'a> {

View File

@ -1288,7 +1288,7 @@ where
}
a = Some(if has_minus_sign { -1 } else {1 });
a_raw = Some((if has_plus_sign { "+" } else if has_minus_sign { "-" } else { "" }).into());
a_raw = Some(self.input.atom(if has_plus_sign { "+" } else if has_minus_sign { "-" } else { "" }));
n_value = if has_minus_sign { ident_value[1..].to_string() } else { ident_value.to_string() };
}
@ -1357,7 +1357,7 @@ where
b_raw_str.push_str("- ");
b_raw_str.push_str(&number.1);
b_raw = Some(b_raw_str.into());
b_raw = Some(self.input.atom(b_raw_str));
}
// '+'? n ['+' | '-'] <signless-integer>
// -n ['+' | '-'] <signless-integer>
@ -1387,7 +1387,7 @@ where
b_raw_str.push(b_sign_raw);
b_raw_str.push(' ');
b_raw_str.push_str(&number.1);
b_raw = Some(b_raw_str.into());
b_raw = Some(self.input.atom(b_raw_str));
}
// '+'? <ndashdigit-ident>
// <dashndashdigit-ident>
@ -1408,7 +1408,7 @@ where
b_raw_str.push('-');
b_raw_str.push_str(b_from_ident);
b_raw = Some(b_raw_str.into());
b_raw = Some(self.input.atom(b_raw_str));
}
// '+'? n
// -n

View File

@ -151,7 +151,7 @@ where
let name = if is_dashed_ident {
AtRuleName::DashedIdent(DashedIdent {
span: Span::new(span.lo + BytePos(1), span.hi, Default::default()),
value: at_keyword_name.0[2..].into(),
value: self.input.atom(&at_keyword_name.0[2..]),
raw: Some(at_keyword_name.1),
})
} else {
@ -935,7 +935,7 @@ where
let name = if is_dashed_ident {
FunctionName::DashedIdent(DashedIdent {
span: Span::new(span.lo, span.hi - BytePos(1), Default::default()),
value: function_name.0[2..].into(),
value: self.input.atom(&function_name.0[2..]),
raw: Some(function_name.1),
})
} else {

View File

@ -1878,7 +1878,7 @@ where
Ok(DashedIdent {
span,
value: value[2..].into(),
value: self.input.atom(&value[2..]),
raw: Some(raw),
})
}
@ -2301,7 +2301,7 @@ where
},
unit: Ident {
span: Span::new(span.hi - BytePos(unit_len), span.hi, Default::default()),
value: unit.to_lowercase().into(),
value: self.input.atom(unit.to_lowercase()),
raw: Some(raw_unit),
},
})
@ -2571,7 +2571,7 @@ where
let name_length = raw.0.len() as u32;
let name = Ident {
span: Span::new(span.lo, span.lo + BytePos(name_length), Default::default()),
value: "url".into(),
value: self.input.atom("url"),
raw: Some(raw.0),
};
let value = Some(Box::new(UrlValue::Raw(UrlValueRaw {
@ -2948,9 +2948,9 @@ where
// 4. Exit this algorithm.
return Ok(UnicodeRange {
span: span!(self, span.lo),
start: start.into(),
start: self.input.atom(start),
end: None,
raw: Some(unicode_range.into()),
raw: Some(self.input.atom(unicode_range)),
});
}
@ -2962,9 +2962,9 @@ where
if next.is_none() {
return Ok(UnicodeRange {
span: span!(self, span.lo),
start: start.into(),
start: self.input.atom(start),
end: None,
raw: Some(unicode_range.into()),
raw: Some(self.input.atom(unicode_range)),
});
}
@ -3022,9 +3022,9 @@ where
return Ok(UnicodeRange {
span: span!(self, span.lo),
start: start.into(),
end: Some(end.into()),
raw: Some(unicode_range.into()),
start: self.input.atom(start),
end: Some(self.input.atom(end)),
raw: Some(self.input.atom(unicode_range)),
});
}
}