Skip to content

perf(web): stop re-rendering the whole chat transcript on every stream flush - #18221

Merged
wangq8 merged 1 commit into
infiniflow:mainfrom
dcc123456:perf/web-chat-streaming-render
Aug 13, 2026
Merged

wangq8 merged 1 commit into
infiniflow:mainfrom
dcc123456:perf/web-chat-streaming-render

Conversation

@dcc123456

Copy link
Copy Markdown
Collaborator

Summary

perf(web): stop re-rendering the whole chat transcript on every stream flush

Each streaming flush replaced the message list, and the render loop then rebuilt every message's reference object inline. That handed MessageItem a brand new reference on each flush, defeating its memo and re-parsing the markdown of the entire transcript ~16 times a second.

  • Resolve all references once per message-list change in useMessageReferences, keyed by message object (a question and its answer share an id), and share a single EmptyReference fallback.
  • Memoize MarkdownContent and hoist its rehype plugins and components out of the render body, since react-markdown rebuilds its processor whenever a plugin identity changes.
  • Skip the empty setDocumentIds write that re-rendered every reference-less message.
  • Collapse the rAF + setTimeout auto-scroll chain into one cancellable frame and update isAtBottomRef in the scroll handler, so a queued scroll can no longer yank the view back down after the user scrolls up.
  • Require callers of sendMessage to pass the history snapshot taken before the question and placeholder were appended, instead of reading a store value that already contains them.

…m flush

Each streaming flush replaced the message list, and the render loop then
rebuilt every message's reference object inline. That handed MessageItem a
brand new reference on each flush, defeating its memo and re-parsing the
markdown of the entire transcript ~16 times a second.

- Resolve all references once per message-list change in
  useMessageReferences, keyed by message object (a question and its answer
  share an id), and share a single EmptyReference fallback.
- Memoize MarkdownContent and hoist its rehype plugins and components out
  of the render body, since react-markdown rebuilds its processor whenever
  a plugin identity changes.
- Skip the empty setDocumentIds write that re-rendered every
  reference-less message.
- Collapse the rAF + setTimeout auto-scroll chain into one cancellable
  frame and update isAtBottomRef in the scroll handler, so a queued scroll
  can no longer yank the view back down after the user scrolls up.
- Require callers of sendMessage to pass the history snapshot taken before
  the question and placeholder were appended, instead of reading a store
  value that already contains them.
@dosubot dosubot Bot added size:L This PR changes 100-499 lines, ignoring generated files. 🧰 typescript Pull requests that update Typescript code labels Aug 13, 2026
@dcc123456
dcc123456 requested a review from wangq8 August 13, 2026 08:48
@dcc123456 dcc123456 added 🐞 bug Something isn't working, pull request that fix bug. ci Continue Integration labels Aug 13, 2026
@dcc123456
dcc123456 marked this pull request as draft August 13, 2026 08:49
@dcc123456
dcc123456 marked this pull request as ready for review August 13, 2026 08:49
@coderabbitai

coderabbitai Bot commented Aug 13, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 920c4802-99d0-49c0-a4ea-96cb99bbb77b

📥 Commits

Reviewing files that changed from the base of the PR and between bedc395 and 5572033.

📒 Files selected for processing (8)
  • web/src/components/markdown-content/index.tsx
  • web/src/hooks/logic-hooks.ts
  • web/src/pages/next-chats/chat/chat-box/next-multiple-chat-box.tsx
  • web/src/pages/next-chats/chat/chat-box/single-chat-box.tsx
  • web/src/pages/next-chats/hooks/use-message-references.ts
  • web/src/pages/next-chats/hooks/use-send-chat-message.ts
  • web/src/pages/next-chats/share/index.tsx
  • web/src/pages/next-chats/utils.ts

📝 Walkthrough

Walkthrough

The changes centralize chat message-reference resolution, pass explicit message history during sending and regeneration, and reduce unnecessary markdown rendering and auto-scroll updates.

Changes

Chat flow updates

