Skip to content

Commit

Permalink
Optimize tree-shaking for ParameterVariable
Browse files Browse the repository at this point in the history
  • Loading branch information
TrickyPi committed May 8, 2024
1 parent 90b1383 commit 22baef2
Show file tree
Hide file tree
Showing 11 changed files with 52 additions and 13 deletions.
11 changes: 9 additions & 2 deletions src/ast/variables/ParameterVariable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ interface DeoptimizationInteraction {
path: ObjectPath;
}

const MAX_TRACKED_ARGUMENTS = 7;
const MAX_TRACKED_INTERACTIONS = 20;
const NO_INTERACTIONS = EMPTY_ARRAY as unknown as DeoptimizationInteraction[];
const UNKNOWN_DEOPTIMIZED_FIELD = new Set<ObjectPathKey>([UnknownKey]);
Expand Down Expand Up @@ -91,6 +92,7 @@ export default class ParameterVariable extends LocalVariable {
this.markReassigned();
}

private trackedArguments = new Set<ExpressionEntity>();
private knownValue: ExpressionEntity | null = null;
private knownValueLiteral: LiteralValueOrUnknown = UnknownValue;
/**
Expand All @@ -100,6 +102,9 @@ export default class ParameterVariable extends LocalVariable {
* @param argument The argument of the function call
*/
updateKnownValue(argument: ExpressionEntity) {
if (this.trackedArguments.size <= MAX_TRACKED_ARGUMENTS) {
this.trackedArguments.add(argument);
}
if (this.isReassigned) {
return;
}
Expand Down Expand Up @@ -182,8 +187,10 @@ export default class ParameterVariable extends LocalVariable {

includePath(path?: ObjectPath): void {
super.includePath(path);
if (this.knownValue && path) {
this.knownValue.includePath(path, createInclusionContext(), false);
if (path) {
for (const trackedArgument of this.trackedArguments) {
trackedArgument.includePath(path, createInclusionContext(), false);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
define(['exports'], (function (exports) { 'use strict';

const foo = () => {};
const bar = () => {};

exports.bar = bar;
exports.foo = foo;

}));
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@ define(['require'], (function (require) { 'use strict';

(async () => {
const module = await new Promise(function (resolve, reject) { require(['./generated-module'], resolve, reject); });
readBar(module);
function readBar(module1) {
readFoo({ foo: () => {} });
readFoo(module);
function readFoo(module1) {
module1.foo();
}
function readBar(module2) {
module2.bar();
}
readBar(module);
})();

}));
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict';

const foo = () => {};
const bar = () => {};

exports.bar = bar;
exports.foo = foo;
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,13 @@

(async () => {
const module = await Promise.resolve().then(function () { return require('./generated-module.js'); });
readBar(module);
function readBar(module1) {
readFoo({ foo: () => {} });
readFoo(module);
function readFoo(module1) {
module1.foo();
}
function readBar(module2) {
module2.bar();
}
readBar(module);
})();
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const foo = () => {};
const bar = () => {};

export { foo };
export { bar, foo };
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
(async () => {
const module = await import('./generated-module.js');
readBar(module);
function readBar(module1) {
readFoo({ foo: () => {} });
readFoo(module);
function readFoo(module1) {
module1.foo();
}
function readBar(module2) {
module2.bar();
}
readBar(module);
})();
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ System.register([], (function (exports) {
execute: (function () {

const foo = exports("foo", () => {});
const bar = exports("bar", () => {});

})
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,15 @@ System.register([], (function (exports, module) {

(async () => {
const module$1 = await module.import('./generated-module.js');
readBar(module$1);
function readBar(module1) {
readFoo({ foo: () => {} });
readFoo(module$1);
function readFoo(module1) {
module1.foo();
}
function readBar(module2) {
module2.bar();
}
readBar(module$1);
})();

})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
(async () => {
const module = await import('./module');
readBar(module);
function readBar(module1) {
readFoo({ foo: () => {} });
readFoo(module);
function readFoo(module1) {
module1.foo();
}
function readBar(module2) {
module2.bar();
}
readBar(module);
})();
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export const foo = () => {};
export const bar = () => {};
export const baz = () => {};

0 comments on commit 22baef2

Please sign in to comment.