LibreChat/e2e/benchmarks-fade/why.ts
Danny Avila c3882c09ad
🧵 perf: Stop Re-rendering and Re-styling the Thread on Stream Events and Run Boundaries (#18148)
* 🎞️ perf: Run the In-Flight Label Sweep on the Compositor

The sweep on a running tool call, streaming reasoning and the elapsed timer
animated `background-position` under `background-clip: text`, which no
browser can hand to the compositor. Every frame of it ran style, paint and
commit on the main thread, and each paint walked the transcript around the
label, so the cost of a frame grew with the conversation and the sweep
stuttered whenever the main thread was busy.

`Shimmer` in `@librechat/client` draws the same sweep from two transforms
that cancel: a masked band slides across the label while a second copy of the
text inside it slides back, so the glyphs hold still and only the gap in the
mask moves. The copy is generated content, so the label is still one text
node. Every in-flight label renders through it.

`e2e/benchmarks-fade` measures a frame and each kind of stream event inside
an exported conversation, from a Chromium trace of the built client.

* 🧪 test: Let the Frame Benchmark Open a Chat in a Chosen Theme

* 🧵 perf: Keep the Composer, Nav Rail and Row Toolbars Out of Every Stream Event

A streamed delta rewrites one row, but each one also re-rendered the composer
form (and re-measured its textarea), the message nav rail (re-translating a
label per message), the sidebar's chat provider and the pending-steers row,
because each subscribed to the whole streaming message or the whole messages
array to read something a delta never changes.

- The chat contract and steering read a projection of the branch tail (where it
  sits, whether the run is paused) and fetch the message itself when a turn is
  built, through `useBranchTailGetter`. `ChatTurnsParams` carries
  `getLatestMessage` in place of `latestMessage`.
- The compact action and pending steers select what they use of the tail.
- The URL-prompt hook observes the history only while a submission waits on it.
- The nav rail's ribs are memoized apart from the rail.
- A row's toolbar keeps one element per glyph and stable handlers, so a send
  and its completion no longer re-render every button of every row.

`e2e/benchmarks-fade` gains a render-reason probe (`FADE_PERF_WHY`), a whole-run
profile, and windows for the send and its confirming frame.

* 🫥 perf: Conceal Rerun During a Run Instead of Unmounting It on Every Row

A run starting or ending withholds, then restores, the rerun control on every
row of the thread at once. Unmounting and remounting a button on each row made
the browser re-style the whole transcript both times: the stylesheet holds
`:has(:disabled)` rules and a message row is a `:has()` anchor, so a button
entering, leaving or flipping `disabled` inside a row invalidates that row's
entire subtree. In a 250-message thread that was 80 to 100 ms inside the send
keypress and again inside the final frame.

The control now stays mounted for as long as the row has a rerun to offer and
is concealed by visibility while a run is in flight: no focus, no pointer, no
place in the accessibility tree, and no layout where the toolbar starts at the
row's edge. Below `lg`, where the toolbar is centered, it leaves the flow as
before. It is deliberately not also `disabled`. The reply being written still
mounts no controls at all.

* 🎯 perf: Write :has() Rules So a Change in a Row Does Not Re-style the Row

Two selector shapes in the stylesheet left the browser unable to tell which
elements a `:has()` rule could reach, so it re-styled the whole subtree of
every `:has()` anchor above any change:

- Tailwind's `group-has-[…]`, which compiles to
  `.x:is(:where(.group):has(…) *)`. Message rows and their toolbars are
  `group`s, so a button mounting or unmounting in a row re-styled the row.
- `theme-disabled-within:[&_*]:…`, which compiles to `…:has(:disabled) *`.
  With it in the sheet, a `disabled` flip did the same.

Either one alone was enough: flipping `disabled` on one button per row of a
250-message thread took 100 to 110 ms, and mounting one took 88 ms. A run
starting or ending did one of those on every row.

The row-hover reveal, the header label crossfade and the toolbar's
hover-active hold now use `grouped-*` custom variants that emit
`:where(.group):has(…) .x`: the same elements at the same specificity, with a
subject the browser can read. A wrapper's parts take the disabled fill from
classes the component adds while it is disabled, through `theme-disabled-fill`,
instead of from `:has(:disabled) *`. The same two changes now cost 25 to 42 ms
and 27 ms.

`has-selectors.spec.ts` reads the stylesheet the app ships and fails on either
shape; `restyle.perf.spec.ts` measures the cost.

* 📱 fix: Keep a Concealed Rerun Hidden on Narrow Screens

The narrow-screen stylesheet forces every toolbar button visible from outside
any cascade layer, so it outranked the utilities that conceal a rerun while a
run is in flight: below 1024px the button stayed on screen and in the tab
order, inert. A concealed button now carries `data-concealed`, and that rule
leaves it alone.

Covered at a narrow and a wide viewport by what the reader gets: the settled
reply's rerun is hidden and cannot take focus during a run, and is back after.

* 🧮 perf: Skip the Pause Scan in Pending Steers When None Are Pending

The pending-steers row follows the conversation's messages, so it renders for
every streamed delta, and it resolved the run's pause state before checking
whether it had anything to show. That check walks every part of every message.
It now runs only with a steer to display.
2026-11-14 12:15:48 -05:00

236 lines
8.8 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import fs from 'node:fs';
import path from 'node:path';
import { TraceMap, originalPositionFor } from '@jridgewell/trace-mapping';
import type { CDPSession, Page } from '@playwright/test';
/**
* Which components rendered, how often, and what made each one render: a prop
* that changed identity, one of its own state or store hooks, a context it
* reads, or nothing of its own (its parent rendered and it is not memoized).
*
* Works on the production bundle. React reports each commit to the DevTools
* hook whether or not DevTools is there, so a stand-in installed before the
* bundle loads sees every fiber that did work. Minified component names mean
* nothing, so each component is named by where its function is defined, which
* the browser knows, and by the source map when the build wrote one.
*/
type ProbeRow = { index: number; renders: number; reasons: Record<string, number> };
declare global {
interface Window {
__FADE_WHY__?: {
enabled: boolean;
functions: unknown[];
commits: number;
report(): ProbeRow[];
reset(): void;
};
}
}
const PROBE = `(() => {
const rows = new Map();
const probe = {
enabled: false,
functions: [],
commits: 0,
reset() { rows.clear(); probe.functions.length = 0; probe.commits = 0; },
report() {
return Array.from(rows.values()).map((row) => ({
index: row.index, renders: row.renders, reasons: Object.fromEntries(row.reasons),
}));
},
};
const functionOf = (type) => {
if (typeof type === 'function') return type;
if (type && typeof type === 'object') return functionOf(type.render ?? type.type);
return null;
};
const describe = (value) => {
if (value === null || typeof value !== 'object') return typeof value;
if (Array.isArray(value)) return 'array[' + value.length + ']';
return '{' + Object.keys(value).slice(0, 6).join(',') + '}';
};
const reasonsFor = (fiber) => {
const before = fiber.alternate;
if (!before) return ['mount'];
const reasons = [];
const was = before.memoizedProps ?? {};
const now = fiber.memoizedProps ?? {};
if (was !== now) {
const keys = new Set([...Object.keys(was), ...Object.keys(now)]);
for (const key of keys) {
if (!Object.is(was[key], now[key])) reasons.push('prop ' + key);
}
}
let hook = fiber.memoizedState;
let old = before.memoizedState;
let position = 0;
while (hook && old) {
if (hook.queue != null && !Object.is(hook.memoizedState, old.memoizedState)) {
const value = hook.memoizedState;
const prior = old.memoizedState;
/** A query result is a new object on every cache write, whatever its
* reader selected. Only its data says whether the reader had to render. */
const isQuery = value && typeof value === 'object' && 'fetchStatus' in value;
if (!isQuery) {
reasons.push('state #' + position + ' ' + describe(value));
} else if (!Object.is(value.data, prior?.data)) {
reasons.push('query #' + position + ' ' + describe(value.data));
}
}
hook = hook.next; old = old.next; position += 1;
}
let read = fiber.dependencies?.firstContext;
let readBefore = before.dependencies?.firstContext;
let slot = 0;
while (read && readBefore) {
if (!Object.is(read.memoizedValue, readBefore.memoizedValue)) {
reasons.push('context #' + slot + ' ' + describe(read.memoizedValue));
}
read = read.next; readBefore = readBefore.next; slot += 1;
}
return reasons.length > 0 ? reasons : ['parent'];
};
const visit = (fiber) => {
for (let node = fiber; node; node = node.sibling) {
const fn = functionOf(node.type);
const worked = (node.flags & 1) !== 0 || node.alternate == null;
if (fn && worked) {
let row = rows.get(fn);
if (!row) {
row = { index: probe.functions.push(fn) - 1, renders: 0, reasons: new Map() };
rows.set(fn, row);
}
row.renders += 1;
for (const reason of reasonsFor(node)) {
row.reasons.set(reason, (row.reasons.get(reason) ?? 0) + 1);
}
}
/** A subtree React did not touch keeps the child it had. */
if (node.child && node.child !== node.alternate?.child) visit(node.child);
}
};
window.__FADE_WHY__ = probe;
window.__REACT_DEVTOOLS_GLOBAL_HOOK__ = {
renderers: new Map(),
supportsFiber: true,
inject(renderer) { this.renderers.set(this.renderers.size + 1, renderer); return this.renderers.size; },
onScheduleFiberRoot() {},
onCommitFiberUnmount() {},
onPostCommitFiberRoot() {},
onCommitFiberRoot(_id, root) {
if (!probe.enabled) return;
probe.commits += 1;
try { visit(root.current); } catch (_error) { /* a probe must never break the page */ }
},
};
})();`;
export type RenderProbe = {
start(): Promise<void>;
/** Stops counting and returns one line per component, most renders first. */
finish(limit?: number): Promise<{ commits: number; lines: string[] }>;
};
/**
* Installs the probe ahead of the bundle. Call before the page navigates; the
* debugger is enabled only to learn which script each function came from.
*/
export async function installRenderProbe(page: Page): Promise<RenderProbe> {
await page.addInitScript({ content: PROBE });
const session: CDPSession = await page.context().newCDPSession(page);
const scripts = new Map<string, string>();
session.on('Debugger.scriptParsed', (event) => {
if (event.url) {
scripts.set(event.scriptId, event.url);
}
});
await session.send('Debugger.enable');
const maps = new Map<string, TraceMap | null>();
const mapAt = (file: string): TraceMap | null => {
if (!maps.has(file)) {
maps.set(file, fs.existsSync(file) ? new TraceMap(fs.readFileSync(file, 'utf8')) : null);
}
return maps.get(file) ?? null;
};
const root = path.resolve(__dirname, '../..');
const nameAt = (url: string, line: number, column: number): string => {
const file = url.split('/').pop() ?? '';
const fallback = `${file}:${line + 1}:${column + 1}`;
const bundle = mapAt(path.join(root, 'client/dist/assets', `${file}.map`));
let position = bundle ? originalPositionFor(bundle, { line: line + 1, column }) : null;
if (!position?.source) {
return fallback;
}
let source = position.source;
/** The workspace packages are bundled from their own builds, each with its own map. */
const built = source.match(/packages\/(chat|client|data-provider)\/dist\/(.+)$/);
const inner = built
? mapAt(path.join(root, 'packages', built[1], 'dist', `${built[2]}.map`))
: null;
const deeper = inner
? originalPositionFor(inner, { line: position.line ?? 1, column: position.column ?? 0 })
: null;
if (built && deeper?.source) {
position = { ...deeper, name: deeper.name ?? position.name };
source = `packages/${built[1]}/${deeper.source.replace(/^(\.\.\/)+/, '')}`;
}
return `${position.name ?? '(anonymous)'} ${source.replace(/^(\.\.\/)+/, '')}:${position.line}`;
};
return {
async start() {
await page.evaluate(() => {
window.__FADE_WHY__?.reset();
if (window.__FADE_WHY__) {
window.__FADE_WHY__.enabled = true;
}
});
},
async finish(limit = 60) {
const { rows, commits } = await page.evaluate(() => {
const probe = window.__FADE_WHY__;
if (probe) {
probe.enabled = false;
}
return { rows: probe?.report() ?? [], commits: probe?.commits ?? 0 };
});
rows.sort((a, b) => b.renders - a.renders);
const lines: string[] = [];
for (const row of rows.slice(0, limit)) {
const { result } = await session.send('Runtime.evaluate', {
expression: `window.__FADE_WHY__.functions[${row.index}]`,
});
let name = '(unknown)';
if (result.objectId) {
const { internalProperties } = await session.send('Runtime.getProperties', {
objectId: result.objectId,
ownProperties: true,
});
const location = internalProperties?.find(
(entry) => entry.name === '[[FunctionLocation]]',
)?.value?.value as
| { scriptId: string; lineNumber: number; columnNumber: number }
| undefined;
if (location) {
name = nameAt(
scripts.get(location.scriptId) ?? '',
location.lineNumber,
location.columnNumber,
);
}
}
const reasons = Object.entries(row.reasons)
.sort((a, b) => b[1] - a[1])
.slice(0, 5)
.map(([reason, count]) => `${reason} ×${count}`)
.join('; ');
lines.push(`${String(row.renders).padStart(6)} ${name}\n ${reasons}`);
}
return { commits, lines };
},
};
}