Layer / File(s) Summary
Centralized message-reference resolution
web/src/pages/next-chats/hooks/use-message-references.ts, web/src/pages/next-chats/utils.ts
Added memoized reference resolution and a shared EmptyReference fallback. Removed buildMessageItemReference.
Chat view integration
web/src/pages/next-chats/chat/chat-box/*.tsx, web/src/pages/next-chats/share/index.tsx
Chat views use the precomputed reference map for each MessageItem.
Explicit send history
web/src/pages/next-chats/hooks/use-send-chat-message.ts
Send and regeneration paths snapshot message history and pass it explicitly to sendMessage.
Rendering and scroll optimization
web/src/components/markdown-content/index.tsx, web/src/hooks/logic-hooks.ts
Markdown handlers and configurations are memoized. Document ID updates skip empty results. Auto-scroll uses current bottom state and one cancellable animation frame.

Estimated code review effort: 3 (Moderate) | ~25 minutes

Mergeability Score: ⚪ Minimal · up to 55720

The performance-focused rendering and scrolling changes introduce no actionable merge-blocking risk based on the supplied evidence; the PR is merge-ready after normal checks and review.

Possibly related PRs

Suggested reviewers: wangq8, cike8899

Poem

I’m a rabbit with references neat,
Mapping each message from tail to tweet.
History snapshots hop in line,
Markdown renders crisp and fine.
One scroll frame, then off I flee—
Fast chat paths for you and me.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Title check ✅ Passed The title clearly and concisely describes the primary performance improvement in the chat transcript.
Description check ✅ Passed The description includes the required Summary section and explains the background, scope, and key implementation changes.

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@wangq8 wangq8 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks for this — the diagnosis is correct and the core memoization/hoisting work is well done. I reviewed against current main (b1de7e8). A few things to address before landing:

1. This PR bundles two behavioral changes that aren't perf-related and aren't in the summary

web/src/pages/next-chats/hooks/use-send-chat-message.ts:

  • Regenerate semantics changed. useRegenerateMessage (truncate-after + rewrite-in-place, preserving the original question id — the behavior #17087 fixed) is replaced by an inline regenerateMessage that re-asks the question as a brand-new turn (fresh uuid()), keeping the previous Q&A in the transcript. That's a user-visible behavior change, not a perf change. Please split it out, or document the intended product change explicitly.

  • New-conversation hydration added. handlePressEnter now calls hydrateFromServer(targetConversationId, buildMessageListWithUuid(currentMessages)) before appending the question, which reverses the "Do NOT hydrate from currentMessages here" guidance from #18180. I follow the reasoning (the prologue seeded on the temp id doesn't carry over to the server-generated id, so the history snapshot would drop it), but it's a subtle correctness change that deserves its own review/PR — or at least a clear note in the description.

Both are defensible on their own, but they're currently hidden inside a perf(web) PR.

2. Minor — EmptyReference is a shared mutable singleton

web/src/pages/next-chats/utils.ts: any future consumer that mutates doc_aggs/chunks (e.g. push) would corrupt every reference-less message at once. Current callers are read-only, but consider Object.freeze(...) or a comment so it stays that way.

3. Minor — skipping the empty setDocumentIds write can leave stale state

web/src/components/markdown-content/index.tsx: the if (documentIds.length === 0) return; guard means that if a mounted message's reference ever transitions from non-empty → empty, the thumbnail-id state won't reset and stale thumbnails could persist. Fine for the current streaming flow (references only get populated, never cleared), but worth a one-line comment.

4. Verified-correct

  • useMessageReferences faithfully mirrors the old buildMessageItemReference indexing (prologue skip, error-answer exclusion, pending-answer → EmptyReference, first-id-wins).
  • The module-scope rehypeWrapReference / MarkdownCode / MarkdownParagraph hoists in markdown-content capture no render-scope state, so the react-markdown processor rebuild is genuinely avoided.
  • The scroll change (synchronous isAtBottomRef update in the scroll handler + single cancellable rAF + re-check inside the frame) is sound and actually fixes the "yank back down" race.
  • All sendMessage callers now pass an explicit history snapshot; no caller is left spreading undefined.

@wangq8
wangq8 merged commit 7427d94 into infiniflow:main Aug 13, 2026
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

🐞 bug Something isn't working, pull request that fix bug. ci Continue Integration size:L This PR changes 100-499 lines, ignoring generated files. 🧰 typescript Pull requests that update Typescript code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants