From c85c510c0b5a3494e63dbe3506c8aef24661c491 Mon Sep 17 00:00:00 2001 From: DrSmoothl <1787882683@qq.com> Date: Thu, 2 Jul 2026 15:02:13 +0800 Subject: [PATCH] perf(webui): trim debug-view rendering cost (json-tree, activity, tester) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - json-tree:折叠节点不再枚举子项(改用 count),整棵树的 JSON.stringify 从每次渲染 改为点击复制时才算。 - live-activity:搜索命中串在入队时预生成,过滤不再对每行 JSON.stringify(300 行 × 每次击键)。 - action-tester:流式帧封顶 2000,长/失控流不再无限增长数组与 DOM。 均行为保持(或纯改善),对抗性 Agent 逐条核验无回归。 --- .../src/components/debug/action-tester.tsx | 5 +++- .../src/components/debug/live-activity.tsx | 8 ++++-- .../webui/src/components/ui/json-tree.tsx | 28 ++++++++++++------- 3 files changed, 27 insertions(+), 14 deletions(-) diff --git a/packages/webui/src/components/debug/action-tester.tsx b/packages/webui/src/components/debug/action-tester.tsx index a463d8ee..84e18229 100644 --- a/packages/webui/src/components/debug/action-tester.tsx +++ b/packages/webui/src/components/debug/action-tester.tsx @@ -25,6 +25,9 @@ const cardCls = 'rounded-2xl border border-border/60 bg-card/80 shadow-[0_1px_2p // positives just add one extra click; false negatives skip a confirm — both low // stakes (the amber banner already warns that any write is real). const DESTRUCTIVE_RE = /(kick|ban|mute|recall|withdraw|delete|del_|dismiss|disband|leave|quit|remove|revoke)/i; +// Cap retained stream frames so a long-running / runaway stream can't grow the +// array and DOM without bound. Generous enough to keep a full normal stream. +const MAX_STREAM_FRAMES = 2000; function qqAvatarUrl(uin: string) { return `/avatar/${encodeURIComponent(uin)}`; @@ -136,7 +139,7 @@ export function ActionTester({ accounts, docs, presetAction }: { accounts: QQInf try { await api.debug.invokeStream(uin, actionName.trim(), params, (frame) => { last = frame; - setFrames((prev) => [...prev, frame]); + setFrames((prev) => (prev.length >= MAX_STREAM_FRAMES ? [...prev.slice(-(MAX_STREAM_FRAMES - 1)), frame] : [...prev, frame])); const d = frame.data as { type?: string; progress?: number; transferred?: number; total?: number } | undefined; if (d && typeof d.progress === 'number') tasks.update(taskId, { progress: d.progress }); else if (d && typeof d.transferred === 'number' && typeof d.total === 'number' && d.total > 0) tasks.update(taskId, { progress: d.transferred / d.total }); diff --git a/packages/webui/src/components/debug/live-activity.tsx b/packages/webui/src/components/debug/live-activity.tsx index eac53d87..4ab93647 100644 --- a/packages/webui/src/components/debug/live-activity.tsx +++ b/packages/webui/src/components/debug/live-activity.tsx @@ -14,7 +14,7 @@ import type { DebugStreamMessage } from '@/types'; const STREAM_CAP = 300; const cardCls = 'rounded-2xl border border-border/60 bg-card/80 shadow-[0_1px_2px_rgb(0_0_0/0.04),0_8px_24px_-12px_rgb(0_0_0/0.10)] backdrop-blur-sm'; -interface StreamRow { id: number; at: number; msg: Extract } +interface StreamRow { id: number; at: number; msg: Extract; search: string } function IconBtn({ onClick, title, children }: { onClick: () => void; title: string; children: ReactNode }) { return ( @@ -103,7 +103,9 @@ export function LiveActivity() { (m) => { if (m.kind === 'ready' || pausedRef.current) return; setRows((prev) => { - const next = [{ id: idRef.current++, at: Date.now(), msg: m }, ...prev]; + // Precompute the search haystack once at enqueue, so filtering never + // re-stringifies every row on each keystroke. + const next = [{ id: idRef.current++, at: Date.now(), msg: m, search: JSON.stringify(m).toLowerCase() }, ...prev]; return next.length > STREAM_CAP ? next.slice(0, STREAM_CAP) : next; }); }, @@ -119,7 +121,7 @@ export function LiveActivity() { if (!q) return true; if (r.msg.kind === 'dropped') return false; const { label } = rowLabel(r.msg); - return label.toLowerCase().includes(q) || r.msg.uin.includes(q) || JSON.stringify(r.msg).toLowerCase().includes(q); + return label.toLowerCase().includes(q) || r.msg.uin.includes(q) || r.search.includes(q); }); }, [rows, kindFilter, query]); diff --git a/packages/webui/src/components/ui/json-tree.tsx b/packages/webui/src/components/ui/json-tree.tsx index 36a671f2..59922222 100644 --- a/packages/webui/src/components/ui/json-tree.tsx +++ b/packages/webui/src/components/ui/json-tree.tsx @@ -66,12 +66,18 @@ function Node({ k, value, depth, defaultOpen }: { k?: string; value: unknown; de ); } - const entries = Array.isArray(value) - ? value.map((v, i) => [String(i), v] as const) - : Object.entries(value as Record); - const openB = Array.isArray(value) ? '[' : '{'; - const closeB = Array.isArray(value) ? ']' : '}'; - const shown = open ? Math.min(entries.length, limit) : 0; + const isArr = Array.isArray(value); + const count = isArr ? (value as unknown[]).length : Object.keys(value as object).length; + const openB = isArr ? '[' : '{'; + const closeB = isArr ? ']' : '}'; + const shown = open ? Math.min(count, limit) : 0; + // Only materialise child entries when expanded — a deep tree has many + // collapsed nodes, and enumerating each on every render is wasted work. + const entries: ReadonlyArray = open + ? (isArr + ? (value as unknown[]).map((v, i) => [String(i), v] as const) + : Object.entries(value as Record)) + : []; return (
@@ -84,21 +90,21 @@ function Node({ k, value, depth, defaultOpen }: { k?: string; value: unknown; de {keyLabel} {openB} - {!open && {entries.length}{Array.isArray(value) ? '' : ' keys'}{closeB}} + {!open && {count}{isArr ? '' : ' keys'}{closeB}} {open && ( <> {entries.slice(0, shown).map(([ck, cv]) => ( ))} - {entries.length > shown && ( + {count > shown && ( )}
{closeB}
@@ -110,9 +116,11 @@ function Node({ k, value, depth, defaultOpen }: { k?: string; value: unknown; de export function JsonTree({ data, className, maxHeight = '20rem' }: { data: unknown; className?: string; maxHeight?: string }) { const [copied, setCopied] = useState(false); - const text = (() => { try { return JSON.stringify(data, null, 2); } catch { return String(data); } })(); const copy = async () => { + // Stringify lazily, only on an actual copy — not on every render (a large + // payload would otherwise re-serialise the whole tree each commit). + const text = (() => { try { return JSON.stringify(data, null, 2); } catch { return String(data); } })(); try { if (navigator.clipboard?.writeText) { await navigator.clipboard.writeText(text);