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

Optimize various switch statements #1044

Merged
merged 11 commits into from Aug 24, 2021

Commits on Aug 12, 2021

  1. Optimize various switch statements

    1. This implements full case folding for equivalent branches
    2. This implements default collapsing when all cases are "default"
    
    The default collapsing is hard to describe, but easy to demonstrate:
    
    ```js
    switch (expression) {
      case 1:
      case 2:
      case 3:
      default:
        doSomething();
    }
    ```
    
    Because all of the case `expression`s are side-effect free, we can consider just the case bodies. Because all cases fall through to the last branch, we
    can check to see if this branch is always guaranteed to run due to a `default` branch. If everything is good, we can eliminate the switch statement entirely.
    
    I've hit this a few times when programming TypeScript. I tend to write exhaustive type checking, and a times that leads to code like:
    
    ```js
    switch (type) {
      case "Foo":
      case "Bar":
        if (process.env.NODE_ENV !== 'production') {
          throw new Error('invariant failed!');
        }
      default:
        doSomething(type);
    }
    ```
    
    In production builds, I I strip the invariant checks out for file size (my tests should have caught if it were ever possible to hit those cases). And this used to leave me with large useless switch statements:
    
    ```js
    switch (type) {
      case "Foo":
      case "Bar":
      default:
        doSomething(type);
    }
    ```
    
    This creates the "all cases are default" behavior, which I'd really like to have minimized.
    jridgewell committed Aug 12, 2021
    Copy the full SHA
    1370b07 View commit details
    Browse the repository at this point in the history
  2. Copy the full SHA
    34c1858 View commit details
    Browse the repository at this point in the history

Commits on Aug 13, 2021

  1. Copy the full SHA
    ae63f4f View commit details
    Browse the repository at this point in the history
  2. Copy the full SHA
    3321339 View commit details
    Browse the repository at this point in the history
  3. tmp

    jridgewell committed Aug 13, 2021
    Copy the full SHA
    14228ed View commit details
    Browse the repository at this point in the history
  4. Copy the full SHA
    dd99729 View commit details
    Browse the repository at this point in the history
  5. Copy the full SHA
    9ffb585 View commit details
    Browse the repository at this point in the history
  6. Copy the full SHA
    a9b8a8a View commit details
    Browse the repository at this point in the history

Commits on Aug 23, 2021

  1. add some tests

    fabiosantoscode authored and jridgewell committed Aug 23, 2021
    Copy the full SHA
    e9f87c9 View commit details
    Browse the repository at this point in the history
  2. Collapse inert bodies

    jridgewell committed Aug 23, 2021
    Copy the full SHA
    af11298 View commit details
    Browse the repository at this point in the history
  3. Remove useless branch check

    The branch can't be the default, because it would have already been handled by the switch removal code.
    jridgewell committed Aug 23, 2021
    Copy the full SHA
    bfa0499 View commit details
    Browse the repository at this point in the history