Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

fix(es/minifier): take ident for ObjectPatProp::Assign with value: Some(_) #9465

Closed
wants to merge 5 commits into from

Conversation

wjw99830
Copy link

@wjw99830 wjw99830 commented Aug 20, 2024

Description:
In order to remove unused variables declared by object destructuring with default value, call take_ident_of_pat_if_unused for ObjectPatProp::Assign whose value match Some(_)

Related issue:

@wjw99830 wjw99830 requested a review from a team as a code owner August 20, 2024 07:50
Copy link

changeset-bot bot commented Aug 20, 2024

⚠️ No Changeset found

Latest commit: 717bec5

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@CLAassistant
Copy link

CLAassistant commented Aug 20, 2024

CLA assistant check
All committers have signed the CLA.

@kdy1 kdy1 requested review from magic-akari and Austaras August 20, 2024 07:57
@@ -0,0 +1,3 @@
const a = 1;
const { b = "b", c, d } = {};
Copy link
Member

Choose a reason for hiding this comment

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

Even with pure_getters, the computation of the default value for assignment may still have side effects.
For example:

const { b = console.log("b"), c, d } = {};

@Austaras What do you think?

Copy link
Author

Choose a reason for hiding this comment

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

ignore_return_value will check if the expr is side-effect-free.

Copy link
Author

Choose a reason for hiding this comment

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

ignore_return_value will check if the expr is side-effect-free.

It seems that take_ident_of_pat_if_unused will return before calling ignore_return_value, so that the expression will always be removed if unused.

Copy link
Member

@Austaras Austaras Aug 20, 2024

Choose a reason for hiding this comment

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

ignore_return_value will check if the expr is side-effect-free.

No. There're multiple places that would call take_pat_if_unused like drop_unused_arrow_params

Copy link
Member

@Austaras Austaras Aug 20, 2024

Choose a reason for hiding this comment

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

Maybe just check that assign value is side effect free using may_have_side_effects?

Copy link
Author

Choose a reason for hiding this comment

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

may_have_side_effects

I tried it. But expressions with pure annotation cannot be recognized so that there're still side-effect-free expressions in output code.

Input code

const { a = /*#__PURE__*/ sideEffectFree(), b = "b" } = {};

Output code

const { a = /*#__PURE__*/ sideEffectFree() } = {};

Copy link
Author

Choose a reason for hiding this comment

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

See the latest commit, expected output is

const { a = sideEffect(), d } = {};
console.log(d);

but got

const { a = /*#__PURE__*/ sideEffectFree(sideEffect()), d } = {};
console.log(d);

is_pure_callee doesn't consume the pure annotation.

Copy link
Member

Choose a reason for hiding this comment

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

It's intentional, and if we want to drop it we can invoke ignore_return_value from the method.

Copy link
Author

@wjw99830 wjw99830 Aug 21, 2024

Choose a reason for hiding this comment

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

It seems that ignore_return_value doesn't consume pure either. Its return expr is same as input expr.
image

@kdy1 kdy1 added this to the Planned milestone Aug 20, 2024
Copy link

codspeed-hq bot commented Aug 20, 2024

CodSpeed Performance Report

Merging #9465 will not alter performance

Comparing wjw99830:hotfix/issue_9460 (717bec5) with main (b0b5e36)

Summary

✅ 178 untouched benchmarks

Copy link
Member

@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.

Can we extract all side effects to somewhere, and drop useless bindings instead of checking for side-effect-free from here?

It's how other passes work.

@kdy1
Copy link
Member

kdy1 commented Aug 21, 2024

I made #9478

@wjw99830
Copy link
Author

drop useless bindings

Assign of object pattern only run when the binding's value is undefined. If we drop the binding, the expr will always run.

@wjw99830
Copy link
Author

Maybe we could invoke has_mark from ignore_return_value?
It's sure that the return value of expr is unused when calling ignore_return_value, and we need the assignment indeed.

@kdy1
Copy link
Member

kdy1 commented Aug 21, 2024

Yeap it would be fine.

@wjw99830
Copy link
Author

wjw99830 commented Aug 21, 2024

Should we apply the optimize of take_pat_if_unused for unpredictable expression when pure_getters is true?

export function Component(props) {
  const { a, b } = props;
}

pure_getters means that all unpredictable objects are pure.

@wjw99830 wjw99830 requested a review from kdy1 August 21, 2024 10:04
@@ -164,6 +164,53 @@ impl Optimizer<'_> {
params.retain(|p| !p.is_invalid());
}

#[cfg_attr(feature = "debug", tracing::instrument(skip_all))]
fn take_ident_of_obj_pat_if_unused_and_side_effect_free(
Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Author

Choose a reason for hiding this comment

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

OK.
This pr could be closed and then follow #9478.

@wjw99830 wjw99830 closed this Aug 21, 2024
kdy1 added a commit that referenced this pull request Aug 22, 2024
**Related issue:**

 - Closes #9459
 - Closes #9460
 - Closes #9465
@kdy1 kdy1 modified the milestones: Planned, v1.7.17 Aug 23, 2024
@swc-project swc-project locked as resolved and limited conversation to collaborators Sep 23, 2024
# for free to subscribe to this conversation on GitHub. Already have an account? #.
Labels
None yet
Development

Successfully merging this pull request may close these issues.

Cannot remove unused variables declared by object destructuring with default value
5 participants