mirror of
https://github.com/getpaseo/paseo.git
synced 2026-10-30 21:23:04 +00:00
* perf(app): stop re-walking the timeline on every streamed delta Every coalesced delta rebuilds the stream render model with the same tail. The model re-sliced the tail at the history-window start on every call, so the rendered tail was a fresh array per delta and every WeakMap keyed on it (ordering, history split, turn timing, the layout history cache, row revision) missed each time. Cache the slice by tail identity and window start. Turn timing then still walked the whole rendered tail per delta and allocated a new assistant-timing Map, which the layout memo depends on. Split the walk: the tail is walked once per tail identity and the head is walked from the carried open turn per delta. The Map keeps its identity while the head adds no completed turn. Adds identity-contract tests and a vitest bench for the per-delta pipeline. 200 deltas over a 2,000-item timeline, mean per run: default mounted window 5.68ms -> 2.82ms, history fully revealed 16.95ms -> 1.81ms. Fixes #4816 Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> * perf(app): keep the stream view's per-delta work bounded by the head The chat outline concatenated tail and head, reduced over the result in the render body, and rebuilt jumpToPrompt on every delta even when the outline was disabled, which is every native viewport. Memoize the tail's contribution, scan only the head per delta, do nothing while disabled, make jumpToPrompt a stable event so the rail's memo holds, and republish the active prompt only when the index or reading position changes. The view built a Set of every mounted id plus the live head per delta for the outline's pending-jump check; key it on mounted history only and treat live head rows as always mounted. Split plugin projection so a head change does not re-project the tail, memoize the history-window boundary lookup, and read the turn presentation's submission flag with some() instead of allocating a filtered array. Refs #4816 Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> * test(app): confirm full history before measuring streaming --------- Co-authored-by: Ivan Malison <IvanMalison@gmail.com> Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com> Co-authored-by: paseo-bot[bot] <266920839+paseo-bot[bot]@users.noreply.github.com>
206 lines
6.3 KiB
TypeScript
206 lines
6.3 KiB
TypeScript
import type { ReactNode } from "react";
|
|
import { deriveStreamTurnTiming, type StreamTurnTiming } from "@/timeline/turn-time";
|
|
import type { StreamItem } from "@/types/stream";
|
|
import { findMountedWindowStart, getMountedRecentStreamItems } from "./history-window";
|
|
import { getWebPartialVirtualizationThreshold } from "./web-virtualization";
|
|
import { orderHeadForStreamRenderStrategy, orderTailForStreamRenderStrategy } from "./strategy";
|
|
import { resolveStreamRenderStrategy } from "./strategy-resolver";
|
|
|
|
export interface StreamRenderSegments {
|
|
historyVirtualized: StreamItem[];
|
|
historyMounted: StreamItem[];
|
|
liveHead: StreamItem[];
|
|
}
|
|
|
|
export interface StreamHistoryBoundary {
|
|
hasVirtualizedHistory: boolean;
|
|
hasMountedHistory: boolean;
|
|
hasLiveHead: boolean;
|
|
}
|
|
|
|
export interface StreamRenderAuxiliary {
|
|
pendingPermissions: ReactNode;
|
|
turnFooter: ReactNode;
|
|
}
|
|
|
|
export interface AgentStreamRenderModel {
|
|
history: StreamItem[];
|
|
segments: StreamRenderSegments;
|
|
turnTiming: StreamTurnTiming;
|
|
boundary: StreamHistoryBoundary;
|
|
auxiliary: StreamRenderAuxiliary;
|
|
}
|
|
|
|
export interface BuildAgentStreamRenderModelInput {
|
|
isTurnActive: boolean;
|
|
activeTurnStartedAt: Date | null;
|
|
tail: StreamItem[];
|
|
head: StreamItem[];
|
|
platform: "web" | "native";
|
|
isMobileBreakpoint: boolean;
|
|
historyStart?: number;
|
|
}
|
|
|
|
const EMPTY_STREAM_ITEMS: StreamItem[] = [];
|
|
const EMPTY_AUXILIARY: StreamRenderAuxiliary = {
|
|
pendingPermissions: null,
|
|
turnFooter: null,
|
|
};
|
|
|
|
const orderedTailCache = new WeakMap<StreamItem[], Map<string, StreamItem[]>>();
|
|
const orderedHeadCache = new WeakMap<StreamItem[], Map<string, StreamItem[]>>();
|
|
const splitHistoryCache = new WeakMap<
|
|
StreamItem[],
|
|
Map<string, Pick<AgentStreamRenderModel, "history" | "segments">>
|
|
>();
|
|
// A streamed delta rebuilds the model with the same tail. Everything below is keyed on
|
|
// the rendered tail's identity, so the slice has to be reused, not recomputed.
|
|
const renderedTailCache = new WeakMap<
|
|
StreamItem[],
|
|
{ historyStart: number; rendered: StreamItem[] }
|
|
>();
|
|
|
|
function getRenderedTail(tail: StreamItem[], historyStart: number | undefined): StreamItem[] {
|
|
if (!historyStart) {
|
|
renderedTailCache.delete(tail);
|
|
return tail;
|
|
}
|
|
const cached = renderedTailCache.get(tail);
|
|
if (cached?.historyStart === historyStart) {
|
|
return cached.rendered;
|
|
}
|
|
const rendered = tail.slice(historyStart);
|
|
// Revealing older history replaces the previous window, allowing its arrays and
|
|
// downstream layouts to be collected while the unchanged source tail stays alive.
|
|
renderedTailCache.set(tail, { historyStart, rendered });
|
|
return rendered;
|
|
}
|
|
|
|
function getOrderedItems(params: {
|
|
cache: WeakMap<StreamItem[], Map<string, StreamItem[]>>;
|
|
source: StreamItem[];
|
|
cacheKey: string;
|
|
order: (items: StreamItem[]) => StreamItem[];
|
|
}): StreamItem[] {
|
|
const { cache, source, cacheKey, order } = params;
|
|
let cachedByKey = cache.get(source);
|
|
if (!cachedByKey) {
|
|
cachedByKey = new Map();
|
|
cache.set(source, cachedByKey);
|
|
}
|
|
const cached = cachedByKey.get(cacheKey);
|
|
if (cached) {
|
|
return cached;
|
|
}
|
|
const ordered = order(source);
|
|
cachedByKey.set(cacheKey, ordered);
|
|
return ordered;
|
|
}
|
|
|
|
function splitOrderedTail(params: {
|
|
orderedTail: StreamItem[];
|
|
platform: "web" | "native";
|
|
isMobileBreakpoint: boolean;
|
|
}): Pick<AgentStreamRenderModel, "history" | "segments"> {
|
|
const { orderedTail, platform, isMobileBreakpoint } = params;
|
|
const shouldSplitHistory =
|
|
platform === "web" &&
|
|
!isMobileBreakpoint &&
|
|
orderedTail.length > getWebPartialVirtualizationThreshold();
|
|
const cacheKey = `${platform}:${isMobileBreakpoint}:${getMountedRecentStreamItems()}:${shouldSplitHistory}`;
|
|
let cachedByKey = splitHistoryCache.get(orderedTail);
|
|
if (!cachedByKey) {
|
|
cachedByKey = new Map();
|
|
splitHistoryCache.set(orderedTail, cachedByKey);
|
|
}
|
|
const cached = cachedByKey.get(cacheKey);
|
|
if (cached) {
|
|
return cached;
|
|
}
|
|
|
|
if (!shouldSplitHistory) {
|
|
const unsplit = {
|
|
history: orderedTail,
|
|
segments: {
|
|
historyVirtualized: EMPTY_STREAM_ITEMS,
|
|
historyMounted: orderedTail,
|
|
liveHead: EMPTY_STREAM_ITEMS,
|
|
},
|
|
} satisfies Pick<AgentStreamRenderModel, "history" | "segments">;
|
|
cachedByKey.set(cacheKey, unsplit);
|
|
return unsplit;
|
|
}
|
|
|
|
const mountedWindowStart = findMountedWindowStart({
|
|
items: orderedTail,
|
|
minMountedCount: getMountedRecentStreamItems(),
|
|
});
|
|
const split = {
|
|
history: orderedTail,
|
|
segments: {
|
|
historyVirtualized: orderedTail.slice(0, mountedWindowStart),
|
|
historyMounted: orderedTail.slice(mountedWindowStart),
|
|
liveHead: EMPTY_STREAM_ITEMS,
|
|
},
|
|
} satisfies Pick<AgentStreamRenderModel, "history" | "segments">;
|
|
cachedByKey.set(cacheKey, split);
|
|
return split;
|
|
}
|
|
|
|
export function buildAgentStreamRenderModel(
|
|
input: BuildAgentStreamRenderModelInput,
|
|
): AgentStreamRenderModel {
|
|
const strategy = resolveStreamRenderStrategy({
|
|
platform: input.platform === "web" ? "web" : "native",
|
|
isMobileBreakpoint: input.isMobileBreakpoint,
|
|
});
|
|
const orderingCacheKey = `${input.platform}:${input.isMobileBreakpoint}`;
|
|
const renderedTail = getRenderedTail(input.tail, input.historyStart);
|
|
const orderedTail = getOrderedItems({
|
|
cache: orderedTailCache,
|
|
source: renderedTail,
|
|
cacheKey: orderingCacheKey,
|
|
order: (items) =>
|
|
orderTailForStreamRenderStrategy({
|
|
strategy,
|
|
streamItems: items,
|
|
}),
|
|
});
|
|
const orderedHead = getOrderedItems({
|
|
cache: orderedHeadCache,
|
|
source: input.head,
|
|
cacheKey: orderingCacheKey,
|
|
order: (items) =>
|
|
orderHeadForStreamRenderStrategy({
|
|
strategy,
|
|
streamHead: items,
|
|
}),
|
|
});
|
|
const splitHistory = splitOrderedTail({
|
|
orderedTail,
|
|
platform: input.platform,
|
|
isMobileBreakpoint: input.isMobileBreakpoint,
|
|
});
|
|
const turnTiming = deriveStreamTurnTiming({
|
|
isTurnActive: input.isTurnActive,
|
|
activeTurnStartedAt: input.activeTurnStartedAt,
|
|
tail: renderedTail,
|
|
head: input.head,
|
|
});
|
|
|
|
return {
|
|
history: splitHistory.history,
|
|
segments: {
|
|
...splitHistory.segments,
|
|
liveHead: orderedHead,
|
|
},
|
|
turnTiming,
|
|
boundary: {
|
|
hasVirtualizedHistory: splitHistory.segments.historyVirtualized.length > 0,
|
|
hasMountedHistory: splitHistory.segments.historyMounted.length > 0,
|
|
hasLiveHead: orderedHead.length > 0,
|
|
},
|
|
auxiliary: EMPTY_AUXILIARY,
|
|
};
|
|
}
|