Skip to content

[Performance] Memoize collectFilePaths() in AI Chat Panel to avoid per-render traversal #527

Description

@Somil450

Problem

In ai-chat-panel.tsx, the collectFilePaths() function is called on every render inside the useChat() body configuration. This function traverses the entire virtual file system tree to build a flat list of all file paths. In large projects with many files, this is an unnecessary and expensive operation that runs on every keystroke and every streaming token received.

Proposed Fix

Wrap the call in useMemo:

const filePaths = useMemo(
  () => collectFilePaths(fileTree),
  [fileTree]
);

This ensures file path collection only re-runs when the actual file tree changes, not on every render.

Impact

  • Reduces unnecessary CPU usage during AI streaming
    • Improves typing responsiveness in the chat input
      • Scales better as project file count grows

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions