Skip to content

Commit

Permalink
refactor(css/parser): Canonicalize only if required (#6532)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait committed Nov 30, 2022
1 parent a19ff08 commit 251e98c
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 285 deletions.
23 changes: 4 additions & 19 deletions crates/swc_css_parser/src/parser/at_rules/mod.rs
Expand Up @@ -187,7 +187,6 @@ where
{
let ctx = Ctx {
in_import_at_rule: true,
block_contents_grammar: BlockContentsGrammar::DeclarationValue,
..self.ctx
};
let func = self.with_ctx(ctx).parse_as::<Function>()?;
Expand Down Expand Up @@ -784,7 +783,6 @@ where
Token::Function { value, .. } if *value.to_ascii_lowercase() == *"supports" => {
let ctx = Ctx {
in_import_at_rule: true,
block_contents_grammar: BlockContentsGrammar::DeclarationValue,
..self.ctx
};
let func = self.with_ctx(ctx).parse_as::<Function>()?;
Expand Down Expand Up @@ -1189,7 +1187,6 @@ where
Token::Function { value, .. } if &*value.to_ascii_lowercase() == "selector" => {
// TODO improve me
let ctx = Ctx {
block_contents_grammar: BlockContentsGrammar::DeclarationValue,
in_supports_at_rule: true,
..self.ctx
};
Expand Down Expand Up @@ -1217,7 +1214,7 @@ where
match cur!(self) {
tok!("function") => {
let ctx = Ctx {
block_contents_grammar: BlockContentsGrammar::DeclarationList,
need_canonicalize: false,
..self.ctx
};
let function = self.with_ctx(ctx).parse_as::<Function>()?;
Expand Down Expand Up @@ -1297,11 +1294,7 @@ where
Ok(DocumentPreludeMatchingFunction::Url(self.parse()?))
} else {
// TODO improve me
let ctx = Ctx {
block_contents_grammar: BlockContentsGrammar::DeclarationValue,
..self.ctx
};
let function = self.with_ctx(ctx).parse_as::<Function>()?;
let function = self.parse()?;

Ok(DocumentPreludeMatchingFunction::Function(function))
}
Expand Down Expand Up @@ -1881,11 +1874,7 @@ where
tok!("ident") => Ok(MediaFeatureValue::Ident(self.parse()?)),
tok!("dimension") => Ok(MediaFeatureValue::Dimension(self.parse()?)),
Token::Function { value, .. } if is_math_function(value) => {
let ctx = Ctx {
block_contents_grammar: BlockContentsGrammar::DeclarationValue,
..self.ctx
};
let function = self.with_ctx(ctx).parse_as::<Function>()?;
let function = self.parse()?;

Ok(MediaFeatureValue::Function(function))
}
Expand Down Expand Up @@ -2478,11 +2467,7 @@ where
tok!("ident") => Ok(SizeFeatureValue::Ident(self.parse()?)),
tok!("dimension") => Ok(SizeFeatureValue::Dimension(self.parse()?)),
Token::Function { value, .. } if is_math_function(value) => {
let ctx = Ctx {
block_contents_grammar: BlockContentsGrammar::DeclarationValue,
..self.ctx
};
let function = self.with_ctx(ctx).parse_as::<Function>()?;
let function = self.parse()?;

Ok(SizeFeatureValue::Function(function))
}
Expand Down
22 changes: 20 additions & 2 deletions crates/swc_css_parser/src/parser/mod.rs
Expand Up @@ -55,7 +55,6 @@ pub enum BlockContentsGrammar {
DeclarationList,
RuleList,
Stylesheet,
DeclarationValue,
}

impl Default for BlockContentsGrammar {
Expand All @@ -64,11 +63,12 @@ impl Default for BlockContentsGrammar {
}
}

#[derive(Debug, Default, Clone, Copy)]
#[derive(Debug, Clone, Copy)]
struct Ctx {
is_top_level: bool,
block_contents_grammar: BlockContentsGrammar,
mixed_with_declarations: bool,
need_canonicalize: bool,

in_keyframes_at_rule: bool,
in_supports_at_rule: bool,
Expand All @@ -78,6 +78,24 @@ struct Ctx {
in_font_feature_values_at_rule: bool,
}

impl Default for Ctx {
fn default() -> Self {
Ctx {
is_top_level: false,
block_contents_grammar: BlockContentsGrammar::default(),
mixed_with_declarations: false,
need_canonicalize: true,

in_keyframes_at_rule: false,
in_supports_at_rule: false,
in_import_at_rule: false,
in_page_at_rule: false,
in_container_at_rule: false,
in_font_feature_values_at_rule: false,
}
}
}

#[derive(Debug, Clone)]
pub struct Parser<I>
where
Expand Down
28 changes: 20 additions & 8 deletions crates/swc_css_parser/src/parser/syntax/mod.rs
Expand Up @@ -181,7 +181,9 @@ where
at_rule.span = span!(self, span.lo);

// Canonicalization against a grammar
at_rule = self.canonicalize_at_rule_prelude(at_rule)?;
if self.ctx.need_canonicalize {
at_rule = self.canonicalize_at_rule_prelude(at_rule)?;
}

return Ok(at_rule);
}
Expand All @@ -197,8 +199,10 @@ where
at_rule.span = span!(self, span.lo);

// Canonicalization against a grammar
at_rule = self.canonicalize_at_rule_prelude(at_rule)?;
at_rule = self.canonicalize_at_rule_block(at_rule)?;
if self.ctx.need_canonicalize {
at_rule = self.canonicalize_at_rule_prelude(at_rule)?;
at_rule = self.canonicalize_at_rule_block(at_rule)?;
}

return Ok(at_rule);
}
Expand Down Expand Up @@ -267,8 +271,11 @@ where
};

// Canonicalization against a grammar
qualified_rule = self.canonicalize_qualified_rule_prelude(qualified_rule)?;
qualified_rule = self.canonicalize_qualified_rule_block(qualified_rule)?;
if self.ctx.need_canonicalize {
qualified_rule =
self.canonicalize_qualified_rule_prelude(qualified_rule)?;
qualified_rule = self.canonicalize_qualified_rule_block(qualified_rule)?;
}

return Ok(qualified_rule);
}
Expand Down Expand Up @@ -748,7 +755,9 @@ where
}

// Canonicalization against a grammar
declaration = self.canonicalize_declaration_value(declaration)?;
if self.ctx.need_canonicalize {
declaration = self.canonicalize_declaration_value(declaration)?;
}

// 8. Return the declaration.
Ok(declaration)
Expand All @@ -774,7 +783,8 @@ where
let function = self
.with_ctx({
Ctx {
block_contents_grammar: BlockContentsGrammar::DeclarationList,
// We canonize it later
need_canonicalize: false,
..self.ctx
}
})
Expand Down Expand Up @@ -944,7 +954,9 @@ where
function.span = span!(self, span.lo);

// Canonicalization against a grammar
function = self.canonicalize_function_value(function)?;
if self.ctx.need_canonicalize {
function = self.canonicalize_function_value(function)?;
}

return Ok(function);
}
Expand Down
30 changes: 12 additions & 18 deletions crates/swc_css_parser/src/parser/util.rs
Expand Up @@ -267,27 +267,21 @@ where
&mut self,
mut function: Function,
) -> PResult<Function> {
match self.ctx.block_contents_grammar {
BlockContentsGrammar::DeclarationList => {}
_ => {
let function_name = function.name.value.to_ascii_lowercase();
let function_name = function.name.value.to_ascii_lowercase();
let locv = self.create_locv(function.value);

let locv = self.create_locv(function.value);
function.value = match self.parse_according_to_grammar(&locv, |parser| {
parser.parse_function_values(&function_name)
}) {
Ok(values) => values,
Err(err) => {
if *err.kind() != ErrorKind::Ignore {
self.errors.push(err);
}

function.value = match self.parse_according_to_grammar(&locv, |parser| {
parser.parse_function_values(&function_name)
}) {
Ok(values) => values,
Err(err) => {
if *err.kind() != ErrorKind::Ignore {
self.errors.push(err);
}

locv.children
}
};
locv.children
}
}
};

Ok(function)
}
Expand Down

1 comment on commit 251e98c

@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: 251e98c Previous: ae63220 Ratio
es/full/bugs-1 334484 ns/iter (± 25253) 328281 ns/iter (± 14505) 1.02
es/full/minify/libraries/antd 1985033267 ns/iter (± 54616024) 1936638965 ns/iter (± 26461414) 1.02
es/full/minify/libraries/d3 451264885 ns/iter (± 20399640) 409472672 ns/iter (± 3718591) 1.10
es/full/minify/libraries/echarts 1729017452 ns/iter (± 117064850) 1641509822 ns/iter (± 15448592) 1.05
es/full/minify/libraries/jquery 119329511 ns/iter (± 3731635) 107651357 ns/iter (± 2429310) 1.11
es/full/minify/libraries/lodash 128354724 ns/iter (± 6467181) 125391172 ns/iter (± 5042330) 1.02
es/full/minify/libraries/moment 70627826 ns/iter (± 5695381) 63523602 ns/iter (± 1340855) 1.11
es/full/minify/libraries/react 20674106 ns/iter (± 710523) 21093081 ns/iter (± 420074) 0.98
es/full/minify/libraries/terser 334164428 ns/iter (± 20205839) 319842943 ns/iter (± 13909937) 1.04
es/full/minify/libraries/three 603461179 ns/iter (± 8553684) 597323434 ns/iter (± 9788521) 1.01
es/full/minify/libraries/typescript 3708197985 ns/iter (± 111801337) 3568454333 ns/iter (± 17492505) 1.04
es/full/minify/libraries/victory 905737848 ns/iter (± 24437369) 870602004 ns/iter (± 10791535) 1.04
es/full/minify/libraries/vue 167752283 ns/iter (± 9716446) 157338383 ns/iter (± 2894028) 1.07
es/full/codegen/es3 33029 ns/iter (± 391) 33202 ns/iter (± 929) 0.99
es/full/codegen/es5 32717 ns/iter (± 614) 32994 ns/iter (± 958) 0.99
es/full/codegen/es2015 32818 ns/iter (± 952) 33147 ns/iter (± 1089) 0.99
es/full/codegen/es2016 32662 ns/iter (± 1004) 33376 ns/iter (± 2363) 0.98
es/full/codegen/es2017 32587 ns/iter (± 969) 33009 ns/iter (± 626) 0.99
es/full/codegen/es2018 32652 ns/iter (± 1581) 33078 ns/iter (± 938) 0.99
es/full/codegen/es2019 32719 ns/iter (± 556) 33168 ns/iter (± 647) 0.99
es/full/codegen/es2020 32867 ns/iter (± 698) 33223 ns/iter (± 754) 0.99
es/full/all/es3 186076105 ns/iter (± 11338774) 183486426 ns/iter (± 5689106) 1.01
es/full/all/es5 178762626 ns/iter (± 5144917) 173780099 ns/iter (± 5910213) 1.03
es/full/all/es2015 143152033 ns/iter (± 5274196) 138934513 ns/iter (± 4593874) 1.03
es/full/all/es2016 140941150 ns/iter (± 5224637) 138214548 ns/iter (± 3661703) 1.02
es/full/all/es2017 140996963 ns/iter (± 5534235) 137482885 ns/iter (± 4924360) 1.03
es/full/all/es2018 138547701 ns/iter (± 7491855) 135912938 ns/iter (± 6216641) 1.02
es/full/all/es2019 137104844 ns/iter (± 7630053) 135419782 ns/iter (± 2420503) 1.01
es/full/all/es2020 131219634 ns/iter (± 5035700) 130267688 ns/iter (± 5366045) 1.01
es/full/parser 702270 ns/iter (± 35195) 685824 ns/iter (± 19834) 1.02
es/full/base/fixer 26360 ns/iter (± 342) 25955 ns/iter (± 1049) 1.02
es/full/base/resolver_and_hygiene 91446 ns/iter (± 1905) 88616 ns/iter (± 2952) 1.03
serialization of ast node 220 ns/iter (± 2) 210 ns/iter (± 5) 1.05
serialization of serde 218 ns/iter (± 5) 215 ns/iter (± 5) 1.01

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

Please sign in to comment.