Skip to content

Commit

Permalink
perf(css/ast): Reduce token size (#6569)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait committed Dec 6, 2022
1 parent 9d0247a commit 8633d27
Show file tree
Hide file tree
Showing 128 changed files with 1,552 additions and 1,043 deletions.
54 changes: 27 additions & 27 deletions crates/swc_css_ast/src/base.rs
Expand Up @@ -88,65 +88,65 @@ pub struct ListOfComponentValues {
pub enum ComponentValue {
// No grammar
#[tag("TokenAndSpan")]
PreservedToken(TokenAndSpan),
PreservedToken(Box<TokenAndSpan>),
#[tag("Function")]
Function(Function),
Function(Box<Function>),
#[tag("SimpleBlock")]
SimpleBlock(SimpleBlock),
SimpleBlock(Box<SimpleBlock>),

// Block Contents grammar
#[tag("DeclarationOrAtRule")]
DeclarationOrAtRule(DeclarationOrAtRule),
DeclarationOrAtRule(Box<DeclarationOrAtRule>),
#[tag("Rule")]
Rule(Rule),
Rule(Box<Rule>),
#[tag("StyleBlock")]
StyleBlock(StyleBlock),
StyleBlock(Box<StyleBlock>),
#[tag("KeyframeBlock")]
KeyframeBlock(KeyframeBlock),
KeyframeBlock(Box<KeyframeBlock>),

// Arbitrary Contents grammar
#[tag("Ident")]
Ident(Ident),
Ident(Box<Ident>),
#[tag("DashedIdent")]
DashedIdent(DashedIdent),
DashedIdent(Box<DashedIdent>),
#[tag("String")]
Str(Str),
Str(Box<Str>),
#[tag("Url")]
Url(Url),
Url(Box<Url>),
#[tag("Integer")]
Integer(Integer),
Integer(Box<Integer>),
#[tag("Number")]
Number(Number),
Number(Box<Number>),
#[tag("Percentage")]
Percentage(Percentage),
Percentage(Box<Percentage>),
#[tag("Dimension")]
Dimension(Dimension),
Dimension(Box<Dimension>),
#[tag("Ratio")]
Ratio(Ratio),
Ratio(Box<Ratio>),
#[tag("UnicodeRange")]
UnicodeRange(UnicodeRange),
UnicodeRange(Box<UnicodeRange>),
#[tag("Color")]
Color(Color),
Color(Box<Color>),
#[tag("AlphaValue")]
AlphaValue(AlphaValue),
AlphaValue(Box<AlphaValue>),
#[tag("Hue")]
Hue(Hue),
Hue(Box<Hue>),
#[tag("CmykComponent")]
CmykComponent(CmykComponent),
CmykComponent(Box<CmykComponent>),
#[tag("Delimiter")]
Delimiter(Delimiter),
Delimiter(Box<Delimiter>),

// Special function Contents grammar
#[tag("CalcSum")]
CalcSum(CalcSum),
CalcSum(Box<CalcSum>),
#[tag("ComplexSelector")]
ComplexSelector(ComplexSelector),
ComplexSelector(Box<ComplexSelector>),
#[tag("LayerName")]
LayerName(LayerName),
LayerName(Box<LayerName>),
#[tag("SupportsCondition")]
SupportsCondition(SupportsCondition),
SupportsCondition(Box<SupportsCondition>),
#[tag("Declaration")]
Declaration(Declaration),
Declaration(Box<Declaration>),
}

#[ast_node]
Expand Down
123 changes: 46 additions & 77 deletions crates/swc_css_ast/src/token.rs
Expand Up @@ -35,6 +35,21 @@ pub enum NumberType {
Number,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, EqIgnoreSpan)]
#[cfg_attr(
feature = "rkyv",
derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)
)]
pub struct DimensionToken {
pub value: f64,
pub raw_value: Atom,
#[cfg_attr(feature = "rkyv", with(swc_atoms::EncodeJsWord))]
pub unit: JsWord,
#[serde(rename = "type")]
pub type_flag: NumberType,
pub raw_unit: Atom,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, EqIgnoreSpan)]
#[cfg_attr(
feature = "rkyv",
Expand All @@ -54,116 +69,80 @@ pub enum Token {
value: JsWord,
raw: Atom,
},

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

