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

Mangler performance - some improvement #134

Merged
merged 2 commits into from
Aug 31, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 42 additions & 27 deletions packages/babel-plugin-minify-mangle-names/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ module.exports = ({ types: t }) => {
collect() {
const mangler = this;

this.program.traverse({
const collectVisitor = {
// capture direct evals
CallExpression(path) {
const callee = path.get("callee");
Expand All @@ -50,24 +50,28 @@ module.exports = ({ types: t }) => {
) {
mangler.markUnsafeScopes(path.scope);
}
},
}
};

if (this.charset.shouldConsider) {
// charset considerations
Identifier(path) {
collectVisitor.Identifier = function Identifier(path) {
const { node } = path;

if ((path.parentPath.isMemberExpression({ property: node })) ||
(path.parentPath.isObjectProperty({ key: node }))
) {
mangler.charset.consider(node.name);
}
},
};

// charset considerations
Literal({ node }) {
collectVisitor.Literal = function Literal({ node }) {
mangler.charset.consider(String(node.value));
}
});
};
}

this.program.traverse(collectVisitor);
}

mangle() {
Expand Down Expand Up @@ -103,26 +107,37 @@ module.exports = ({ types: t }) => {
// i = 0;
// }

Object
.keys(scope.getAllBindings())
.filter((b) => {
const binding = scope.getBinding(b);

return scope.hasOwnBinding(b)
&& !binding.path.isLabeledStatement()
&& !mangler.isBlacklist(b, mangler.blacklist)
&& (mangler.keepFnames ? !isFunction(binding.path) : true);
})
.map((b) => {
let next;
do {
next = getNext();
} while (!t.isValidIdentifier(next) || scope.hasBinding(next) || scope.hasGlobal(next) || scope.hasReference(next));
// TODO:
// re-enable this
// resetNext();
mangler.rename(scope, b, next);
});
const bindings = scope.getAllBindings();
const names = Object.keys(bindings);

for (let i = 0; i < names.length; i++) {
const oldName = names[i];
const binding = bindings[oldName];

if (
!scope.hasOwnBinding(oldName)
|| binding.path.isLabeledStatement()
|| mangler.isBlacklist(oldName)
|| (mangler.keepFnames ? isFunction(binding.path) : false)
) {
continue;
}

let next;
do {
next = getNext();
} while (
!t.isValidIdentifier(next)
|| hop.call(bindings, next)
|| scope.hasGlobal(next)
Copy link
Member

Choose a reason for hiding this comment

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

Idea: collect global references upfront and check manually instead of calling hasGlobal for each

Copy link
Member Author

Choose a reason for hiding this comment

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

each scope maintains its global references when the scope is created only (I think), and we visit scopes only once - and we transform in place. So my gut feel is that it's not possible to do better that that.

Copy link
Member

Choose a reason for hiding this comment

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

Wait, all scopes end up in the global... why do they need to store references?

Copy link
Member Author

Choose a reason for hiding this comment

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

I'm not sure. scope is created (globals calculated from bindings) -> visitor applied is what I thought. I'll check though.

Copy link
Member Author

@boopathi boopathi Aug 31, 2016

Choose a reason for hiding this comment

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

Also, from the timing values, the next-identifier is just 39ms (0.011 ms per call) - this includes -

let next;
            do {
              next = getNext();
            } while (
              !t.isValidIdentifier(next)
              || hop.call(bindings, next)
              || scope.hasGlobal(next)
              || scope.hasReference(next)
            );

So this is not something we need to worry about ...

|| scope.hasReference(next)
);

// TODO:
// re-enable this - check above
// resetNext();
mangler.rename(scope, oldName, next);
}
}
});

Expand Down