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

[Meta] Known issues for the minifier #2934

Closed
kdy1 opened this issue Dec 1, 2021 · 8 comments
Closed

[Meta] Known issues for the minifier #2934

kdy1 opened this issue Dec 1, 2021 · 8 comments

Comments

@kdy1
Copy link
Member

kdy1 commented Dec 1, 2021

Rules to port from google closure compiler

defaults

We have to enable

  • reduce_funcs
  • reduce_vars

by default


Rules to implement

Promise.resolve

If it's immediately resolved, we can drop some nodes.

@kdy1 kdy1 pinned this issue Dec 1, 2021
@kdy1 kdy1 added this to the Meta milestone Feb 10, 2022
@kdy1 kdy1 changed the title [Meta] Minifier [Meta] Known issues for the minifier Feb 15, 2022
@kdy1 kdy1 added the A: meta label Feb 20, 2022
@kdy1 kdy1 removed this from the Meta milestone Feb 20, 2022
@Austaras
Copy link
Member

Austaras commented Mar 7, 2022

benchmark

@kdy1
Copy link
Member Author

kdy1 commented Mar 7, 2022

While looking at the benchmark, I found that

I found that

  • swc minifier uses all cpu core, but benchmark is invoked on 2 core machine

    • But I think actual minification is done on CI, so maybe parallel optimization is not a correct way?
    • I found this because I couldn’t reproduce the numbers on the chart. The minifier was way faster on my machine due to the parallelism.
  • Parallelization strategy of the optimizer is optimized for the webpack bundles.

    • This result in some perf bug for some inputs. I'll fix it

@Austaras
Copy link
Member

Seems like swc is exponentially slower than esbuild when input size increases.

@kdy1
Copy link
Member Author

kdy1 commented Mar 10, 2022

I think the analyzer is the problem.
Due to the restriction of rust, swc minifier does not track changes and instead performs full analysis before invoking the optimizer.

But there are much more rules than esbuild, which does not use visitors.

@Austaras
Copy link
Member

Misalignment between terser and swc default options

computed_props
conditionals
dead_code
directives
reduce_funcs
reduce_vars
switches

However simply enable them would likely to cause lots of bugs.

@Austaras
Copy link
Member

As google closure compiler ranks #1 in compress react, I diffed the output artifact with swc and found some abilities even terser doesn't have:

  1. gcc could remove common statement inside if -> switch -> catch, so following
function getComponentName(type) {
  if (typeof type === 'object') {
    switch (type.$$typeof) {
      case REACT_BLOCK_TYPE:
        return getComponentName(type._render);

      case REACT_LAZY_TYPE:
        {
          try {
            return getComponentName(init(payload));
          } catch (x) {
            return null;
          }
        }
    }
  }

  return null;
}

can be transformed into

function getComponentName(type) {
  if (typeof type === 'object') {
    switch (type.$$typeof) {
      case REACT_BLOCK_TYPE:
        return getComponentName(type._render);

      case REACT_LAZY_TYPE:
        {
          try {
            return getComponentName(init(payload));
          } catch (x) {
          }
        }
    }
  }

  return null;
}
  1. reduce var declaration if a previous var is only used in its intialization
function getWrappedName(outerType, innerType, wrapperName) {
  var functionName = innerType.displayName || innerType.name || '';
  return outerType.displayName || (functionName !== '' ? wrapperName + "(" + functionName + ")" : wrapperName);
}

compressed into

function getWrappedName(c, a, b) {
  a = a.displayName || a.name || "";
  return c.displayName || ("" !== a ? b + "(" + a + ")" : b);
}
  1. would inline functions called multiple times if it's simple enough
function describeFunctionComponentFrame(fn, source, ownerFn) {
    {
      return describeNativeComponentFrame(fn, false);
    }
  }

would be inlined

@kdy1
Copy link
Member Author

kdy1 commented Apr 28, 2022

Thank you so much!

@kdy1 kdy1 unpinned this issue Jul 14, 2022
@kdy1 kdy1 pinned this issue Aug 18, 2022
@kdy1 kdy1 unpinned this issue Aug 18, 2022
@kdy1 kdy1 changed the title [Meta] Known issues for the minifier [Meta] Minifier tasklist Aug 18, 2022
@kdy1 kdy1 pinned this issue Aug 18, 2022
@kdy1 kdy1 unpinned this issue Aug 18, 2022
@kdy1 kdy1 changed the title [Meta] Minifier tasklist [Meta] Known issues for the minifier Aug 19, 2022
@kdy1 kdy1 closed this as completed Aug 19, 2022
@swc-bot
Copy link
Collaborator

swc-bot commented Oct 16, 2022

This closed issue has been automatically locked because it had no new activity for a month. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@swc-project swc-project locked as resolved and limited conversation to collaborators Oct 16, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Development

No branches or pull requests

4 participants