-
Notifications
You must be signed in to change notification settings - Fork 2.6k
fix: optimize ChatView to prevent excessive re-rendering when typing #7420
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
Changes from all 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 |
|---|---|---|
|
|
@@ -160,7 +160,9 @@ const ChatViewComponent: React.ForwardRefRenderFunction<ChatViewRef, ChatViewPro | |
| return getLatestTodo(messages) | ||
| }, [messages, currentTaskTodos]) | ||
|
|
||
| const modifiedMessages = useMemo(() => combineApiRequests(combineCommandSequences(messages.slice(1))), [messages]) | ||
| const modifiedMessages = useMemo(() => { | ||
| return combineApiRequests(combineCommandSequences(messages.slice(1))) | ||
| }, [messages]) | ||
|
|
||
| // Has to be after api_req_finished are all reduced into api_req_started messages. | ||
| const apiMetrics = useMemo(() => getApiMetrics(modifiedMessages), [modifiedMessages]) | ||
|
|
@@ -1521,6 +1523,10 @@ const ChatViewComponent: React.ForwardRefRenderFunction<ChatViewRef, ChatViewPro | |
| userRespondedRef.current = true | ||
| }, []) | ||
|
|
||
| // Memoize the last modified message to prevent unnecessary re-renders | ||
| const lastModifiedMessage = useMemo(() => modifiedMessages.at(-1), [modifiedMessages]) | ||
|
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. While this memoization correctly prevents re-renders, I'm wondering if creating a new reference on every |
||
|
|
||
| // Properly memoized itemContent callback with stable dependencies | ||
| const itemContent = useCallback( | ||
|
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. Since this is a performance fix, would it be helpful to add a comment documenting the performance improvement achieved? For example: "Reduces re-renders from X to Y when typing". Future me would appreciate knowing why past me made this change. |
||
| (index: number, messageOrGroup: ClineMessage | ClineMessage[]) => { | ||
| // browser session group | ||
|
|
@@ -1529,7 +1535,7 @@ const ChatViewComponent: React.ForwardRefRenderFunction<ChatViewRef, ChatViewPro | |
| <BrowserSessionRow | ||
| messages={messageOrGroup} | ||
| isLast={index === groupedMessages.length - 1} | ||
| lastModifiedMessage={modifiedMessages.at(-1)} | ||
| lastModifiedMessage={lastModifiedMessage} | ||
| onHeightChange={handleRowHeightChange} | ||
| isStreaming={isStreaming} | ||
| isExpanded={(messageTs: number) => expandedRows[messageTs] ?? false} | ||
|
|
@@ -1549,12 +1555,12 @@ const ChatViewComponent: React.ForwardRefRenderFunction<ChatViewRef, ChatViewPro | |
| key={messageOrGroup.ts} | ||
| message={messageOrGroup} | ||
| isExpanded={expandedRows[messageOrGroup.ts] || false} | ||
| onToggleExpand={toggleRowExpansion} // This was already stabilized | ||
| lastModifiedMessage={modifiedMessages.at(-1)} // Original direct access | ||
| isLast={index === groupedMessages.length - 1} // Original direct access | ||
| onToggleExpand={toggleRowExpansion} | ||
| lastModifiedMessage={lastModifiedMessage} | ||
| isLast={index === groupedMessages.length - 1} | ||
| onHeightChange={handleRowHeightChange} | ||
| isStreaming={isStreaming} | ||
| onSuggestionClick={handleSuggestionClickInRow} // This was already stabilized | ||
| onSuggestionClick={handleSuggestionClickInRow} | ||
| onBatchFileResponse={handleBatchFileResponse} | ||
| onFollowUpUnmount={handleFollowUpUnmount} | ||
| isFollowUpAnswered={messageOrGroup.ts === currentFollowUpTs} | ||
|
|
@@ -1582,7 +1588,7 @@ const ChatViewComponent: React.ForwardRefRenderFunction<ChatViewRef, ChatViewPro | |
| [ | ||
| expandedRows, | ||
| toggleRowExpansion, | ||
| modifiedMessages, | ||
| lastModifiedMessage, // Use the memoized value instead of recalculating | ||
|
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. Could we add a comment explaining why switching from |
||
| groupedMessages.length, | ||
| handleRowHeightChange, | ||
| isStreaming, | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.