Skip to content

Commit

Permalink
perf(css): Reduce size of tokens (#6384)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait committed Nov 10, 2022
1 parent c428e82 commit bea6cce
Show file tree
Hide file tree
Showing 59 changed files with 222 additions and 377 deletions.
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

1 comment on commit bea6cce

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Benchmark

Benchmark suite Current: bea6cce Previous: 26b21b1 Ratio
es/full/bugs-1 336568 ns/iter (± 23232) 348235 ns/iter (± 24345) 0.97
es/full/minify/libraries/antd 1856412549 ns/iter (± 51559288) 1922972701 ns/iter (± 62456339) 0.97
es/full/minify/libraries/d3 422786654 ns/iter (± 20878780) 413812087 ns/iter (± 21402067) 1.02
es/full/minify/libraries/echarts 1538472001 ns/iter (± 15772516) 1649081106 ns/iter (± 62698250) 0.93
es/full/minify/libraries/jquery 101917859 ns/iter (± 5080502) 115734339 ns/iter (± 2701675) 0.88
es/full/minify/libraries/lodash 116503093 ns/iter (± 4825111) 138305217 ns/iter (± 7068393) 0.84
es/full/minify/libraries/moment 59238515 ns/iter (± 2275152) 65603525 ns/iter (± 6197636) 0.90
es/full/minify/libraries/react 20082978 ns/iter (± 690125) 22406865 ns/iter (± 4785033) 0.90
es/full/minify/libraries/terser 292860931 ns/iter (± 8574147) 312904613 ns/iter (± 8091240) 0.94
es/full/minify/libraries/three 545251989 ns/iter (± 12360454) 569381501 ns/iter (± 14391992) 0.96
es/full/minify/libraries/typescript 3286426060 ns/iter (± 23615918) 3475782141 ns/iter (± 170485085) 0.95
es/full/minify/libraries/victory 805671494 ns/iter (± 9331432) 823851166 ns/iter (± 29606990) 0.98
es/full/minify/libraries/vue 148865981 ns/iter (± 5283395) 174813078 ns/iter (± 9426868) 0.85
es/full/codegen/es3 33744 ns/iter (± 706) 33666 ns/iter (± 4174) 1.00
es/full/codegen/es5 33775 ns/iter (± 751) 33303 ns/iter (± 1046) 1.01
es/full/codegen/es2015 33768 ns/iter (± 920) 33412 ns/iter (± 960) 1.01
es/full/codegen/es2016 33864 ns/iter (± 1864) 34122 ns/iter (± 4833) 0.99
es/full/codegen/es2017 33831 ns/iter (± 694) 33991 ns/iter (± 2401) 1.00
es/full/codegen/es2018 33882 ns/iter (± 436) 33386 ns/iter (± 2459) 1.01
es/full/codegen/es2019 33823 ns/iter (± 479) 33334 ns/iter (± 693) 1.01
es/full/codegen/es2020 33900 ns/iter (± 1112) 33106 ns/iter (± 376) 1.02
es/full/all/es3 188865288 ns/iter (± 8759261) 197369340 ns/iter (± 12109582) 0.96
es/full/all/es5 180127504 ns/iter (± 8311815) 179570702 ns/iter (± 8738753) 1.00
es/full/all/es2015 142353693 ns/iter (± 4669647) 144385430 ns/iter (± 10688053) 0.99
es/full/all/es2016 140182180 ns/iter (± 3836377) 141897396 ns/iter (± 5873454) 0.99
es/full/all/es2017 140205383 ns/iter (± 4765211) 142260743 ns/iter (± 12685811) 0.99
es/full/all/es2018 139264525 ns/iter (± 4506820) 139359796 ns/iter (± 4854931) 1.00
es/full/all/es2019 138597011 ns/iter (± 3696290) 140963011 ns/iter (± 6604995) 0.98
es/full/all/es2020 133967552 ns/iter (± 5845734) 133868444 ns/iter (± 5125602) 1.00
es/full/parser 705235 ns/iter (± 24304) 694789 ns/iter (± 38462) 1.02
es/full/base/fixer 26696 ns/iter (± 490) 25697 ns/iter (± 793) 1.04
es/full/base/resolver_and_hygiene 92049 ns/iter (± 4196) 90279 ns/iter (± 3679) 1.02
serialization of ast node 214 ns/iter (± 8) 223 ns/iter (± 11) 0.96
serialization of serde 231 ns/iter (± 5) 211 ns/iter (± 2) 1.09

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.