Skip to content

Replace O(N^2) Set-merge traversal with O(N) post-order DFS for list form#213

Merged
XhmikosR merged 2 commits into
mainfrom
xmr/perf2
May 24, 2026
Merged

Replace O(N^2) Set-merge traversal with O(N) post-order DFS for list form#213
XhmikosR merged 2 commits into
mainfrom
xmr/perf2

Conversation

@XhmikosR
Copy link
Copy Markdown
Member

The previous list-form implementation built each node's result by iterating over every child's full transitive Set and re-adding items into the parent Set. For K files sharing a common package with M transitive deps this cost O(K*M) Set.add calls, and the spread push(...subTree) over a large Set risked a RangeError on very deep trees.

The new traverseList / traverseListHelper pair does a single post-order DFS with one seen Set for cycle/duplicate detection, visiting each file exactly once and pushing it to the result array after its children - O(N) in unique files with identical output order.

… list form

The previous list-form implementation built each node's result by iterating
over every child's full transitive Set and re-adding items into the parent Set.
For K files sharing a common package with M transitive deps this cost O(K*M)
Set.add calls, and the spread push(...subTree) over a large Set risked a
RangeError on very deep trees.

The new traverseList / traverseListHelper pair does a single post-order DFS
with one seen Set for cycle/duplicate detection, visiting each file exactly
once and pushing it to the result array after its children - O(N) in unique
files with identical output order.
@XhmikosR XhmikosR marked this pull request as ready for review May 24, 2026 08:44
@XhmikosR XhmikosR merged commit 8fd9696 into main May 24, 2026
9 checks passed
@XhmikosR XhmikosR deleted the xmr/perf2 branch May 24, 2026 08:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant