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

refactor(css/parser): reduce size of tokens #6384

Merged
merged 6 commits into from Nov 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 5 additions & 17 deletions crates/swc_css_ast/src/token.rs
Expand Up @@ -89,9 +89,7 @@ pub enum Token {

BadString {
#[cfg_attr(feature = "rkyv", with(swc_atoms::EncodeJsWord))]
value: JsWord,
#[cfg_attr(feature = "rkyv", with(swc_atoms::EncodeJsWord))]
raw: JsWord,
raw_value: JsWord,
},

/// `url(value)`
Expand All @@ -101,10 +99,6 @@ pub enum Token {
#[cfg_attr(feature = "rkyv", with(swc_atoms::EncodeJsWord))]
raw_name: JsWord,
#[cfg_attr(feature = "rkyv", with(swc_atoms::EncodeJsWord))]
before: JsWord,
#[cfg_attr(feature = "rkyv", with(swc_atoms::EncodeJsWord))]
after: JsWord,
#[cfg_attr(feature = "rkyv", with(swc_atoms::EncodeJsWord))]
value: JsWord,
#[cfg_attr(feature = "rkyv", with(swc_atoms::EncodeJsWord))]
raw_value: JsWord,
Expand All @@ -116,8 +110,6 @@ pub enum Token {
#[cfg_attr(feature = "rkyv", with(swc_atoms::EncodeJsWord))]
raw_name: JsWord,
#[cfg_attr(feature = "rkyv", with(swc_atoms::EncodeJsWord))]
value: JsWord,
#[cfg_attr(feature = "rkyv", with(swc_atoms::EncodeJsWord))]
raw_value: JsWord,
},

Expand Down Expand Up @@ -213,11 +205,13 @@ impl Hash for Token {
Token::Ident { value, raw }
| Token::Function { value, raw }
| Token::AtKeyword { value, raw }
| Token::String { value, raw }
| Token::BadString { value, raw } => {
| Token::String { value, raw } => {
value.hash(state);
raw.hash(state);
}
Token::BadString { raw_value } => {
raw_value.hash(state);
}
Token::Hash { value, raw, is_id } => {
value.hash(state);
raw.hash(state);
Expand All @@ -226,27 +220,21 @@ impl Hash for Token {
Token::Url {
name,
raw_name,
before,
after,
value,
raw_value,
} => {
name.hash(state);
raw_name.hash(state);
before.hash(state);
after.hash(state);
value.hash(state);
raw_value.hash(state);
}
Token::BadUrl {
name,
raw_name,
value,
raw_value,
} => {
name.hash(state);
raw_name.hash(state);
value.hash(state);
raw_value.hash(state);
}
Token::Delim { value } => {
Expand Down
4 changes: 0 additions & 4 deletions crates/swc_css_ast/src/value.rs
Expand Up @@ -432,11 +432,7 @@ pub struct UrlValueRaw {
#[cfg_attr(feature = "rkyv", with(swc_atoms::EncodeJsWord))]
pub value: JsWord,
#[cfg_attr(feature = "rkyv", with(swc_atoms::EncodeJsWord))]
pub before: Option<JsWord>,
#[cfg_attr(feature = "rkyv", with(swc_atoms::EncodeJsWord))]
pub raw: Option<JsWord>,
#[cfg_attr(feature = "rkyv", with(swc_atoms::EncodeJsWord))]
pub after: Option<JsWord>,
}

#[ast_node]
Expand Down
20 changes: 6 additions & 14 deletions crates/swc_css_codegen/src/lib.rs
Expand Up @@ -1978,28 +1978,22 @@ where

write_raw!(self, span, &function);
}
Token::BadString { raw, .. } => {
write_str!(self, span, raw);
Token::BadString { raw_value } => {
write_str!(self, span, raw_value);
}
Token::String { raw, .. } => {
write_str!(self, span, raw);
}
Token::Url {
raw_name,
raw_value,
before,
after,
..
} => {
let mut url = String::with_capacity(
raw_name.len() + before.len() + raw_value.len() + after.len() + 2,
);
let mut url = String::with_capacity(raw_name.len() + raw_value.len() + 2);

url.push_str(raw_name);
url.push('(');
url.push_str(before);
url.push_str(raw_value);
url.push_str(after);
url.push(')');

write_str!(self, span, &url);
Expand All @@ -2009,7 +2003,7 @@ where
raw_value,
..
} => {
let mut bad_url = String::with_capacity(raw_name.len() + raw_value.len() + 2);
let mut bad_url = String::with_capacity(raw_value.len() + 2);

bad_url.push_str(raw_name);
bad_url.push('(');
Expand Down Expand Up @@ -2097,12 +2091,10 @@ where
url.push_str(&n.value);

write_str!(self, n.span, &url);
} else if let (Some(before), Some(raw), Some(after)) = (&n.before, &n.raw, &n.after) {
let mut url = String::with_capacity(before.len() + raw.len() + after.len());
} else if let Some(raw) = &n.raw {
let mut url = String::with_capacity(raw.len());

url.push_str(before);
url.push_str(raw);
url.push_str(after);

write_str!(self, n.span, &url);
} else {
Expand Down
2 changes: 0 additions & 2 deletions crates/swc_css_codegen/tests/fixture.rs
Expand Up @@ -283,8 +283,6 @@ impl VisitMut for NormalizeTest {
fn visit_mut_url_value_raw(&mut self, n: &mut UrlValueRaw) {
n.visit_mut_children_with(self);

n.before = None;
n.after = None;
n.raw = None;
}

Expand Down
4 changes: 3 additions & 1 deletion crates/swc_css_minifier/src/compressor/mod.rs
Expand Up @@ -400,7 +400,9 @@ impl VisitMut for Compressor {
| Token::Function { value, .. }
| Token::AtKeyword { value, .. }
| Token::String { value, .. }
| Token::BadString { value, .. }
| Token::BadString {
raw_value: value, ..
}
| Token::Dimension { unit: value, .. }
if !contains_only_ascii_characters(value) =>
{
Expand Down
2 changes: 0 additions & 2 deletions crates/swc_css_minifier/src/compressor/url.rs
Expand Up @@ -46,9 +46,7 @@ impl Compressor {
url.value = Some(Box::new(UrlValue::Raw(UrlValueRaw {
span: *span,
value: escaped.into(),
before: None,
raw: None,
after: None,
})));
}
}
Expand Down
30 changes: 12 additions & 18 deletions crates/swc_css_parser/src/lexer/mod.rs
Expand Up @@ -730,8 +730,7 @@ where
l.reconsume();

return Ok(Token::BadString {
value: (&**buf).into(),
raw: (&**raw).into(),
raw_value: (&**raw).into(),
});
}

Expand Down Expand Up @@ -780,15 +779,17 @@ where

// This section describes how to consume a url token from a stream of code
// points. It returns either a <url-token> or a <bad-url-token>.
fn read_url(&mut self, name: (JsWord, JsWord), mut before: String) -> LexResult<Token> {
fn read_url(&mut self, name: (JsWord, JsWord), before: String) -> LexResult<Token> {
// Initially create a <url-token> with its value set to the empty string.
self.with_buf_and_raw_buf(|l, out, raw| {
raw.push_str(&before);

// Consume as much whitespace as possible.
while let Some(c) = l.next() {
if is_whitespace(c) {
l.consume();

before.push(c);
raw.push(c);
} else {
break;
}
Expand All @@ -807,8 +808,6 @@ where
raw_name: name.1,
value: (&**out).into(),
raw_value: (&**raw).into(),
before: before.into(),
after: js_word!(""),
});
}

Expand All @@ -822,8 +821,6 @@ where
raw_name: name.1,
value: (&**out).into(),
raw_value: (&**raw).into(),
before: before.into(),
after: js_word!(""),
});
}

Expand Down Expand Up @@ -853,25 +850,25 @@ where
Some(')') => {
l.consume();

raw.push_str(&whitespaces);

return Ok(Token::Url {
name: name.0,
raw_name: name.1,
value: (&**out).into(),
raw_value: (&**raw).into(),
before: before.into(),
after: whitespaces.into(),
});
}
None => {
l.emit_error(ErrorKind::UnterminatedUrl);

raw.push_str(&whitespaces);

return Ok(Token::Url {
name: name.0,
raw_name: name.1,
value: (&**out).into(),
raw_value: (&**raw).into(),
before: before.into(),
after: whitespaces.into(),
});
}
_ => {}
Expand All @@ -890,8 +887,7 @@ where
return Ok(Token::BadUrl {
name: name.0,
raw_name: name.1,
value: (before.clone() + (&**raw)).into(),
raw_value: (before.clone() + (&**raw)).into(),
raw_value: (&**raw).into(),
});
}

Expand All @@ -914,8 +910,7 @@ where
return Ok(Token::BadUrl {
name: name.0,
raw_name: name.1,
value: (before.clone() + (&**raw)).into(),
raw_value: (before.clone() + (&**raw)).into(),
raw_value: (&**raw).into(),
});
}

Expand Down Expand Up @@ -946,8 +941,7 @@ where
return Ok(Token::BadUrl {
name: name.0,
raw_name: name.1,
value: (before.clone() + (&**raw)).into(),
raw_value: (before.clone() + (&**raw)).into(),
raw_value: (&**raw).into(),
});
}
}
Expand Down
4 changes: 0 additions & 4 deletions crates/swc_css_parser/src/parser/values_and_units/mod.rs
Expand Up @@ -2870,8 +2870,6 @@ where
raw_name,
value,
raw_value,
before,
after,
} => {
let name_length = raw_name.len() as u32;
let name = Ident {
Expand All @@ -2890,9 +2888,7 @@ where
Default::default(),
),
value,
before: Some(before),
raw: Some(raw_value),
after: Some(after),
})));

Ok(Url {
Expand Down
Expand Up @@ -589,9 +589,7 @@
"ctxt": 0
},
"value": "https://www.example.com/",
"before": "",
"raw": "https://www.example.com/",
"after": ""
"raw": "https://www.example.com/"
},
"modifiers": null
}
Expand Down