mirror of
https://github.com/danny-avila/LibreChat.git
synced 2026-11-18 14:08:21 +00:00
* 🪟 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.
168 lines
6.4 KiB
TypeScript
168 lines
6.4 KiB
TypeScript
import { randomUUID } from 'crypto';
|
|
import { expect, test } from '@playwright/test';
|
|
import type { Page } from '@playwright/test';
|
|
import {
|
|
deleteConversations,
|
|
deleteMessagesByConversation,
|
|
seedConversations,
|
|
seedMessages,
|
|
} from '../db';
|
|
import { getE2EUser } from '../../../setup/user';
|
|
|
|
/**
|
|
* Rows more than a screen from the thread's viewport are skipped by the browser, so a change
|
|
* that touches the whole thread costs only the rows near the reader. The thread must be the
|
|
* same thread either way: the same height, with every row reachable.
|
|
*/
|
|
|
|
const TURNS = 80;
|
|
const turnText = (index: number) =>
|
|
/** Uneven heights, none of them the fallback a never-measured row would get. */
|
|
[`Turn ${index + 1} of the skipped thread`, ...Array(index % 5).fill('Another line.')].join(
|
|
'\n\n',
|
|
);
|
|
|
|
async function seedThread(): Promise<string> {
|
|
const conversationId = randomUUID();
|
|
const { email } = getE2EUser();
|
|
await seedConversations(email, [
|
|
{ conversationId, title: 'Skipped rows', updatedAt: new Date() },
|
|
]);
|
|
let parentMessageId = '00000000-0000-0000-0000-000000000000';
|
|
const messages = Array.from({ length: TURNS }, (_, index) => {
|
|
const messageId = randomUUID();
|
|
const isCreatedByUser = index % 2 === 0;
|
|
const message = {
|
|
messageId,
|
|
parentMessageId,
|
|
text: turnText(index),
|
|
isCreatedByUser,
|
|
sender: isCreatedByUser ? 'User' : 'Mock Provider A',
|
|
};
|
|
parentMessageId = messageId;
|
|
return message;
|
|
});
|
|
await seedMessages(email, conversationId, messages);
|
|
return conversationId;
|
|
}
|
|
|
|
/** How each row stands: skipped or not, and whether any of it is inside the thread's viewport. */
|
|
const rowStates = (page: Page, conversationId: string) =>
|
|
page.evaluate((id) => {
|
|
const scroller = document.getElementById('messages-end')?.parentElement?.parentElement;
|
|
if (!scroller) {
|
|
throw new Error('thread not mounted');
|
|
}
|
|
const view = scroller.getBoundingClientRect();
|
|
/** The sidebar's link to this conversation carries the same attribute as the thread. */
|
|
const rows = scroller.querySelectorAll<HTMLElement>(
|
|
`[data-conversation-id="${id}"] .message-render`,
|
|
);
|
|
return Array.from(rows, (row) => {
|
|
const box = row.getBoundingClientRect();
|
|
return {
|
|
skipped: row.hasAttribute('data-offscreen'),
|
|
contentVisibility: getComputedStyle(row).contentVisibility,
|
|
inView: box.bottom > view.top && box.top < view.bottom,
|
|
screensAway:
|
|
Math.max(view.top - box.bottom, box.top - view.bottom, 0) / scroller.clientHeight,
|
|
};
|
|
});
|
|
}, conversationId);
|
|
|
|
test.describe('rows far from the viewport', () => {
|
|
let conversationId: string;
|
|
|
|
test.beforeEach(async () => {
|
|
conversationId = await seedThread();
|
|
});
|
|
|
|
test.afterEach(async () => {
|
|
await deleteMessagesByConversation([conversationId]);
|
|
await deleteConversations([conversationId]);
|
|
});
|
|
|
|
test('are skipped, and the rows a reader can see never are @scenario:rows-far-from-the-viewport-are-skipped', async ({
|
|
page,
|
|
}) => {
|
|
await page.goto(`/c/${conversationId}`);
|
|
const thread = page.locator(`[data-conversation-id="${conversationId}"]`);
|
|
await expect(thread.locator('.message-render')).toHaveCount(TURNS);
|
|
await expect(thread.locator('.message-render[data-offscreen]').first()).toBeAttached();
|
|
|
|
const rows = await rowStates(page, conversationId);
|
|
for (const row of rows) {
|
|
expect(row.contentVisibility).toBe(row.skipped ? 'auto' : 'visible');
|
|
if (row.inView) {
|
|
expect(row.skipped).toBe(false);
|
|
}
|
|
/** The margin is one screen, so a row two screens off is always past it. */
|
|
if (row.screensAway > 2) {
|
|
expect(row.skipped).toBe(true);
|
|
}
|
|
}
|
|
expect(rows.filter((row) => row.skipped).length).toBeGreaterThan(TURNS / 2);
|
|
});
|
|
|
|
test('keep the height they were laid out at, so the thread is as tall as with nothing skipped @scenario:skipped-rows-keep-their-height', async ({
|
|
page,
|
|
}) => {
|
|
await page.goto(`/c/${conversationId}`);
|
|
const thread = page.locator(`[data-conversation-id="${conversationId}"]`);
|
|
await expect(thread.locator('.message-render')).toHaveCount(TURNS);
|
|
await expect(thread.locator('.message-render[data-offscreen]').first()).toBeAttached();
|
|
|
|
const heights = await page.evaluate((id) => {
|
|
const rows = Array.from(
|
|
document.querySelectorAll<HTMLElement>(`[data-conversation-id="${id}"] .message-render`),
|
|
);
|
|
const scroller = document.getElementById('messages-end')?.parentElement?.parentElement;
|
|
const read = () => ({
|
|
thread: scroller?.scrollHeight ?? 0,
|
|
rows: rows.map((row) => row.offsetHeight),
|
|
});
|
|
const skipping = read();
|
|
const skipped = rows.filter((row) => row.hasAttribute('data-offscreen'));
|
|
for (const row of skipped) {
|
|
row.removeAttribute('data-offscreen');
|
|
}
|
|
const laidOut = read();
|
|
for (const row of skipped) {
|
|
row.setAttribute('data-offscreen', '');
|
|
}
|
|
return { skipping, laidOut, skipped: skipped.length };
|
|
}, conversationId);
|
|
|
|
expect(heights.skipped).toBeGreaterThan(0);
|
|
expect(heights.skipping.rows).toEqual(heights.laidOut.rows);
|
|
expect(heights.skipping.thread).toBe(heights.laidOut.thread);
|
|
/** Uneven on purpose: equal heights would pass with every row at its fallback. */
|
|
expect(new Set(heights.laidOut.rows).size).toBeGreaterThan(2);
|
|
});
|
|
|
|
test('are laid out again by the time a scroll brings them into view @scenario:skipped-rows-return-on-scroll', async ({
|
|
page,
|
|
}) => {
|
|
await page.goto(`/c/${conversationId}`);
|
|
const thread = page.locator(`[data-conversation-id="${conversationId}"]`);
|
|
await expect(thread.locator('.message-render')).toHaveCount(TURNS);
|
|
const first = thread.locator('.message-render').first();
|
|
const last = thread.locator('.message-render').last();
|
|
await expect(first).toHaveAttribute('data-offscreen', '');
|
|
await expect(last).not.toHaveAttribute('data-offscreen');
|
|
|
|
await thread
|
|
.getByText('Turn 1 of the skipped thread', { exact: true })
|
|
.scrollIntoViewIfNeeded();
|
|
|
|
await expect(first).not.toHaveAttribute('data-offscreen');
|
|
await expect(first).toBeInViewport();
|
|
await expect(first).toHaveCSS('content-visibility', 'visible');
|
|
await expect(last).toHaveAttribute('data-offscreen', '');
|
|
for (const row of await rowStates(page, conversationId)) {
|
|
if (row.inView) {
|
|
expect(row.skipped).toBe(false);
|
|
}
|
|
}
|
|
});
|
|
});
|