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

Declare template objects inline #12588

Merged

Conversation

nicolo-ribaudo
Copy link
Member

@nicolo-ribaudo nicolo-ribaudo commented Jan 5, 2021

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

Template literal objects are cached globally per source location, so they cannot be evaluated twice.
We were solving this by using a singleton function defined in the top-level scope, but we can remove the functions and use cachedObject || (cachedObject = ...) inline, where cachedObject is a variable in the top-level scope.

This PR makes the output smaller when using terser, since the function and return keyword cannot be minified away with function declarations.


Input code:

foo`ab`;
foo`a${x}b`;
Output code (`main`):
function _templateObject2() {
  var data = babelHelpers.taggedTemplateLiteral(["a", "b"]);

  _templateObject2 = function _templateObject2() {
    return data;
  };

  return data;
}

function _templateObject() {
  var data = babelHelpers.taggedTemplateLiteral(["ab"]);

  _templateObject = function _templateObject() {
    return data;
  };

  return data;
}

function _taggedTemplateLiteral(strings, raw) { if (!raw) { raw = strings.slice(0); } return Object.freeze(Object.defineProperties(strings, { raw: { value: Object.freeze(raw) } })); }

foo(_templateObject());
foo(_templateObject2(), x);
Output code (this PR):
var _templateObject, _templateObject2;

foo(_templateObject || (_templateObject = babelHelpers.taggedTemplateLiteral(["ab"])));
foo(_templateObject2 || (_templateObject2 = babelHelpers.taggedTemplateLiteral(["a", "b"])), x);
Output code (`main` + terser): 211 bytes
function e(){var r=babelHelpers.taggedTemplateLiteral(["a","b"]);return e=function(){return r},r}function r(){var e=babelHelpers.taggedTemplateLiteral(["ab"]);return r=function(){return e},e}foo(r()),foo(e(),x);
Output code (this PR + terser): 123 bytes
var e,a;foo(e||(e=babelHelpers.taggedTemplateLiteral(["ab"]))),foo(a||(a=babelHelpers.taggedTemplateLiteral(["a","b"])),x);

@nicolo-ribaudo nicolo-ribaudo added the PR: Output optimization 🔬 A type of pull request used for our changelog categories label Jan 5, 2021
@babel-bot
Copy link
Collaborator

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

@codesandbox-ci
Copy link

codesandbox-ci bot commented Jan 5, 2021

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit 0cad61b:

Sandbox Source
babel-repl-custom-plugin Configuration
babel-plugin-multi-config Configuration

@nicolo-ribaudo
Copy link
Member Author

nicolo-ribaudo commented Jan 5, 2021

I'm running the test262-pr job just in case, to be 100% sure that this doesn't break anything.

@nicolo-ribaudo
Copy link
Member Author

There are some failing tests, but they are all caused by the test runner and not by Babel itself.

The test runner handles eval as if it was a different file, so Babel injects two temp variables with the same name: https://github.com/tc39/test262/blob/main/test/language/expressions/tagged-template/cache-differing-expressions-eval.js

@nicolo-ribaudo nicolo-ribaudo merged commit fb12afc into babel:main Jan 5, 2021
@nicolo-ribaudo nicolo-ribaudo deleted the tagged-template-literals-inline branch January 5, 2021 22:25
@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 Apr 7, 2021
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 7, 2021
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.

None yet

4 participants