Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions webview-ui/src/components/chat/ChatRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ interface ChatRowContentProps extends Omit<ChatRowProps, "onHeightChange"> {}
const ChatRow = memo(
(props: ChatRowProps) => {
const { isLast, onHeightChange, message } = props

// Store the previous height to compare with the current height
// This allows us to detect changes without causing re-renders
const prevHeightRef = useRef(0)
Expand Down
20 changes: 13 additions & 7 deletions webview-ui/src/components/chat/ChatView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down Expand Up @@ -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])
Copy link
Contributor Author

Choose a reason for hiding this comment

The 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 modifiedMessages change could lead to memory pressure in long-running sessions? Could we consider using a more granular dependency or perhaps a ref-based approach to track just the last message without triggering re-renders?


// Properly memoized itemContent callback with stable dependencies
const itemContent = useCallback(
Copy link
Contributor Author

Choose a reason for hiding this comment

The 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
Expand All @@ -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}
Expand All @@ -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}
Expand Down Expand Up @@ -1582,7 +1588,7 @@ const ChatViewComponent: React.ForwardRefRenderFunction<ChatViewRef, ChatViewPro
[
expandedRows,
toggleRowExpansion,
modifiedMessages,
lastModifiedMessage, // Use the memoized value instead of recalculating
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Could we add a comment explaining why switching from modifiedMessages to lastModifiedMessage in the dependency array prevents the re-rendering issue? The fix works, but documenting the reasoning would help future maintainers understand the optimization.

groupedMessages.length,
handleRowHeightChange,
isStreaming,
Expand Down
Loading