Skip to content

🫣 perf: Defer Offscreen Message Toolbars Until Needed - #18551

Merged
danny-avila merged 1 commit into
devfrom
danny-avila/row-toolbar-on-reach
Nov 20, 2026
Merged

danny-avila merged 1 commit into
devfrom
danny-avila/row-toolbar-on-reach

Conversation

@danny-avila

Copy link
Copy Markdown
Collaborator

Summary

Opening a long conversation keeps the page busy for seconds after its first rows appear, in a run of tasks long enough to drop clicks and keystrokes, and the cost grows faster than the conversation does. Close to half of it, and most of it in a very long conversation, is building a toolbar for every message: the copy, edit, branch, feedback, read-aloud and rerun buttons that show on hover, or under the latest message.

A long thread already mounts in steps: the rows around the scroll position first, then the rest a chunk at a time, and rows more than a screen from the viewport are skipped by the browser. The toolbar was still built for every one of those rows as it mounted, although nobody can press a button in a row a thousand pixels off screen.

With this change, a row that a long thread adds after its first commit leaves its toolbar out until the row comes within a screen of the viewport, then keeps it. The footer holds the toolbar's height the whole time, so no row changes size. Nothing changes for the rows a thread opens on, for the latest message, for a thread short enough to mount at once, or for rows outside the main thread view.

Measured with the new e2e/benchmarks-fade/open.perf.spec.ts on the built client, CPU unthrottled, switching to the conversation from the sidebar and tracing until every row is in:

thread build main-thread time tasks over 50 ms longest task
246 rows, 25k elements dev 2,087 to 2,238 ms 8 99 to 121 ms
246 rows this branch 1,275 to 1,454 ms 2 98 to 109 ms
984 rows, 100k elements dev 10,109 to 10,530 ms 32 to 33 125 to 237 ms
984 rows this branch 4,676 to 4,713 ms 8 to 10 90 to 103 ms

Reloading the 246-row thread goes from 2,959 to 3,242 ms, with every row in at about 3.1 s, to 1,832 to 2,012 ms, with every row in at about 1.7 s; tasks over 50 ms go from 11 or 12 to between 4 and 6. Each figure is three rounds (two for the 984-row thread). The 984-row thread is the 246-row export chained four times. dev here is this branch's build with the change switched off by a temporary flag that is not in the diff, so the two sides differ in nothing else.

The other benchmarks in that directory, run on the same build with the change switched off and on:

benchmark dev this branch
send a short reply in the opened 246-row thread 1,729 and 1,773 ms, longest task 217 and 210 ms, 6 tasks over 50 ms 1,234 and 1,348 ms, longest task 83 and 92 ms, 1 or 2 over 50 ms
scroll back 30 pages, 246 rows (up / down) 635, 637 / 646, 650 ms 694, 688 / 591, 602 ms
scroll back 30 pages, 984 rows (up / down) 1,409 / 1,424 ms 1,333 / 1,147 ms
sidebar collapse / expand 137 to 151 / 193 to 208 ms 126 to 143 / 185 to 194 ms, with one outlying round

A send and its completion re-render the toolbar of every row that has one, which is why a run in a freshly opened thread gets cheaper too; a reader who has scrolled through the whole thread has mounted every toolbar and is back to the first column. The first pass up the 246-row thread costs about 9% more, the toolbars mounting as their rows come near; the longest task of that pass is 10 to 11 ms either way.

Not addressed here: switching to a thread while it has a reply in flight mounts every remaining row in one commit instead of in steps. That is a single task of 1.5 s in the 246-row thread and 7 s in the 984-row one, with or without this change, and it is the next change. Not measured: Firefox and Safari, a throttled CPU, a touch device.

How it works

MessagesView
  useProgressiveRowMount      # the window of rows allowed to mount, widened step by step
  RowMountProvider            # + whether the rows mounting now are a widening step's
  useOffscreenRows            # one IntersectionObserver per thread, a screen of margin
    MessageRow
      useWidenedRow           # fixed at mount: did a widening step add this row?
      useOffscreenRow(far)    # far rows start unreached; the observer reaches them
      RowReachProvider
        MessageFooter
          useRowReached       # reached || latest message ? toolbar : a stand-in

