Skip to content

🥞 perf: Progressively Mount Long Threads During Active Replies - #18555

Merged
danny-avila merged 2 commits into
devfrom
danny-avila/mount-steps-while-replying
Nov 20, 2026
Merged

danny-avila merged 2 commits into
devfrom
danny-avila/mount-steps-while-replying

Conversation

@danny-avila

@danny-avila danny-avila commented Nov 19, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Opening a long conversation that has a reply in flight freezes the page. A long thread normally mounts in steps, the rows around the scroll position first and the rest a chunk at a time, but a run under way switched that off: the first sixteen rows appear, then every remaining row mounts in one synchronous commit. In a 246-row thread that commit is a single task of 1.5 s; in a 984-row thread, 7 s, during which nothing on the page responds. It happens on every return to a conversation whose agent is still working, on a reload of one, and when a message is sent while a long thread is still mounting.

With this change the thread keeps mounting in steps while a reply is in flight. The reply's row is at the thread's end, which the window of mounted rows always holds when it is anchored there (the default), so the rows above keep arriving: four per frame, each step committed at once. The longest task of the open drops to the first commit's, about 100 ms, whatever the thread's length. A thread anchored at its start (auto-scroll switched off) does not hold the reply's row and still mounts the rest at once when a run starts, as before.

A step also stops costing a render of every message in the thread. Each level of the thread read the window from context, so widening it re-rendered a component per message on every step: work that grows with the thread, paid once per step. Rows now wait on the window through a subscription and only the rows a step adds render. That is what makes small steps affordable, and it shortens the ordinary open as well.

Measured with e2e/benchmarks-fade/open.perf.spec.ts on the built client, CPU unthrottled, switching to the conversation from the sidebar. "In flight" is a run that has started and not produced a token yet; "streaming" is one writing its reply as the thread mounts.

thread reply build longest task main-thread time rows per frame
246 rows in flight #18551 1,456 to 1,627 ms 2,076 to 2,258 ms 16, then 232 at once
246 rows in flight this branch 105 to 112 ms 1,682 to 1,732 ms 16, then 4 a frame
246 rows streaming this branch 86 to 103 ms 1,951 to 2,092 ms 16, then 4 a frame
984 rows in flight #18551 6,976 to 7,508 ms 8,389 to 9,159 ms 16, then 970 at once
984 rows in flight this branch 91 to 104 ms 7,131 to 7,280 ms 16, then 4 a frame
984 rows streaming this branch 90 to 109 ms 8,936 to 9,014 ms 16, then 4 a frame
246 rows none #18551 98 to 109 ms 1,275 to 1,454 ms 16, then 32 a step
246 rows none this branch 91 to 100 ms 1,165 to 1,247 ms 16, then 32 a step
984 rows none #18551 90 to 103 ms 4,676 to 4,713 ms 16, then 32 a step
984 rows none this branch 95 to 107 ms 4,512 to 4,526 ms 16, then 32 a step

The 984-row thread with a reply streaming keeps the main thread about 88% busy for nine seconds while it mounts, in frames of about 23 ms: responsive, not idle. The total is what mounting a thread that size costs; this change is about no part of it being a freeze. Not measured: Firefox and Safari, a throttled CPU.

Stacked on #18551: the rows these steps add count as mounted away from the reader, so they leave their toolbars out until scrolled near.

How it works

Why the steps beside a reply are not transitions: every frame of a streaming reply is an urgent update, and an urgent update discards a transition's unfinished render. A 32-row chunk takes longer to render than the gap between two frames, so it would be started over again and again without landing. Beside a reply each step is four rows, committed in one go, which fits in a frame next to the reply's own update.

useProgressiveRowMount
  moveTo(next)                  # setMountWindow(next) + publish(gates, next), in one batch
    not submitting              # startTransition: +32 rows, interruptible
    submitting, anchored at end # no transition: +4 rows, one commit per frame
    submitting, anchored at start
                                # the reply is outside the window: mount the rest at once

