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

fix(es/minifier): calc function body cost when inline function #5342

Merged
merged 3 commits into from Oct 15, 2022

Conversation

Austaras
Copy link
Member

Description:

A follow up of #5253.

BREAKING CHANGE:

Related issue (if exists):

@kdy1 kdy1 added this to the Planned milestone Aug 1, 2022
@Austaras Austaras force-pushed the inline branch 2 times, most recently from 7f2fda0 to 9252b01 Compare August 1, 2022 09:04
@Austaras Austaras marked this pull request as ready for review August 1, 2022 11:49
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.

Seems like lots of unrelated changes are in this PR.
Can you rebase again to drop them?

@kdy1
Copy link
Member

kdy1 commented Aug 3, 2022

image

Hmm... I'll try vscode extension

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.

I think we should count variables or fix #5367 before merging this.
I can work on it if you prefer it

@kdy1
Copy link
Member

kdy1 commented Aug 3, 2022

I reverted some rust-debug changes because I couldn't see actually changed files.
I think we can split the PR into analyzer fix and this change, if you want

@Austaras
Copy link
Member Author

Austaras commented Aug 3, 2022

I could start working on #5367 this evening or tomorrow

@Austaras
Copy link
Member Author

Austaras commented Aug 3, 2022

I reverted some rust-debug changes because I couldn't see actually changed files. I think we can split the PR into analyzer fix and this change, if you want

Yes, to fix #5367 need to tinker anaylzer anyway..

@Austaras
Copy link
Member Author

Austaras commented Aug 4, 2022

Why does minifier has a seperate trait Storage

@kdy1
Copy link
Member

kdy1 commented Aug 4, 2022

I made it because I wanted to create more cheaper analyzers without repeating work for ctx

I think it's fine to remove it.

@kdy1
Copy link
Member

kdy1 commented Aug 5, 2022

Will this fix #5404?
I'm asking just because I'm not sure

@Austaras
Copy link
Member Author

Austaras commented Aug 5, 2022

Will this fix #5404? I'm asking just because I'm not sure

Unlikely. #5404 is because inline not strong enough. Terser can neither properly inline it, only gcc advanced mode would.

@Austaras
Copy link
Member Author

All obstacles blocking this have been removed. I could restart this PR this weekend.

@Austaras Austaras force-pushed the inline branch 2 times, most recently from 1d85d96 to a093045 Compare October 2, 2022 10:24
@kdy1 kdy1 self-assigned this Oct 2, 2022
@kdy1
Copy link
Member

kdy1 commented Oct 13, 2022

Main:

File: ./benches/full/antd.js
  458111
File: ./benches/full/d3.js
   87613
File: ./benches/full/echarts.js
  321142
File: ./benches/full/jquery.js
   30902
File: ./benches/full/lodash.js
   25119
File: ./benches/full/moment.js
   18626
File: ./benches/full/react.js
    8228
File: ./benches/full/terser.js
  122427
File: ./benches/full/three.js
  158421
File: ./benches/full/typescript.js
  813037
File: ./benches/full/victory.js
  158156
File: ./benches/full/vue.js
   42810

This PR:

File: ./benches/full/antd.js
  458110
File: ./benches/full/d3.js
   87635
File: ./benches/full/echarts.js
  321171
File: ./benches/full/jquery.js
   30902
File: ./benches/full/lodash.js
   25120
File: ./benches/full/moment.js
   18630
File: ./benches/full/react.js
    8228
File: ./benches/full/terser.js
  122427
File: ./benches/full/three.js
  158414
File: ./benches/full/typescript.js
  813366
File: ./benches/full/victory.js
  158152
File: ./benches/full/vue.js
   42796

@Austaras
Copy link
Member Author

Regression in moment is because we inline big number literal which is fine, and I haven't investigated Typescipt because of its size.

All other regerssions can be run down to a single issue

export function foo(a) {
  return bar(a) + bar(a)
}

function bar(a) {
   return a + 1
}

where simple functions(in this case, bar) is declared under usage, so in first pass it's only inlined by Finalizer but not invoked.

@kdy1
Copy link
Member

kdy1 commented Oct 13, 2022

Oh... So we need one more pass.

@kdy1
Copy link
Member

kdy1 commented Oct 13, 2022

I'll create a PR to change default pass limit to 3

@Austaras
Copy link
Member Author

Austaras commented Oct 13, 2022

Maybe not an extra physical pass but something like Finalizer

@kdy1
Copy link
Member

kdy1 commented Oct 13, 2022

I think it's now fine to run a full pass again so I made #6138

@kdy1
Copy link
Member

kdy1 commented Oct 13, 2022

Now #6138 is merged. Can you rebase?

@Austaras
Copy link
Member Author

Sure. I will rebase this and test it tonight.

@Austaras
Copy link
Member Author

Three passes
main

File: ./benches/full/antd.js
459448
File: ./benches/full/d3.js
87351
File: ./benches/full/echarts.js
319813
File: ./benches/full/jquery.js
30766
File: ./benches/full/lodash.js
24907
File: ./benches/full/moment.js
18604
File: ./benches/full/react.js
8230
File: ./benches/full/terser.js
122430
File: ./benches/full/three.js
158065
File: ./benches/full/typescript.js
807256
File: ./benches/full/victory.js
157789
File: ./benches/full/vue.js
42429

inline

File: ./benches/full/antd.js
459422
File: ./benches/full/d3.js
87349
File: ./benches/full/echarts.js
319769
File: ./benches/full/jquery.js
30766
File: ./benches/full/lodash.js
24888
File: ./benches/full/moment.js
18606
File: ./benches/full/react.js
8230
File: ./benches/full/terser.js
122430
File: ./benches/full/three.js
158065
File: ./benches/full/typescript.js
807284
File: ./benches/full/victory.js
157785
File: ./benches/full/vue.js
42416

The only regression is in moment and typescript

@kdy1 kdy1 requested review from jridgewell and kdy1 October 14, 2022 14:04
break;
case 'bottomleft':
setPosx(getElementLeft(), pos), setPosy(getElementBottom(), pos);
setPosx(getElementLeft(), pos), value7 = getElementBottom(), pos.top = value7;
Copy link
Member

@kdy1 kdy1 Oct 14, 2022

Choose a reason for hiding this comment

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

This looks like a regresison. Are we considering the size of the member property correctly?

Copy link
Member Author

@Austaras Austaras Oct 14, 2022

Choose a reason for hiding this comment

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

We count pos.top = value as size 7, as both pos and value can be mangled, and function call cost is 3 + param_count * 2 + 14 / callee_count larger than 7, so it got inlined. This regression is caused by value cannot be inlined, as pos.top may have side effect. However we cannot know if an argument has side effect at the time of inlining since we only know it's declaration.

jridgewell
jridgewell previously approved these changes Oct 15, 2022
usage: &VarUsageInfo,
) -> bool {
let param_cost = param_count * 2;
// if it's passed as value but not called, the function expr cannot be removed
Copy link
Contributor

Choose a reason for hiding this comment

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

If this is the case, couldn't we just return false and skip the work?

Copy link
Member Author

Choose a reason for hiding this comment

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

function foo() {
  g()
}

foo()

module.exports = foo

In this case foo cannot be removed, but still can be inlined

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.

I'm still looking at changes, so this is just a bump comment


swc-bump:

  • swc_ecma_minifier

kdy1
kdy1 previously approved these changes Oct 15, 2022
@kdy1 kdy1 dismissed stale reviews from jridgewell and themself via d230e17 October 15, 2022 04:52
@kdy1 kdy1 enabled auto-merge (squash) October 15, 2022 04:53
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 merged commit b40d486 into swc-project:main Oct 15, 2022
@Austaras Austaras deleted the inline branch October 15, 2022 06:13
@kdy1 kdy1 modified the milestones: Planned, v1.3.9 Oct 17, 2022
@swc-project swc-project locked as resolved and limited conversation to collaborators Nov 16, 2022
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

4 participants