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/minifier): Remove more redundant clones #6580

Merged
merged 14 commits into from Dec 6, 2022
Merged

Conversation

kdy1
Copy link
Member

@kdy1 kdy1 commented Dec 6, 2022

Description:

Related issue (if exists):

@kdy1 kdy1 added this to the Planned milestone Dec 6, 2022
@kdy1 kdy1 self-assigned this Dec 6, 2022
Copy link
Member Author

@kdy1 kdy1 left a comment

Choose a reason for hiding this comment

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

swc-bump:

  • swc_css_ast

@alexander-akait
Copy link
Collaborator

Please use:

fn discard_overridden(
        &self,
        parent_node: ParentNode,
        names: &mut AHashMap<Name, isize>,
        remove_rules_list: &mut Vec<usize>,
    ) {
        let mut discarder = |at_rule: &AtRule| match &at_rule.prelude {
            Some(box AtRulePrelude::CounterStylePrelude(CustomIdent { value: name, .. })) => {
                if let Some(counter) = names.get_mut(&Name::CounterStyle(name.clone())) {
                    if *counter > 1 {
                        *counter -= 1;

                        false
                    } else {
                        true
                    }
                } else {
                    false
                }
            }
            Some(box AtRulePrelude::KeyframesPrelude(KeyframesName::CustomIdent(
                box CustomIdent { value: name, .. },
            )))
            | Some(box AtRulePrelude::KeyframesPrelude(KeyframesName::Str(box Str {
                value: name,
                ..
            }))) => {
                let counter = names.get_mut(&Name::Keyframes(
                    self.get_at_rule_name(at_rule),
                    name.clone(),
                ));

                if let Some(counter) = counter {
                    if *counter > 1 {
                        *counter -= 1;

                        false
                    } else {
                        true
                    }
                } else {
                    false
                }
            }
            _ => true,
        };

        match parent_node {
            ParentNode::Stylesheet(stylesheet) => {
                for index in 0..stylesheet.rules.len() {
                    let node = stylesheet.rules.get(index);

                    match node {
                        Some(Rule::AtRule(box at_rule)) => {
                            if !discarder(&at_rule) {
                                remove_rules_list.push(index);
                            }
                        },
                        _ => { },
                    }
                }
            }
            ParentNode::SimpleBlock(simple_block) => {
                for index in 0..simple_block.value.len() {
                    let node = simple_block.value.get(index);

                    match node {
                        Some(ComponentValue::Rule(box Rule::AtRule(box at_rule))) => {
                            if !discarder(&at_rule) {
                                remove_rules_list.push(index);
                            }
                        },
                        _ => { },
                    }
                }
            }
        }
    }

and

self.discard_overridden(
  ParentNode::Stylesheet(stylesheet),
  &mut names,
  &mut remove_rules_list,
);

@kdy1 kdy1 marked this pull request as ready for review December 6, 2022 11:00
@kdy1 kdy1 enabled auto-merge (squash) December 6, 2022 11:01
@kdy1 kdy1 changed the title perf(css/minifier): Remove more redundant clone perf(css/minifier): Remove more redundant clones Dec 6, 2022
Copy link
Collaborator

@swc-bot swc-bot left a comment

Choose a reason for hiding this comment

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

Automated review comment generated by auto-rebase script

@kdy1 kdy1 disabled auto-merge December 6, 2022 11:47
@kdy1 kdy1 merged commit 4171ee7 into swc-project:main Dec 6, 2022
@kdy1 kdy1 deleted the clone2 branch December 6, 2022 11:48
@kdy1 kdy1 modified the milestones: Planned, v1.3.22 Dec 9, 2022
@swc-project swc-project locked as resolved and limited conversation to collaborators Jan 8, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants