-
Notifications
You must be signed in to change notification settings - Fork 4.3k
fix: large amounts of stacks slows down synthesis #34480
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
base: main
Are you sure you want to change the base?
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| import { IConstruct } from 'constructs'; | ||
|
|
||
| /** | ||
| * Breadth-first iterator over the construct tree | ||
| * | ||
| * Replaces `node.findAll()` which both uses recursive function | ||
| * calls and accumulates into an array, both of which are much slower | ||
| * than this solution. | ||
| */ | ||
| export function* iterateDfsPreorder(root: IConstruct) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [q]: preorder would be visiting left children before right one, which is not what this method does. Was that intended?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Preorder means parent before children, which this method does do. |
||
| // Use a specialized queue data structure. Using `Array.shift()` | ||
| // has a huge performance penalty (difference on the order of | ||
| // ~50ms vs ~1s to iterate a large construct tree) | ||
| const queue: IConstruct[] = [root]; | ||
|
||
|
|
||
| let next = queue.pop(); | ||
| while (next) { | ||
| // Get at the construct internals to get at the children faster | ||
| // const children: Record<string, IConstruct> = (next.construct.node as any)._children; | ||
| for (const child of next.node.children) { | ||
| queue.push(child); | ||
| } | ||
| yield next; | ||
|
|
||
| next = queue.pop(); | ||
| } | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You mean Depth-first?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah yes, copy/paste-o. Thanks.