Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Reduce transform-block-scoping loops output size#15746

Merged
nicolo-ribaudo merged 4 commits into
babel:mainfrom
liuxingbaoyu:improve-loop-scoping
Jul 18, 2023
Merged

Reduce transform-block-scoping loops output size#15746
nicolo-ribaudo merged 4 commits into
babel:mainfrom
liuxingbaoyu:improve-loop-scoping

Conversation

@liuxingbaoyu
Copy link
Copy Markdown
Member

@liuxingbaoyu liuxingbaoyu commented Jul 4, 2023

Q                       A
Fixed Issues? Fixes #1, Fixes #2
Patch: Bug Fix?
Major: Breaking Change?
Minor: New Feature?
Tests Added + Pass? Yes
Documentation PR Link
Any Dependency Changes?
License MIT

Improved performance and size of transform-block-scoping for loops.
There will be a small impact on the readability of the output when the comments are not preserved.

@liuxingbaoyu liuxingbaoyu added the PR: Output optimization 🔬 A type of pull request used for our changelog categories label Jul 4, 2023
@babel-bot
Copy link
Copy Markdown
Collaborator

babel-bot commented Jul 4, 2023

Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/54894/

@nicolo-ribaudo
Copy link
Copy Markdown
Member

Do we have a test with multiple labels?

outer: while (t) {
  inner: while (t2) {
    for (let i = 0; i < 3; i++) {
      later(() => i);
      if (test1) break outer;
      else if (test2) break inner;
      else if (test3) continue outer;
      else if (test4) continue inner;
      else if (test5) break;
      else if (test6) continue;
    }
  }
}

Comment thread packages/babel-plugin-transform-block-scoping/src/loop.ts Outdated
@liuxingbaoyu
Copy link
Copy Markdown
Member Author

Do we have a test with multiple labels?

There might be something similar in the execution test, I added it in the outputs.

Comment on lines +136 to +139
const declarations = varPath.get("declarations");
declarations[declarations.length - 1]
.get("init")
.unwrapFunctionEnvironment();
Copy link
Copy Markdown
Member

@nicolo-ribaudo nicolo-ribaudo Jul 5, 2023

Choose a reason for hiding this comment

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

We can simplify this back to what it was if we push the new variables to the end of varPath instead of unshifting them.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Do you think it would look better if we put the variable first?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It would look similar, but it's slightly simpler code on our end :)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Sounds very reasonable!


var gen5 = foo3();
expect(gen5.next().value).toBe("iteration");
expect(gen5.next({what: "three"}).done).toBe(true);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Can you duplicate this test to an executable test case (exec.js)?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Oops, this is actually a copy of packages\babel-core\test\fixtures\transformation\regenerator\destructuring\exec.js. I put it here for easy fix, maybe let me just add a note to clarify?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

In that case can you move the packages\babel-core\test\fixtures\transformation\regenerator\destructuring\exec.js to packages/babel-plugin-transform-block-scoping/test/fixtures/regression/complex? They should have been moved to plugin specific test folders.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

image
From what I understand it is ideally positioned?🤔

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The test cases here were added long before Babel plugins are separated from Babel core: https://github.com/babel/babel/blob/d4debc3c855dd4e797de3dbe8bcffe47d325dcb3/test/fixtures/transformation/regenerator/destructuring/exec.js

Since then most test cases have been moved to plugins. IMO the babel-core test cases should focus on core's behaviour, like providing plugin pass to plugin visitors, defining NodePath#hub or merging configs. In this case we already copy this test to the plugin's test folder (as input/output tests) and we generally do not separate exec.js with input.js. So I think the exec.js should be moved, too.

I can open a PR to move all such cases out of babel-core to avoid such confusing test fixtures structures.

@liuxingbaoyu
Copy link
Copy Markdown
Member Author

liuxingbaoyu commented Jul 7, 2023

It occurred to me, is it possible for us to support .get("a.-1")?

@nicolo-ribaudo
Copy link
Copy Markdown
Member

Well, in new node versions you can do .get("a").at(-1). We could just polyfill it.

@nicolo-ribaudo
Copy link
Copy Markdown
Member

Could you rebase, since we merged #15766?

@nicolo-ribaudo nicolo-ribaudo changed the title Improve transform-block-scoping performance and size for loops Reduce transform-block-scoping loops output size Jul 18, 2023
@nicolo-ribaudo nicolo-ribaudo merged commit b798a69 into babel:main Jul 18, 2023
@liuxingbaoyu
Copy link
Copy Markdown
Member Author

@nicolo-ribaudo Thank you! Sorry I forgot it.

@github-actions github-actions Bot added the outdated A closed issue/PR that is archived due to age. Recommended to make a new issue label Oct 19, 2023
@github-actions github-actions Bot locked as resolved and limited conversation to collaborators Oct 19, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

outdated A closed issue/PR that is archived due to age. Recommended to make a new issue PR: Output optimization 🔬 A type of pull request used for our changelog categories

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants