LibreChat/e2e/benchmarks-fade/scroll.perf.spec.ts
Danny Avila b3a2ba96a9
🪟 perf: Skip Thread Rows Far From the Viewport (#18201)
* 🪟 perf: Skip Thread Rows Far From the Viewport

Opening or closing the sidebar beside a long thread laid out and painted
every row of it on every frame of the 300 ms slide, because the slide
changes the chat pane's width. In a 246-row thread that was 17 ms of main
thread per frame, 55 ms on a CPU four times slower, where the slide ran at
about a dozen frames a second with tasks over 100 ms.

A row more than a screen from the thread's viewport is now skipped by the
browser (`content-visibility: auto`), so a change that touches the whole
thread costs only the rows near the reader. Rows within a screen are left
exactly as they were: none of them gains the clipping and containment that
skipping implies. A skipped row keeps the size it was last laid out at, so
the scrollbar, the navigation rail and the mount anchor see the same
thread, and it stays reachable by find-in-page, focus and anchors.

The skip sits on the row element the rail and the mount anchor measure:
reading the box of an element inside a skipped row makes the browser lay
that row out again. Screenshot export lays every row out before it clones
the thread, since a clone has no remembered size.

The navigation rail measured every row on each frame of a width change.
It now waits for the width to hold still and measures once; a height
change, which is what a streaming reply is, is still followed frame by
frame. Alone that saved nothing, because its reads forced a layout the
frame needed anyway; with rows skipped it is a fifth of what remains.

* 🧮 fix: Keep Rows Laid Out Until Every Overlapping Capture Is Done

`revealOffscreenRows` handed each caller the thread's `watch` function as
its resume. Two captures that overlap (a second screenshot export started
before the first finished) each got one: the first to finish resumed
skipping while the second was still cloning the thread, and the second
then created another observer without disconnecting the first, which no
cleanup released. A resume called after the thread had unmounted created
an observer for a thread that was gone.

Each thread now counts its outstanding reveals. Skipping resumes when the
last one is done, a resume counts once however often it is called, and
does nothing once the thread has unmounted.
2026-11-16 08:47:14 -05:00

69 lines
2.7 KiB
TypeScript

import { test } from '@playwright/test';
import { formatFrameCost, traceFrames } from './trace';
import { CONVOS, chatName, openChat, removeChats, seedChat } from './chats';
/**
* What reading back through a long thread costs.
*
* Rows far from the viewport are skipped by the browser and laid out again as a scroll brings
* them near. That work used to happen once, when the thread opened; this traces a page-by-page
* sweep from the latest turn towards the first to show what it costs spread across the scroll.
*/
test.use({ trace: 'off', video: 'off' });
/** `FADE_PERF_CPU=4` slows the renderer fourfold, the way DevTools throttling does. */
const CPU_RATE = Number(process.env.FADE_PERF_CPU) || 1;
/** A stylesheet laid over the shipped one, to try a remedy on the same build. */
const EXTRA_CSS = process.env.FADE_PERF_CSS;
const PAGES = 30;
const PAGE_MS = 120;
test.describe('scrolling back through a long thread', () => {
test.skip(CONVOS.length === 0, 'FADE_PERF_CHATS names no exported conversation');
test.afterAll(removeChats);
for (const convo of CONVOS) {
test(`page up through ${chatName(convo)}`, async ({ page, browser }) => {
await seedChat(convo);
const size = await openChat(page, convo);
if (EXTRA_CSS) {
await page.addStyleTag({ content: EXTRA_CSS });
}
const session = await page.context().newCDPSession(page);
if (CPU_RATE > 1) {
await session.send('Emulation.setCPUThrottlingRate', { rate: CPU_RATE });
}
const skipped = () =>
page.evaluate(() => document.querySelectorAll('.message-render[data-offscreen]').length);
console.log(
`\n${chatName(convo)}: ${size.rows} rows, ${size.nodes} elements, ${await skipped()} rows skipped`,
);
const sweep = (direction: 1 | -1) => async () => {
await page.evaluate(
([pages, wait, sign]) =>
new Promise<void>((resolve) => {
const scroller = document.getElementById('messages-end')?.parentElement
?.parentElement as HTMLElement;
let step = 0;
const timer = window.setInterval(() => {
scroller.scrollBy(0, sign * scroller.clientHeight * 0.9);
step += 1;
if (step >= pages) {
window.clearInterval(timer);
resolve();
}
}, wait);
}),
[PAGES, PAGE_MS, direction] as const,
);
};
const up = await traceFrames(browser, page, sweep(-1));
console.log(formatFrameCost(`${PAGES} pages up, cpu x${CPU_RATE}`, up));
const down = await traceFrames(browser, page, sweep(1));
console.log(formatFrameCost(`${PAGES} pages down, cpu x${CPU_RATE}`, down));
});
}
});