LibreChat/e2e/benchmarks-fade/sidebar.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

92 lines
3.7 KiB
TypeScript

import { test } from '@playwright/test';
import { formatFrameCost, profileScript, traceFrames } from './trace';
import { CONVOS, chatName, openChat, removeChats, seedChat } from './chats';
import { installRenderProbe } from './why';
/**
* What opening and closing the sidebar costs a long thread.
*
* The sidebar sits beside the chat, so anything that changes its width changes the width the
* thread is laid out in. Collapsing it and expanding it are each traced on their own, from the
* click to a second after, with the long thread open: layout time and the longest task say
* whether the thread was laid out once or once per frame of the slide.
*/
test.use({ trace: 'off', video: 'off' });
const SETTLE_MS = 1_000;
/** `FADE_PERF_CPU=4` slows the renderer fourfold, the way DevTools throttling does. */
const CPU_RATE = Number(process.env.FADE_PERF_CPU) || 1;
const PROFILE = process.env.FADE_PERF_PROFILE === '1';
/** A stylesheet laid over the shipped one, to try a remedy on the same build. */
const EXTRA_CSS = process.env.FADE_PERF_CSS;
/** `FADE_PERF_WHY=1` counts the components that render for each toggle, with their causes. */
const WHY = process.env.FADE_PERF_WHY === '1';
test.describe('sidebar toggle beside a long thread', () => {
test.skip(CONVOS.length === 0, 'FADE_PERF_CHATS names no exported conversation');
test.afterAll(removeChats);
for (const convo of CONVOS) {
test(`toggle the sidebar beside ${chatName(convo)}`, async ({ page, browser }) => {
await seedChat(convo);
const probe = WHY ? await installRenderProbe(page) : null;
const size = await openChat(page, convo);
const skipped = await page.evaluate(
() => document.querySelectorAll('[data-offscreen]').length,
);
console.log(
`\n${chatName(convo)}: ${size.rows} rows, ${size.nodes} elements, ${skipped} rows skipped`,
);
const toggle = (testId: string) => async () => {
await page.evaluate((id) => {
document.querySelector<HTMLElement>(`[data-testid="${id}"]`)?.click();
}, testId);
await page.waitForTimeout(SETTLE_MS);
};
const expanded = await page.evaluate(
() => document.querySelector('[data-testid="close-sidebar-button"]') != null,
);
const order = expanded
? (['close-sidebar-button', 'open-sidebar-button'] as const)
: (['open-sidebar-button', 'close-sidebar-button'] as const);
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 });
}
for (let round = 1; round <= 2; round += 1) {
for (const testId of order) {
const cost = await traceFrames(browser, page, toggle(testId));
const name = testId.startsWith('close') ? 'collapse' : 'expand';
console.log(formatFrameCost(`${name} (round ${round}) cpu x${CPU_RATE}`, cost));
}
}
if (probe) {
for (const testId of order) {
const name = testId.startsWith('close') ? 'collapse' : 'expand';
await probe.start();
await toggle(testId)();
const { commits, lines } = await probe.finish(
Number(process.env.FADE_PERF_WHY_LIMIT) || 40,
);
console.log(`${name}: ${commits} commits\n${lines.join('\n')}`);
}
}
if (PROFILE) {
for (const testId of order) {
const name = testId.startsWith('close') ? 'collapse' : 'expand';
const top = await profileScript(session, toggle(testId), 25);
console.log(`${name} script self-time, cpu x${CPU_RATE}:\n${top.join('\n')}`);
}
}
});
}
});