-
Notifications
You must be signed in to change notification settings - Fork 1
⚡ Bolt: 최적화: NetworkGraph의 O(N) 배열 생성 방지 #1483
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
Closed
seonghobae
wants to merge
1
commit into
develop
from
bolt/network-graph-array-creation-bounds-1308854882889465339
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| ## 2025-05-24 - NetworkGraph O(N) array creation bounds | ||
| **Learning:** Using `Array.from(map.values()).slice(0, N)` creates an array of the entire map in memory (O(N) operations) before truncating it. This can block the main thread and consume significant memory for large datasets in React components when we only need a few items. | ||
| **Action:** Replace `Array.from(iterable).slice(0, N)` with a bounded `for...of` loop that breaks early when the desired number of items is reached, keeping the complexity O(1). |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -277,28 +277,49 @@ export default function NetworkGraph() { | |
| } | ||
| }, [nodes, edges, nodeMap, edgeMap]); | ||
|
|
||
| // ⚡ Bolt: Replace O(N) array mapping and filtering with O(1) bounded iteration | ||
| // 🎯 Why: Mapping and filtering the entire node array blocks the main thread for large graphs when we only need 5 labels | ||
| const nodeLabels = useMemo(() => { | ||
| return nodes | ||
| .map((node) => String(node.label ?? node.id)) | ||
| .filter(Boolean) | ||
| .slice(0, 5); | ||
| const labels = []; | ||
| for (const node of nodes) { | ||
| if (labels.length >= 5) break; | ||
| const label = String(node.label ?? node.id); | ||
| if (label) labels.push(label); | ||
| } | ||
| return labels; | ||
|
Comment on lines
282
to
+289
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. |
||
| }, [nodes]); | ||
|
|
||
| const firstEdge = edges[0] ?? null; | ||
| // ⚡ Bolt: Replace O(N) Array.from().slice() with O(1) bounded iteration | ||
| // 🎯 Why: Converting the entire map to an array blocks the main thread for large graphs when we only need 5 items | ||
| const relationshipOptions = useMemo(() => { | ||
| return Array.from(edgeMap.values()).slice(0, 5).map((edge, index) => ({ | ||
| edge, | ||
| id: String(edge.id), | ||
| label: `관계 ${index + 1}: ${describeEdge(edge, nodeMap)}`, | ||
| })); | ||
| const options = []; | ||
| let index = 0; | ||
| for (const edge of edgeMap.values()) { | ||
| if (options.length >= 5) break; | ||
| options.push({ | ||
| edge, | ||
| id: String(edge.id), | ||
| label: `관계 ${index + 1}: ${describeEdge(edge, nodeMap)}`, | ||
| }); | ||
| index++; | ||
| } | ||
| return options; | ||
| }, [edgeMap, nodeMap]); | ||
|
|
||
| // ⚡ Bolt: Replace O(N) Array.from().slice() with O(1) bounded iteration | ||
| // 🎯 Why: Converting the entire map to an array blocks the main thread for large graphs when we only need 8 items | ||
| const nodeOptions = useMemo(() => { | ||
| return Array.from(nodeInstanceMap.values()).slice(0, 8).map((node) => ({ | ||
| id: String(node.id), | ||
| label: `노드: ${String(node.label ?? node.id)}`, | ||
| node, | ||
| })); | ||
| const options = []; | ||
| for (const node of nodeInstanceMap.values()) { | ||
| if (options.length >= 8) break; | ||
| options.push({ | ||
| id: String(node.id), | ||
| label: `노드: ${String(node.label ?? node.id)}`, | ||
| node, | ||
| }); | ||
| } | ||
| return options; | ||
| }, [nodeInstanceMap]); | ||
|
Comment on lines
295
to
323
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. |
||
|
|
||
| const selectRelationship = (edge: Edge, status: string) => { | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
🚀 Performance & Scalability | 🟡 Minor | ⚡ Quick win
Bound the
nodeLabelssource scan or correct the performance claim.The break condition limits the number of collected labels, not the number of nodes visited. If fewer than five labels are non-empty, this loop still scans all of
nodes, so large sparse graphs retain O(N) traversal and do not meet the documented bounded-iteration objective. If labels are guaranteed to be non-empty, enforce that invariant and stop after five source nodes; otherwise document and test the remaining full-scan case.🤖 Prompt for AI Agents