/// `@`
AtKeyword {
#[cfg_attr(feature = "rkyv", with(swc_atoms::EncodeJsWord))]
value: JsWord,
raw: Atom,
},

/// `#`
Hash {
is_id: bool,
#[cfg_attr(feature = "rkyv", with(swc_atoms::EncodeJsWord))]
value: JsWord,
raw: Atom,
},

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

BadString {
raw_value: Atom,
raw: Atom,
},

/// `url(value)`
Url {
#[cfg_attr(feature = "rkyv", with(swc_atoms::EncodeJsWord))]
name: JsWord,
raw_name: Atom,
#[cfg_attr(feature = "rkyv", with(swc_atoms::EncodeJsWord))]
value: JsWord,
raw_value: Atom,
/// Name and value
raw: Box<(Atom, Atom)>,
},

BadUrl {
#[cfg_attr(feature = "rkyv", with(swc_atoms::EncodeJsWord))]
name: JsWord,
raw_name: Atom,
raw_value: Atom,
raw: Atom,
},

Delim {
value: char,
},

Number {
value: f64,
raw: Atom,
#[serde(rename = "type")]
type_flag: NumberType,
},

Percentage {
value: f64,
raw: Atom,
},

Dimension {
value: f64,
raw_value: Atom,
#[cfg_attr(feature = "rkyv", with(swc_atoms::EncodeJsWord))]
unit: JsWord,
raw_unit: Atom,
#[serde(rename = "type")]
type_flag: NumberType,
},

Dimension(Box<DimensionToken>),
/// One or more whitespace.
WhiteSpace {
value: Atom,
},

/// `<!--`
CDO,

/// `-->`
CDC,

/// `:``
Colon,

/// `;`
Semi,

/// `,`
Comma,

/// `[`
LBracket,

/// `]`
RBracket,

/// `(`
LParen,

/// `)`
RParen,

/// `{`
LBrace,

/// `}`
RBrace,
}
Expand All @@ -187,40 +166,36 @@ impl Hash for Token {
}

