Skip to content

Commit fdde3da

Browse files
authoredAug 29, 2023
fix: improve addChild and path performance (#1891)
Improve the performance of the `constructs` library by removing several branches in the library's hot paths and switching to some more performant operations. On my laptop, the benchmark I used (shared in a gist [here](https://gist.github.com/Chriscbr/fa69e5f7f35d5aa4dc312dd7025952b1)) takes about 5.0-5.3 seconds with the old version of constructs, and about 1.1-1.2 seconds with the new version of constructs. Misc: - Removed an unused file in the `test` folder. - Added some files to `npmignore` to reduce the package's npm footprint.
1 parent f47fb2f commit fdde3da

File tree

4 files changed

+10
-81
lines changed

4 files changed

+10
-81
lines changed
 

‎.npmignore

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎.projenrc.ts

+1
Original file line numberDiff line numberDiff line change
@@ -81,5 +81,6 @@ project.buildWorkflow?.addPostBuildJobCommands(
8181
{ checkoutRepo: true },
8282
);
8383

84+
project.npmignore?.exclude('/scripts/', '.projenrc.ts');
8485

8586
project.synth();

‎src/construct.ts

+7-10
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,12 @@ export class Node {
7777
* Components are separated by '/'.
7878
*/
7979
public get path(): string {
80-
const components = this.scopes.filter(c => c.node.id).map(c => c.node.id);
80+
const components = [];
81+
for (const scope of this.scopes) {
82+
if (scope.node.id) {
83+
components.push(scope.node.id);
84+
}
85+
}
8186
return components.join(Node.PATH_SEP);
8287
}
8388

@@ -417,21 +422,13 @@ export class Node {
417422
throw new Error(`Cannot add children to "${this.path}" during synthesis`);
418423
}
419424

420-
if (childName in this._children) {
425+
if (this._children[childName]) {
421426
const name = this.id ?? '';
422427
const typeName = this.host.constructor.name;
423428
throw new Error(`There is already a Construct with name '${childName}' in ${typeName}${name.length > 0 ? ' [' + name + ']' : ''}`);
424429
}
425430

426-
if (!childName && this.id) {
427-
throw new Error(`cannot add a nameless construct to the named scope: ${this.path}`);
428-
}
429-
430431
this._children[childName] = child;
431-
432-
if (Object.keys(this._children).length > 1 && Object.keys(this._children).filter(x => !x).length > 0) {
433-
throw new Error('only a single construct is allowed in a scope if it has an empty name');
434-
}
435432
}
436433
}
437434

‎test/evaluate-cfn.ts

-71
This file was deleted.

0 commit comments

Comments
 (0)
Failed to load comments.