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 assignment wrapper #326

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
47 changes: 40 additions & 7 deletions lib/assignment-visitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,26 +80,59 @@ function assignmentHelper(visitor, path, childName) {
const assignedNames = utils.getNamesFromPattern(child);
const nameCount = assignedNames.length;

// Wrap assignments to exported identifiers with `module.runSetters`.
let scope = null;
function inModuleScope(name) {
if (scope === null) {
scope = path.getScope();
}

return !scope || scope.find_owner(name).parent === null;
}

let modifiedExports = [];

// Identify which exports if any are modified by the assignment.
for (let i = 0; i < nameCount; ++i) {
if (visitor.exportedLocalNames[assignedNames[i]] === true) {
wrap(visitor, path);
break;
let name = assignedNames[i];
if (
visitor.exportedLocalNames[name] &&
inModuleScope(name)
) {
modifiedExports.push(...visitor.exportedLocalNames[name]);
}
}

// Wrap assignments to exported identifiers with `module.runSetters`.
if (modifiedExports.length > 0) {
wrap(visitor, path, modifiedExports);
}
}

function wrap(visitor, path) {
function wrap(visitor, path, names) {
const value = path.getValue();

if (visitor.magicString !== null) {
let end = ')';
if (names) {
end = `,[${names.map(n => `"${n}"`).join(',')}])`;
}

visitor.magicString.prependRight(
value.start,
visitor.moduleAlias + ".runSetters("
).appendLeft(value.end, ")");
).appendLeft(value.end, end);
}

if (visitor.modifyAST) {
let args = [value];
if (names) {
let array = {
type: "ArrayExpression",
elements: names.map(n => ({ type: "StringLiteral", value: n }))
};
args.push(array);
}

path.replace({
type: "CallExpression",
callee: {
Expand All @@ -113,7 +146,7 @@ function wrap(visitor, path) {
name: "runSetters"
}
},
arguments: [value]
arguments: args
});
}
}
Expand Down
17 changes: 17 additions & 0 deletions lib/fast-path.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

const assert = require("assert");
const utils = require("./utils.js");
const periscopic = require('periscopic');
const brandKey = "reify-fast-path";
const brand = typeof Symbol === "function"
? Symbol.for(brandKey)
Expand All @@ -15,6 +16,7 @@ class FastPath {
assert.notStrictEqual(ast, null);
assert.strictEqual(typeof ast, "object");
this.stack = [ast];
this._scope = null;
}

static isInstance(value) {
Expand Down Expand Up @@ -102,6 +104,21 @@ class FastPath {
return getStackAt(this, 1);
}

getScope() {
if (!this._scope) {
this._scope = periscopic.analyze(this.stack[0]);
}

let node;
let pos = 0;
while (node = getNodeAt(this, pos++)) {
let scope = this._scope.map.get(node);
if (scope) {
return scope;
}
}
}

replace(newValue) {
const s = this.stack;
const len = s.length;
Expand Down
10 changes: 4 additions & 6 deletions lib/import-export-visitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -457,12 +457,10 @@ function addExportedLocalNames(visitor, specifierMap) {
const localCount = locals.length;

for (let j = 0; j < localCount; ++j) {
// It's tempting to record the exported name as the value here,
// instead of true, but there can be more than one exported name
// per local variable, and we don't actually use the exported
// name(s) in the assignmentVisitor, so it's not worth the added
// complexity of tracking unused information.
exportedLocalNames[locals[j]] = true;
if (exportedLocalNames[locals[j]] === undefined) {
exportedLocalNames[locals[j]] = [];
}
exportedLocalNames[locals[j]].push(exported);
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions lib/runtime/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,8 @@ function moduleExportAs(name) {
// the module system is about to return module.exports from require. This
// might happen more than once per module, in case of dependency cycles,
// so we want Module.prototype.runSetters to run each time.
function runSetters(valueToPassThrough) {
Entry.getOrCreate(this.id, this).runSetters();
function runSetters(valueToPassThrough, names) {
Entry.getOrCreate(this.id, this).runSetters(names, true);

// Assignments to exported local variables get wrapped with calls to
// module.runSetters, so module.runSetters returns the
Expand Down
Loading