Skip to content

⚡ Bolt: Replace O(N) array find with O(1) map lookups for node selection#68

Open
mbayue wants to merge 2 commits into
masterfrom
bolt-optimization-map-lookups-7442781794420946948
Open

⚡ Bolt: Replace O(N) array find with O(1) map lookups for node selection#68
mbayue wants to merge 2 commits into
masterfrom
bolt-optimization-map-lookups-7442781794420946948

Conversation

@mbayue

@mbayue mbayue commented Jul 3, 2026

Copy link
Copy Markdown
Owner

⚡ Bolt: Replace O(N) array find with O(1) map lookups for node selection

💡 What: Replaced linear array .find() lookups with O(1) Map.get() lookups across AISidebar.tsx, useAiCenterState.ts, and VizPage.tsx. Pre-computes a map of node.id -> node using useMemo.
🎯 Why: Prevents blocking the React render cycle when looking up selectedNode in large graphs with thousands of nodes, which was previously happening via linear scans every render.
📊 Impact: Expected to reduce lookup times for selected node resolution from O(N) to O(1), directly mitigating frame drops during graph interactions for large codebases.
🔬 Measurement: Can be verified by profiling React render times during interactions in a repository with > 1,000 files.


PR created automatically by Jules for task 7442781794420946948 started by @mbayue


Summary by cubic

Replaced O(N) array .find() with O(1) Map.get() for resolving selectedNode, removing render-time scans and smoothing interactions on large graphs.

  • Refactors
    • Precompute nodeById via useMemo and use it in AISidebar.tsx, useAiCenterState.ts, and VizPage.tsx.
    • Updated useMemo deps in VizPage.tsx to avoid stale reads; behavior unchanged, lookups faster.

Written for commit 91cd23e. Summary will update on new commits.

Review in cubic

💡 What: Replaced linear array `.find()` lookups with O(1) `Map.get()` lookups across `AISidebar.tsx`, `useAiCenterState.ts`, and `VizPage.tsx`. Pre-computes a map of `node.id -> node` using `useMemo`.
🎯 Why: Prevents blocking the React render cycle when looking up `selectedNode` in large graphs with thousands of nodes, which was previously happening via linear scans every render.
📊 Impact: Expected to reduce lookup times for selected node resolution from O(N) to O(1), directly mitigating frame drops during graph interactions for large codebases.
🔬 Measurement: Can be verified by profiling React render times during interactions in a repository with > 1,000 files.

Co-authored-by: mbayue <[email protected]>
@google-labs-jules

Copy link
Copy Markdown
Contributor

👋 Jules, reporting for duty! I'm here to lend a hand with this pull request.

When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down.

I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job!

For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

@github-actions

github-actions Bot commented Jul 3, 2026

Copy link
Copy Markdown

Dependency Review

✅ No vulnerabilities or license issues or OpenSSF Scorecard issues found.

Scanned Files

None

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 issue found across 4 files

Confidence score: 5/5

  • src/components/viz/ai-sidebar/hooks/useAiCenterState.ts includes an AI-branded, copy-pasted comment that adds noise rather than clarity; merging as-is is unlikely to break behavior but can reduce maintainability and reviewer trust in future diffs. This PR is safe to merge, but it would be cleaner to remove or rewrite the comment to capture file-specific intent before merging.
Architecture diagram
sequenceDiagram
    participant Store as VizStore/State
    participant Comp as React Component
    participant Graph as Graph Nodes Array
    participant Map as nodeById Map (useMemo)
    participant SelectedId as selectedNodeId

    Note over Store,SelectedId: O(1) Lookup Path (Post-PR)

    Comp->>Store: useVizStore() → selectedNodeId, analysis.graph.nodes
    Comp->>Graph: access nodes array
    Comp->>Map: useMemo → new Map(nodes.map(n=>[n.id, n]))
    Map-->>Comp: nodeById (Map instance)

    alt selectedNodeId is not null
        Comp->>Map: nodeById.get(selectedNodeId)
        Map-->>Comp: selectedNode (GraphNode) or null
    else selectedNodeId is null
        Comp->>Comp: selectedNode = null (no lookup needed)
    end

    Comp->>Comp: Render with selectedNode
    Note over Comp: O(1) Map.get() avoids O(N) array scan each render
Loading

Reply with feedback, questions, or to request a fix.

Re-trigger cubic

Comment thread src/components/viz/ai-sidebar/hooks/useAiCenterState.ts Outdated
💡 What: Replaced linear array `.find()` lookups with O(1) `Map.get()` lookups across `AISidebar.tsx`, `useAiCenterState.ts`, and `VizPage.tsx`. Pre-computes a map of `node.id -> node` using `useMemo`.
🎯 Why: Prevents blocking the React render cycle when looking up `selectedNode` in large graphs with thousands of nodes, which was previously happening via linear scans every render.
📊 Impact: Expected to reduce lookup times for selected node resolution from O(N) to O(1), directly mitigating frame drops during graph interactions for large codebases.
🔬 Measurement: Can be verified by profiling React render times during interactions in a repository with > 1,000 files.

Co-authored-by: mbayue <[email protected]>
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