GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — perf(javascript): walk patterns, labels, updates and dead branches by id · webpack/webpack@dfd3d0b

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • lib
    • dependencies/context
      • ContextDependencyHelpers.js
    • javascript
      • ConstPlugin.js
      • JavascriptParser.js
    • optimize
      • ConstExportsPlugin.js
  • types.d.ts
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎lib/dependencies/context/ContextDependencyHelpers.js‎

Copy file name to clipboardExpand all lines: lib/dependencies/context/ContextDependencyHelpers.js
+11-4Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -207,9 +207,10 @@ module.exports.create = (
207207
});
208208
} else {
209209
// Expression
210+
// the node id walks it in place, without making its object
211+
const node = part.expressionNode;
210212
parser.walkExpression(
211-
/** @type {Expression} */
212-
(part.expression)
213+
node !== 0 ? node : /** @type {Expression} */ (part.expression)
213214
);
214215
}
215216
}
@@ -283,7 +284,10 @@ module.exports.create = (
283284

284285
if (parser && param.wrappedInnerExpressions) {
285286
for (const part of param.wrappedInnerExpressions) {
286-
if (part.expression) {
287+
const node = part.expressionNode;
288+
if (node !== 0) {
289+
parser.walkExpression(node);
290+
} else if (part.expression) {
287291
parser.walkExpression(
288292
/** @type {Expression} */
289293
(part.expression)
@@ -311,7 +315,10 @@ module.exports.create = (
311315
options.exprContextCritical &&
312316
"the request of a dependency is an expression";
313317

314-
parser.walkExpression(/** @type {Expression} */ (param.expression));
318+
const node = param.expressionNode;
319+
parser.walkExpression(
320+
node !== 0 ? node : /** @type {Expression} */ (param.expression)
321+
);
315322

316323
return dep;
317324
};
Collapse file

‎lib/javascript/ConstPlugin.js‎

Copy file name to clipboardExpand all lines: lib/javascript/ConstPlugin.js
+172-8Lines changed: 172 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,174 @@ const collectDeclaration = (declarations, pattern) => {
6868
}
6969
};
7070

71+
/**
72+
* `collectDeclaration` for a pattern a node reader reads, by id.
73+
* @param {JavascriptNodeReader} path the reader
74+
* @param {Declarations} declarations set of declarations
75+
* @param {number} pattern the pattern to collect declarations from
76+
*/
77+
const collectDeclarationByPath = (path, declarations, pattern) => {
78+
const stack = [pattern];
79+
while (stack.length > 0) {
80+
const node = /** @type {number} */ (stack.pop());
81+
switch (path.type(node)) {
82+
case NodeType.Identifier:
83+
declarations.add(path.name(node));
84+
break;
85+
case NodeType.ArrayPattern: {
86+
const count = path.fieldCount(Field.elements, node);
87+
for (let i = 0; i < count; i++) {
88+
const element = path.field(i, Field.elements, node);
89+
if (element !== 0) stack.push(element);
90+
}
91+
break;
92+
}
93+
case NodeType.AssignmentPattern:
94+
stack.push(path.field(0, Field.left, node));
95+
break;
96+
case NodeType.ObjectPattern: {
97+
const count = path.fieldCount(Field.properties, node);
98+
for (let i = 0; i < count; i++) {
99+
const property = path.field(i, Field.properties, node);
100+
stack.push(
101+
path.type(property) === NodeType.RestElement
102+
? property
103+
: path.field(0, Field.value, property)
104+
);
105+
}
106+
break;
107+
}
108+
case NodeType.RestElement:
109+
stack.push(path.field(0, Field.argument, node));
110+
break;
111+
}
112+
}
113+
};
114+
115+
/**
116+
* `getHoistedDeclarations` for a statement a node reader reads, by id.
117+
* @param {JavascriptNodeReader} path the reader
118+
* @param {number} branch branch to get hoisted declarations from
119+
* @param {boolean} includeFunctionDeclarations whether to include function declarations
120+
* @returns {string[]} hoisted declarations
121+
*/
122+
const getHoistedDeclarationsByPath = (
123+
path,
124+
branch,
125+
includeFunctionDeclarations
126+
) => {
127+
/** @type {Declarations} */
128+
const declarations = new Set();
129+
const stack = [branch];
130+
while (stack.length > 0) {
131+
const node = /** @type {number} */ (stack.pop());
132+
if (node === 0) continue;
133+
switch (path.type(node)) {
134+
case NodeType.BlockStatement: {
135+
const count = path.fieldCount(Field.body, node);
136+
for (let i = 0; i < count; i++) {
137+
stack.push(path.field(i, Field.body, node));
138+
}
139+
break;
140+
}
141+
case NodeType.IfStatement:
142+
stack.push(path.field(0, Field.consequent, node));
143+
stack.push(path.field(0, Field.alternate, node));
144+
break;
145+
case NodeType.ForStatement:
146+
stack.push(path.field(0, Field.init, node));
147+
stack.push(path.field(0, Field.body, node));
148+
break;
149+
case NodeType.ForInStatement:
150+
case NodeType.ForOfStatement:
151+
stack.push(path.field(0, Field.left, node));
152+
stack.push(path.field(0, Field.body, node));
153+
break;
154+
case NodeType.DoWhileStatement:
155+
case NodeType.WhileStatement:
156+
case NodeType.LabeledStatement:
157+
stack.push(path.field(0, Field.body, node));
158+
break;
159+
case NodeType.SwitchStatement: {
160+
const count = path.fieldCount(Field.cases, node);
161+
for (let i = 0; i < count; i++) {
162+
const switchCase = path.field(i, Field.cases, node);
163+
const consequentCount = path.fieldCount(Field.consequent, switchCase);
164+
for (let j = 0; j < consequentCount; j++) {
165+
stack.push(path.field(j, Field.consequent, switchCase));
166+
}
167+
}
168+
break;
169+
}
170+
case NodeType.TryStatement: {
171+
stack.push(path.field(0, Field.block, node));
172+
const handler = path.field(0, Field.handler, node);
173+
if (handler !== 0) stack.push(path.field(0, Field.body, handler));
174+
stack.push(path.field(0, Field.finalizer, node));
175+
break;
176+
}
177+
case NodeType.FunctionDeclaration:
178+
if (includeFunctionDeclarations) {
179+
collectDeclarationByPath(
180+
path,
181+
declarations,
182+
path.field(0, Field.id, node)
183+
);
184+
}
185+
break;
186+
case NodeType.VariableDeclaration:
187+
if (path.textOf(Part.kind, node) === "var") {
188+
const count = path.fieldCount(Field.declarations, node);
189+
for (let i = 0; i < count; i++) {
190+
collectDeclarationByPath(
191+
path,
192+
declarations,
193+
path.field(0, Field.id, path.field(i, Field.declarations, node))
194+
);
195+
}
196+
}
197+
break;
198+
}
199+
}
200+
return [...declarations];
201+
};
202+
203+
/**
204+
* `ConstPlugin.eliminateUnusedStatement` for a statement a node reader reads, by id.
205+
* @param {JavascriptParser} parser the parser
206+
* @param {JavascriptNodeReader} path the reader
207+
* @param {number} statement the statement to remove
208+
* @param {boolean} alwaysInBlock whether to always generate curly brackets
209+
* @returns {void}
210+
*/
211+
const eliminateUnusedStatementByPath = (
212+
parser,
213+
path,
214+
statement,
215+
alwaysInBlock
216+
) => {
217+
// as in `eliminateUnusedStatement`, strict mode hoists `var` but not `function`
218+
const declarations = getHoistedDeclarationsByPath(
219+
path,
220+
statement,
221+
!parser.scope.isStrict
222+
);
223+
const inBlock =
224+
alwaysInBlock || path.type(statement) === NodeType.BlockStatement;
225+
226+
let replacement = inBlock ? "{" : "";
227+
replacement +=
228+
declarations.length > 0 ? ` var ${declarations.join(", ")}; ` : "";
229+
replacement += inBlock ? "}" : "";
230+
231+
const dep = new ConstDependency(
232+
`// removed by dead control flow\n${replacement}`,
233+
path.range(statement)
234+
);
235+
dep.loc = parser.getLocation(parser.nodeOfPath(path, statement));
236+
parser.state.module.addPresentationalDependency(dep);
237+
};
238+
71239
/**
72240
* Gets hoisted declarations.
73241
* @param {Statement} branch branch to get hoisted declarations from
@@ -299,9 +467,10 @@ class ConstPlugin {
299467
statement
300468
);
301469
if (branchToRemove !== 0) {
302-
this.eliminateUnusedStatement(
470+
eliminateUnusedStatementByPath(
303471
parser,
304-
/** @type {Statement} */ (path.object(branchToRemove)),
472+
path,
473+
branchToRemove,
305474
true
306475
);
307476
}
@@ -319,12 +488,7 @@ class ConstPlugin {
319488
) {
320489
return;
321490
}
322-
// dead code after a jump is rare, so it reads the statement's object
323-
this.eliminateUnusedStatement(
324-
parser,
325-
/** @type {Statement} */ (path.object(statement)),
326-
false
327-
);
491+
eliminateUnusedStatementByPath(parser, path, statement, false);
328492
return true;
329493
}
330494
);

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.