diff --git a/packages/webui/src/components/pages/logs-page.tsx b/packages/webui/src/components/pages/logs-page.tsx index 3978eccf..03e0e1f0 100644 --- a/packages/webui/src/components/pages/logs-page.tsx +++ b/packages/webui/src/components/pages/logs-page.tsx @@ -141,9 +141,27 @@ export function LogsPage() { }, [api, serverLevel, levelBusy]); useEffect(() => { - return api.logs.stream({ + // Batch incoming lines: buffer in a ref and flush on a ~80ms timer instead + // of a per-line setState. Under a high log rate this turns N O(n) array + // copies + N React commits per window into one. Dedup-by-id and the + // maxLines cap are preserved (a reconnect can replay recent ids). + let pending: LogEntry[] = []; + let flushTimer: number | null = null; + const flush = () => { + flushTimer = null; + if (pending.length === 0) return; + const batchMap = new Map(pending.map((e) => [e.id, e] as const)); + pending = []; + setLogs((prev) => { + const kept = prev.length ? prev.filter((it) => !batchMap.has(it.id)) : prev; + const merged = kept.length ? [...kept, ...batchMap.values()] : [...batchMap.values()]; + return merged.length > maxLinesRef.current ? merged.slice(-maxLinesRef.current) : merged; + }); + }; + const dispose = api.logs.stream({ onLine: (entry) => { - setLogs((prev) => [...prev.filter((it) => it.id !== entry.id), entry].slice(-maxLinesRef.current)); + pending.push(entry); + if (flushTimer == null) flushTimer = window.setTimeout(flush, 80); }, onStatus: (s) => { if (s === 'open') setStreamStatus('实时'); @@ -151,6 +169,10 @@ export function LogsPage() { else setStreamStatus('已断开'); }, }); + return () => { + dispose(); + if (flushTimer != null) clearTimeout(flushTimer); + }; }, [api]); useEffect(() => {