From 78955995a2fd381a9d56a325e368aa29dcc3c7be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Donny/=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Tue, 6 Dec 2022 22:04:34 +0900 Subject: [PATCH] perf(css/minifier): Improve performance (#6583) --- .../swc_css_minifier/src/compressor/rules.rs | 31 +++++++++++++------ 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/crates/swc_css_minifier/src/compressor/rules.rs b/crates/swc_css_minifier/src/compressor/rules.rs index b5266b26762..c8b5a83c794 100644 --- a/crates/swc_css_minifier/src/compressor/rules.rs +++ b/crates/swc_css_minifier/src/compressor/rules.rs @@ -482,12 +482,23 @@ impl Compressor { pub(super) fn compress_simple_block(&mut self, simple_block: &mut SimpleBlock) { let mut names: AHashMap = Default::default(); - let mut prev_rule: Option = None; + let mut prev_rule_idx = None; let mut remove_rules_list = vec![]; let mut prev_index = 0; - let mut index = 0; - simple_block.value.retain_mut(|rule| { + for index in 0..simple_block.value.len() { + // We need two &mut + let (a, b) = simple_block.value.split_at_mut(index); + + let mut prev_rule = match prev_rule_idx { + Some(idx) => a.get_mut(idx), + None => None, + }; + let rule = match b.first_mut() { + Some(v) => v, + None => continue, + }; + let result = match rule { ComponentValue::Rule(box Rule::AtRule(box AtRule { block: Some(block), .. @@ -600,24 +611,24 @@ impl Compressor { if self.is_mergeable_at_rule(at_rule) => { prev_index = index; - prev_rule = Some(rule.clone()); + prev_rule_idx = Some(index); } ComponentValue::Rule(box Rule::QualifiedRule(_)) | ComponentValue::StyleBlock(box StyleBlock::QualifiedRule(_)) => { prev_index = index; - prev_rule = Some(rule.clone()); + prev_rule_idx = Some(index); } _ => { - prev_rule = None; + prev_rule_idx = None; } } - - index += 1; } - result - }); + if !result { + remove_rules_list.push(index); + } + } if !names.is_empty() { self.discard_overridden(