match self {
Token::Ident { value, raw }
| Token::Function { value, raw }
| Token::AtKeyword { value, raw }
| Token::String { value, raw } => {
Token::Ident { value, raw } => {
value.hash(state);
raw.hash(state);
}
Token::BadString { raw_value } => {
raw_value.hash(state);
Token::Function { value, raw } => {
value.hash(state);
raw.hash(state);
}
Token::AtKeyword { value, raw } => {
value.hash(state);
raw.hash(state);
}
Token::String { value, raw } => {
value.hash(state);
raw.hash(state);
}
Token::BadString { raw } => {
raw.hash(state);
}
Token::Hash { value, raw, is_id } => {
value.hash(state);
raw.hash(state);
is_id.hash(state);
}
Token::Url {
name,
raw_name,
value,
raw_value,
} => {
name.hash(state);
raw_name.hash(state);
Token::Url { value, raw } => {
value.hash(state);
raw_value.hash(state);
raw.hash(state);
}
Token::BadUrl {
name,
raw_name,
raw_value,
} => {
name.hash(state);
raw_name.hash(state);
raw_value.hash(state);
Token::BadUrl { raw, .. } => {
raw.hash(state);
}
Token::Delim { value } => {
value.hash(state);
Expand All @@ -238,20 +213,14 @@ impl Hash for Token {
integer_decode(*value).hash(state);
raw.hash(state);
}
Token::Dimension {
value,
raw_value,
unit,
raw_unit,
type_flag,
} => {
integer_decode(*value).hash(state);
raw_value.hash(state);
unit.hash(state);
raw_unit.hash(state);
type_flag.hash(state);
Token::Dimension(dimension) => {
integer_decode(dimension.value).hash(state);
dimension.unit.hash(state);
dimension.type_flag.hash(state);
dimension.raw_value.hash(state);
dimension.raw_unit.hash(state);
}
Token::WhiteSpace { value } => {
Token::WhiteSpace { value, .. } => {
value.hash(state);
}
Token::CDO
Expand Down

1 comment on commit 8633d27

@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: 8633d27 Previous: ad95ead Ratio
es/full/bugs-1 355955 ns/iter (± 23391) 349126 ns/iter (± 51224) 1.02
es/full/minify/libraries/antd 2074256177 ns/iter (± 42312882) 2005709384 ns/iter (± 23971869) 1.03
es/full/minify/libraries/d3 470748383 ns/iter (± 12776484) 431507299 ns/iter (± 17555624) 1.09
es/full/minify/libraries/echarts 1668518668 ns/iter (± 25975328) 1737759431 ns/iter (± 55567864) 0.96
es/full/minify/libraries/jquery 113988748 ns/iter (± 4757739) 115306802 ns/iter (± 8146660) 0.99
es/full/minify/libraries/lodash 137292553 ns/iter (± 4524299) 131533739 ns/iter (± 22798873) 1.04
es/full/minify/libraries/moment 66923243 ns/iter (± 5153896) 67778714 ns/iter (± 6074974) 0.99
es/full/minify/libraries/react 25186007 ns/iter (± 1112396) 22135480 ns/iter (± 2337502) 1.14
es/full/minify/libraries/terser 353103492 ns/iter (± 13037696) 335168257 ns/iter (± 16355075) 1.05
es/full/minify/libraries/three 644744601 ns/iter (± 17554761) 626864277 ns/iter (± 12529049) 1.03
es/full/minify/libraries/typescript 3829266116 ns/iter (± 62621533) 3806480072 ns/iter (± 41925400) 1.01
es/full/minify/libraries/victory 912006407 ns/iter (± 30388870) 980598818 ns/iter (± 62152431) 0.93
es/full/minify/libraries/vue 172806701 ns/iter (± 7097635) 186171761 ns/iter (± 39465352) 0.93
es/full/codegen/es3 34648 ns/iter (± 3653) 35530 ns/iter (± 3535) 0.98
es/full/codegen/es5 34707 ns/iter (± 4450) 34897 ns/iter (± 4061) 0.99
es/full/codegen/es2015 35481 ns/iter (± 4222) 35544 ns/iter (± 5642) 1.00
es/full/codegen/es2016 34698 ns/iter (± 5155) 35340 ns/iter (± 3283) 0.98
es/full/codegen/es2017 34945 ns/iter (± 4089) 36077 ns/iter (± 4358) 0.97
es/full/codegen/es2018 35886 ns/iter (± 6685) 35021 ns/iter (± 5510) 1.02
es/full/codegen/es2019 34359 ns/iter (± 2987) 34958 ns/iter (± 5465) 0.98
es/full/codegen/es2020 35858 ns/iter (± 6349) 35144 ns/iter (± 4445) 1.02
es/full/all/es3 206414744 ns/iter (± 14046727) 198734925 ns/iter (± 15894404) 1.04
es/full/all/es5 189877395 ns/iter (± 17583766) 187761665 ns/iter (± 15165323) 1.01
es/full/all/es2015 150824523 ns/iter (± 7502891) 151063053 ns/iter (± 10703975) 1.00
es/full/all/es2016 163363948 ns/iter (± 16297666) 150740393 ns/iter (± 15137766) 1.08
es/full/all/es2017 156392441 ns/iter (± 12666257) 151336708 ns/iter (± 31271585) 1.03
es/full/all/es2018 151988067 ns/iter (± 10754135) 147259927 ns/iter (± 13510691) 1.03
es/full/all/es2019 146125605 ns/iter (± 7321473) 157021835 ns/iter (± 20201457) 0.93
es/full/all/es2020 141531282 ns/iter (± 5759423) 141912546 ns/iter (± 11883928) 1.00
es/full/parser 736734 ns/iter (± 89877) 761806 ns/iter (± 79287) 0.97
es/full/base/fixer 26595 ns/iter (± 1940) 27263 ns/iter (± 3450) 0.98
es/full/base/resolver_and_hygiene 93144 ns/iter (± 6704) 95987 ns/iter (± 8357) 0.97
serialization of ast node 215 ns/iter (± 21) 213 ns/iter (± 17) 1.01
serialization of serde 233 ns/iter (± 36) 231 ns/iter (± 26) 1.01

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

Please sign in to comment.