RowMountProvider gates={gates}  # the same gates the hook publishes to
  MultiMessage / List
    useRowMounted(depth)        # false until the window reaches this depth; listens, then latches
 function MultiMessage(...) {
-  const mountWindow = useRowMountWindow();      // context: every level renders on every step
+  const rowMounted = useRowMounted(messagesTree?.[0]?.depth ?? 0);

The window still lives in the hook's state, and the provider still publishes it after each commit, so a provider driven by its prop alone (no gates passed) mounts rows one commit later rather than not at all. The scroll anchor is unchanged: the first mounted row's document offset is captured before a step and the scroll position corrected by its measured shift after it, which keeps a reader's place whether they are riding the reply or have scrolled up.

Type of change

  • Performance improvement

Testing

The benchmarks above, on the mock e2e lane in a real browser. A new scenario in e2e/specs/mock/scenarios/long-conversation-mount.spec.ts seeds a 160-turn conversation, starts a run that stays in flight, and reloads: the thread mounts in steps (no frame adds more than a chunk of rows), rests on its end, and has every row in while the run is still under way. With the old rule restored on the same build, an earlier form of the scenario failed with a single step of 145 rows.

A reader who scrolls up while the rest of the thread is still arriving is held by the same scroll anchor as before, and it holds no better than before: in the 984-row thread a wheel-up during the mount leaves the marked row 1,527 px from where it was, with no reply in flight, on dev and on this branch alike. That is rows above the reader changing height after their step, which the anchor does not see; it is not addressed here. A scenario for it was dropped: it raced the landing at the thread's end that every open performs.

Tested environments/configuration:

  • Chromium via Playwright, mock e2e lane (in-memory MongoDB, fake model), built client.

Automated tests:

  • packages/chat, useProgressiveRowMount.spec.tsx: a thread anchored at its end keeps its window while a run is under way and widens four rows a frame without a transition; a run starting part way through does not complete the mount, and the steps grow again once it ends; a thread anchored at its start still mounts the rest at once. For the gates: a step renders the rows it adds and no other row; a row whose level only now has a depth inside the window (a turn arriving at the thread's end) is in its first commit; a provider given no gates follows its prop a commit later. npx jest src/react/messages src/components/messages, npx tsc --noEmit.
  • Mock e2e: long-conversation-mount, offscreen-rows, streaming, conversation-management, reload-resume, resume-body-end, pane-resume, stream-disconnect, stream-events, message-tree, branch-history, thread-renderers, thread-rows, thread-fold, scroll-control-fade, thread-end-width, rail-nav-states and others: 74 scenarios, then 46 of them again, then the long-conversation spec five times over on the final head.

@lia-by-librechat lia-by-librechat Bot changed the title 🪜 perf: Mount a Long Thread in Steps While a Reply Is in Flight 🥞 perf: Progressively Mount Long Threads During Active Replies Nov 20, 2026
@danny-avila
danny-avila added this pull request to stack #18563 November 20, 2026 03:05
Base automatically changed from danny-avila/row-toolbar-on-reach to dev November 20, 2026 03:07
A long thread mounts in steps, but a run under way switched that off:
the first sixteen rows appeared, then every remaining row mounted in one
synchronous commit. Returning to a conversation whose agent was still
working froze the page for as long as the whole thread takes to render:
1.5 s for 246 rows, 7 s for 984.

A thread anchored at its end, the default, now keeps widening while a
reply is in flight: four rows a frame, each step committed at once,
because a reply's frames would discard a transition's unfinished render.
A thread anchored at its start does not hold the reply's row and still
mounts the rest at once.

A step also no longer renders a component per message. Rows wait on the
window through a subscription instead of reading it from context, so only
the rows a step adds render, whatever the thread's length.
…t Scenario

The scenario streamed a reply that could finish before a slow machine had reloaded, and its second half moved the reader with a synthetic scroll that raced the landing every open performs. It now starts a run that stays in flight, checks the steps and the resting place, and stops the run. The reader-place half is dropped: it failed on timing that has nothing to do with the steps.
@danny-avila
danny-avila force-pushed the danny-avila/mount-steps-while-replying branch from 5cd97e7 to 049c856 Compare November 20, 2026 03:07
@danny-avila
danny-avila marked this pull request as ready for review November 20, 2026 03:07
@danny-avila
danny-avila merged commit 45b27bb into dev Nov 20, 2026
22 of 23 checks passed
@danny-avila
danny-avila deleted the danny-avila/mount-steps-while-replying branch November 20, 2026 03:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant