LibreChat/client/src/components/Messages/MessageContent.tsx
Danny Avila b341f93805
Some checks are pending
Docker Dev Images Build / publish (push) Waiting to run
Sync Helm Chart Tags / Ignore non-main push (push) Waiting to run
Sync Helm Chart Tags / Sync chart tags (push) Waiting to run
🏎️ perf: Re-render Only the Tail Rows When a Message Is Sent (#16572)
Every send moved the thread's tail (isSubmitting, latestMessageId,
latestMessageDepth, and a handleContinue rebound per turn), and the memoized
row context listed all of them as dependencies, so each move handed every row
a new context object and re-rendered the whole thread: three commits of
~12,000 component renders on a 246-message thread, one of them inside the
Enter keypress.

- Keep the row context object stable; the tail values are ref-backed getters
  for call-time reads, and rows receive them as props that the row comparator
  (isSameTailRelation) only reacts to when the row enters or leaves the tail.
- Fork reads the split target through a getter at click time.
- HoverButtons subscribes to a boolean isSubmitting context and a stable
  operations context instead of the combined messages-view context; Fork and
  Feedback are memoized so the per-send toolbar pass stops there.
- Make handleContinue referentially stable.
- Cache the Intl formatters behind getMessageTimestamp per locale and clock.
- Add a submit-lag benchmark (e2e/benchmarks-tree) that reports long tasks,
  Enter-to-row latency, a CPU profile and per-commit render reasons.
2026-09-30 10:19:49 -04:00

52 lines
1.4 KiB
TypeScript

import React from 'react';
import type { TMessageProps } from '~/common';
import { useMessageProcess, useMemoizedChatContext } from '~/hooks';
import { areMessageRowPropsEqual } from '~/utils';
import ContentRender from './ContentRender';
const MessageContainer = React.memo(function MessageContainer({
handleScroll,
children,
}: {
handleScroll: (event?: unknown) => void;
children: React.ReactNode;
}) {
return (
<div
className="w-full border-0 bg-transparent text-text-primary"
onWheel={handleScroll}
onTouchMove={handleScroll}
>
{children}
</div>
);
});
function MessageContent(props: TMessageProps) {
const { handleScroll, isSubmitting } = useMessageProcess({
message: props.message,
});
const { message } = props;
const { chatContext, effectiveIsSubmitting, latestMessageId, latestMessageDepth } =
useMemoizedChatContext(message, isSubmitting);
if (!message || typeof message !== 'object') {
return null;
}
return (
<MessageContainer handleScroll={handleScroll}>
<div className="m-auto justify-center px-4 py-3 sm:px-0">
<ContentRender
{...props}
chatContext={chatContext}
isSubmitting={effectiveIsSubmitting}
latestMessageId={latestMessageId}
latestMessageDepth={latestMessageDepth}
/>
</div>
</MessageContainer>
);
}
export default React.memo(MessageContent, areMessageRowPropsEqual);