A row learns once, as it mounts, whether a widening step added it. The provider keeps that in a ref the rows read rather than in a context value, because a value that flipped would re-render every row of the thread to tell the few that are mounting.

The thread's existing observer (the one that marks rows data-offscreen) calls the row back the first time it finds it within a screen of the viewport. The toolbar then mounts in a transition, so a fast scroll through a long thread does not trade one blocked frame for another. Once mounted it stays.

Three things reach a row's toolbar from somewhere else, and each mounts what it needs synchronously through reachRow or revealOffscreenRows:

  • The copy-last-response and edit-last-message shortcuts press a button in the thread's last turns from wherever the reader has scrolled to. They reach that row first, so they cannot land on an earlier message's button instead.
  • The latest message never leaves its toolbar out: the rerun, continue and read-aloud shortcuts press its buttons.
  • Screenshot export clones the thread, so it mounts every toolbar before it does.

A browser without IntersectionObserver holds nothing back.

Type of change

  • Performance improvement

Testing

The benchmarks above, on the mock e2e lane in a real browser. Two new scenarios in e2e/specs/mock/scenarios/offscreen-rows.spec.ts open an 80-turn thread and check that the rows far from the viewport have no toolbar while every row near it has one, and that a far reply gains its copy button as a scroll brings it near, at the same row height, shows it on hover, and keeps it after scrolling away.

Tested environments/configuration:

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

Automated tests:

  • packages/chat: new cases in offscreen.spec.tsx (a far row holds its toolbar back until the observer reports it near, then keeps it; reachRow and revealOffscreenRows mount it before returning; nothing is held back outside a watched thread or without IntersectionObserver), useProgressiveRowMount.spec.tsx (useWidenedRow) and MessageFooter.spec.tsx (the stand-in, the latest message). Four of them fail with the gate removed. npx jest src/react/messages src/components/messages, npx tsc --noEmit.
  • client: a new case in useKeyboardShortcuts.spec.tsx (copy-last-response reaches the last reply's row before pressing), npx jest src/components/Chat/Messages src/components/Messages src/Providers (108 suites), npx tsc --noEmit.
  • Mock e2e: offscreen-rows, long-conversation-mount, and the specs that press toolbar buttons (hover-actions, hover-buttons, message-actions, message-footer, mouse-reveal, fork-feedback, turn-regenerate, message-tree, message-edit-layout, branch-history, thread-rows, tts-cache and others): 31 scenarios on this head, and 99 across the wider set on the commit before the last rebase.

Opening a long conversation built a toolbar for every message as its rows
mounted: the copy, edit, branch, feedback, read-aloud and rerun buttons
that show on hover or under the latest message. That was close to half of
what opening a 246-row thread cost the main thread, and most of it in a
thread four times as long.

A row a long thread adds after its first commit now leaves its toolbar out
until the thread's row observer finds it within a screen of the viewport,
then keeps it. The footer holds the toolbar's height throughout. The rows
a thread opens on, the latest message, short threads and rows outside the
main thread view are unchanged.

The copy-last-response and edit-last-message shortcuts reach their row
first, and screenshot export mounts every toolbar before it clones.

Adds e2e/benchmarks-fade/open.perf.spec.ts, which traces a reload, a switch
from the sidebar and a switch while a reply is in flight.
@danny-avila
danny-avila marked this pull request as ready for review November 20, 2026 03:05
@lia-by-librechat lia-by-librechat Bot changed the title 🧰 perf: Mount a Row's Toolbar Once the Row Comes Near the Viewport 🫣 perf: Defer Offscreen Message Toolbars Until Needed Nov 20, 2026
@danny-avila
danny-avila added this pull request to stack #18563 November 20, 2026 03:05
@danny-avila
danny-avila merged commit 66aab36 into dev Nov 20, 2026
65 of 66 checks passed
@danny-avila
danny-avila deleted the danny-avila/row-toolbar-on-reach branch November 20, 2026 03:07
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