Skip to content

Commit

Permalink
Reduce performance overhead
Browse files Browse the repository at this point in the history
  • Loading branch information
TrickyPi committed May 7, 2024
1 parent 0dbc57a commit e3966ab
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 26 deletions.
28 changes: 22 additions & 6 deletions src/ast/nodes/Identifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ const tdzVariableKinds = new Set(['class', 'const', 'let', 'var', 'using', 'awai
export default class Identifier extends NodeBase implements PatternNode {
declare name: string;
declare type: NodeType.tIdentifier;
declare includedPaths: ObjectPath[] | null;

variable: Variable | null = null;

private get isTDZAccess(): boolean | null {
Expand Down Expand Up @@ -193,19 +195,33 @@ export default class Identifier extends NodeBase implements PatternNode {
}
}

private hasOrAddIncludedPaths(path: ObjectPath) {
if (!this.includedPaths) {
this.includedPaths = [];
}
for (const includedPath of this.includedPaths) {
if (
path.length === includedPath.length &&
path.every((key, index) => key === includedPath[index])
) {
return true;
}
}
this.includedPaths.push(path);
return false;
}

includePath(path?: ObjectPath): void {
if (!this.deoptimized) this.applyDeoptimizations();
if (this.included) {
if (path?.length && !this.includedPaths.has(path[0])) {
this.includedPaths.add(path[0]);
this.variable?.includePath(path);
}
} else {
if (!this.included) {
this.included = true;
if (this.variable !== null) {
this.scope.context.includeVariableInModule(this.variable);
}
}
if (path?.length && !this.hasOrAddIncludedPaths(path)) {
this.variable?.includePath(path);
}
}

includeCallArguments(
Expand Down
11 changes: 5 additions & 6 deletions src/ast/nodes/ObjectPattern.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { EMPTY_PATH, type ObjectPath, UNKNOWN_PATH } from '../utils/PathTracker'
import type LocalVariable from '../variables/LocalVariable';
import type Variable from '../variables/Variable';
import * as NodeType from './NodeType';
import Property from './Property';
import type Property from './Property';
import RestElement from './RestElement';
import VariableDeclarator from './VariableDeclarator';
import type { ExpressionEntity, InclusionOptions } from './shared/Expression';
Expand Down Expand Up @@ -57,13 +57,12 @@ export default class ObjectPattern extends NodeBase implements PatternNode {
): void {
super.includePath(path, context, includeChildrenRecursively, options);
if (this.parent instanceof VariableDeclarator) {
for (const p of this.properties) {
if (p instanceof Property) {
if (this.properties[this.properties.length - 1] instanceof RestElement) {
this.parent.init?.includePath(UNKNOWN_PATH, context, includeChildrenRecursively, options);
} else {
for (const p of this.properties as Property[]) {
this.parent.init?.includePath([p.key.name], context, includeChildrenRecursively, options);
}
if (p instanceof RestElement) {
this.parent.init?.includePath(UNKNOWN_PATH, context, includeChildrenRecursively, options);
}
}
}
}
Expand Down
9 changes: 1 addition & 8 deletions src/ast/nodes/shared/Expression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,7 @@ import type { DeoptimizableEntity } from '../../DeoptimizableEntity';
import type { WritableEntity } from '../../Entity';
import type { HasEffectsContext, InclusionContext } from '../../ExecutionContext';
import type { NodeInteraction, NodeInteractionCalled } from '../../NodeInteractions';
import type {
ObjectPath,
ObjectPathKey,
PathTracker,
SymbolToStringTag
} from '../../utils/PathTracker';
import type { ObjectPath, PathTracker, SymbolToStringTag } from '../../utils/PathTracker';
import { EMPTY_PATH, UNKNOWN_PATH } from '../../utils/PathTracker';
import type { LiteralValue } from '../Literal';
import type SpreadElement from '../SpreadElement';
Expand All @@ -34,8 +29,6 @@ export interface InclusionOptions {
export class ExpressionEntity implements WritableEntity {
protected flags: number = 0;

includedPaths = new Set<ObjectPathKey>();

get included(): boolean {
return isFlagSet(this.flags, Flag.included);
}
Expand Down
10 changes: 4 additions & 6 deletions src/ast/variables/LocalVariable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,12 +183,7 @@ export default class LocalVariable extends Variable {
}

includePath(path?: ObjectPath): void {
if (this.included) {
if (path?.length && !this.includedPaths.has(path[0])) {
this.includedPaths.add(path[0]);
this.init.includePath(path, createInclusionContext(), false);
}
} else {
if (!this.included) {
super.includePath();
for (const declaration of this.declarations) {
// If node is a default export, it can save a tree-shaking run to include the full declaration now
Expand All @@ -204,6 +199,9 @@ export default class LocalVariable extends Variable {
}
}
}
if (path?.length) {
this.init.includePath(path, createInclusionContext(), false);
}
}

includeCallArguments(
Expand Down

0 comments on commit e3966ab

Please sign in to comment.