Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

perf(max): memoize thread messages to cut re-renders in long threads (#64792)

Georgiy Tarasov3962f09f8cf6
diff --git a/frontend/src/scenes/max/Thread.tsx b/frontend/src/scenes/max/Thread.tsx
index f5e1f9b04b73..a5640dc81133 100644
--- a/frontend/src/scenes/max/Thread.tsx
+++ b/frontend/src/scenes/max/Thread.tsx
@@ -384,7 +384,10 @@ interface MessageProps {
isSlashCommandResponse?: boolean
}
-function Message({
+// Memoized so a thread re-render (e.g. on every streaming token) only re-renders messages whose
+// props actually changed. Relies on threadGrouped preserving message object identity for unchanged
+// messages — see enhanceThreadToolCalls in maxThreadLogic.
+const Message = React.memo(function Message({
message,
nextMessage,
isLastInGroup,
@@ -722,7 +725,7 @@ function Message({
</div>
</MessageContainer>
)
-}
+})
function MessageGroupSkeleton({
groupType,
diff --git a/frontend/src/scenes/max/maxThreadLogic.tsx b/frontend/src/scenes/max/maxThreadLogic.tsx
index dff761fe691d..c64a2b1d096a 100644
--- a/frontend/src/scenes/max/maxThreadLogic.tsx
+++ b/frontend/src/scenes/max/maxThreadLogic.tsx
@@ -2025,12 +2025,12 @@ function enhanceThreadToolCalls(
// Enhance assistant messages with tool call status
return group.map((message, messageIndex) => {
- message = { ...message }
// A message is in the final group if it comes after or is the last human message
const isFinalGroup = messageIndex >= lastHumanMessageIndex
if (isAssistantMessage(message) && message.tool_calls && message.tool_calls.length > 0) {
const isLastPlanningMessage = message.id === lastPlanningMessageId
- message.tool_calls = message.tool_calls.map<EnhancedToolCall>((toolCall) => {
+ const enhancedMessage = { ...message }
+ enhancedMessage.tool_calls = message.tool_calls.map<EnhancedToolCall>((toolCall) => {
const resultMessage = toolCallCompletions.get(toolCall.id)
const isCompleted = !!resultMessage
// create_form is an interactive tool - it's "completed" once rendered (waiting for user input)
@@ -2054,7 +2054,10 @@ function enhanceThreadToolCalls(
result: isAssistantToolCallMessage(resultMessage) ? resultMessage : undefined,
}
})
+ return enhancedMessage
}
+ // Messages we don't enhance are returned by reference so their identity stays stable
+ // across recomputes — this is what lets React.memo(Message) skip them on each token.
return message
})
}