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

perf(css/ast): Reduce token size #6569

Merged
merged 43 commits into from Dec 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
8d929c9
perf(css/parser): reduce bad url token size
alexander-akait Dec 2, 2022
efecfbb
perf(css/parser): reduce token size
alexander-akait Dec 2, 2022
b59240f
perf(css/parser): no extra creation
alexander-akait Dec 2, 2022
645edde
perf(css/parser): reduce token sized to 32 bytes
alexander-akait Dec 2, 2022
1902acd
fix: bug
alexander-akait Dec 2, 2022
f36b18a
perf(css/parser): reduce component value size
alexander-akait Dec 2, 2022
61726ea
perf(css/parser): reduce component value size
alexander-akait Dec 2, 2022
bef5337
perf(css/parser): reduce component value size
alexander-akait Dec 2, 2022
598bb06
perf(css/parser): reduce component value size
alexander-akait Dec 2, 2022
2109f4c
perf(css/parser): reduce component value size
alexander-akait Dec 2, 2022
9000b65
perf(css/parser): reduce component value size
alexander-akait Dec 2, 2022
82d5b63
perf(css/parser): reduce component value size
alexander-akait Dec 2, 2022
67dc333
perf(css/parser): reduce component value size
alexander-akait Dec 2, 2022
408da56
refactor: code on boxes
alexander-akait Dec 4, 2022
2b20bea
refactor: css modules
alexander-akait Dec 4, 2022
ae7e4ea
refactor: lints
alexander-akait Dec 4, 2022
bf88623
refactor: code
alexander-akait Dec 4, 2022
81a4565
refactor: fix code
alexander-akait Dec 4, 2022
e2a91fb
fix: box tokens
alexander-akait Dec 4, 2022
b7e95f5
fix: box tokens
alexander-akait Dec 4, 2022
793ce69
fix: box tokens
alexander-akait Dec 4, 2022
853e4aa
fix: box tokens
alexander-akait Dec 4, 2022
a18ed22
fix: codegen
alexander-akait Dec 4, 2022
5d3c0ab
fix: codegen
alexander-akait Dec 4, 2022
a4d919f
fix: code
alexander-akait Dec 4, 2022
e58990f
refactor: code
alexander-akait Dec 4, 2022
6e1d3b4
refactor: code
alexander-akait Dec 4, 2022
ef6b85b
perf: box all tokens
alexander-akait Dec 4, 2022
0789a5d
fix: codegen
alexander-akait Dec 4, 2022
c848e56
fix: code
alexander-akait Dec 4, 2022
9110672
fix: code
alexander-akait Dec 4, 2022
99e0195
fix: cargo
alexander-akait Dec 4, 2022
a4ed346
fix: code
alexander-akait Dec 4, 2022
5f228b7
refactor: unwrap often whitespace token
alexander-akait Dec 5, 2022
54c7fa3
refactor: unwrap often whitespace token
alexander-akait Dec 5, 2022
e1b14df
refactor: unwrap often whitespace token
alexander-akait Dec 5, 2022
71a3f48
test: fix
alexander-akait Dec 5, 2022
5e1b929
refactor: url token
alexander-akait Dec 5, 2022
e2816dc
refactor: tokens
alexander-akait Dec 5, 2022
f2360a6
refactor: bad url token + fix bugs
alexander-akait Dec 5, 2022
b2ba340
fix: clippy
alexander-akait Dec 5, 2022
d3bf833
refactor: code
alexander-akait Dec 6, 2022
03cfb1a
Merge branch 'main' into fix-css-perf
swc-bot Dec 6, 2022
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
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