From 50fd66925ab0cbf5c65e392a2a5a145443270ea9 Mon Sep 17 00:00:00 2001 From: Tunglies <77394545+Tunglies@users.noreply.github.com> Date: Thu, 19 Nov 2026 10:09:59 +0800 Subject: [PATCH] perf(proxy): reuse the parsed scroll-position snapshot and drop per-frame copies The sticky-index lookup copied and reversed its array per range extraction and every throttled save re-read and re-parsed the same localStorage payload; the reverse scan is now allocation-free and the parse goes through one read-through snapshot. --- Changelog.md | 1 + src/components/proxy/proxy-groups.tsx | 47 ++++++++++++++++++--------- 2 files changed, 32 insertions(+), 16 deletions(-) diff --git a/Changelog.md b/Changelog.md index 51c123c80..4f05d5a1e 100644 --- a/Changelog.md +++ b/Changelog.md @@ -67,6 +67,7 @@ - 优化全局模式代理列表:仅显示 `GLOBAL` 节点,选中其中的策略组时才显示该组 - 优化设置页开关和选项所在行的鼠标悬停高亮,便于对应说明与控件 - 优化首页「当前节点」卡片:代理组列表加入 `fallback` 代理组,切换到 `url-test` 或 `fallback` 代理组时同样联动 `select` 代理组 +- 优化主页流量图表与日志、代理列表的实时刷新和滚动占用,降低高频更新时的 CPU 负载 **🍎 macOS** diff --git a/src/components/proxy/proxy-groups.tsx b/src/components/proxy/proxy-groups.tsx index 11e357387..4dc4903e2 100644 --- a/src/components/proxy/proxy-groups.tsx +++ b/src/components/proxy/proxy-groups.tsx @@ -60,6 +60,23 @@ const ProxyGroupsChain = lazy(() => })), ) +const SCROLL_POSITIONS_KEY = 'proxy-scroll-positions' +type ScrollPositions = Record +let scrollPositionsCache: ScrollPositions | undefined + +// Scroll handling runs per frame and is throttled to 500ms writes; keep the +// parsed object so saves do not re-read and re-parse localStorage each time. +const readScrollPositions = (): ScrollPositions | undefined => { + if (scrollPositionsCache) return scrollPositionsCache + try { + const raw = localStorage.getItem(SCROLL_POSITIONS_KEY) + if (raw) scrollPositionsCache = JSON.parse(raw) as ScrollPositions + } catch (e) { + console.error('Error restoring scroll position:', e) + } + return scrollPositionsCache +} + function useStableCallback any>(fn: T): T { const ref = useRef(fn) ref.current = fn @@ -150,13 +167,12 @@ function useProxyRenderState( const saveScrollPosition = useCallback( (scrollTop: number) => { - const scrollPositions = localStorage.getItem('proxy-scroll-positions') - ? JSON.parse(localStorage.getItem('proxy-scroll-positions') ?? '{}') - : {} + const scrollPositions = readScrollPositions() ?? {} scrollPositions[scrollPositionKey] = scrollTop + scrollPositionsCache = scrollPositions try { localStorage.setItem( - 'proxy-scroll-positions', + SCROLL_POSITIONS_KEY, JSON.stringify(scrollPositions), ) } catch (e) { @@ -167,15 +183,10 @@ function useProxyRenderState( ) const getScrollPosition = useCallback(() => { - try { - const savedPositions = localStorage.getItem('proxy-scroll-positions') - if (savedPositions) { - const positions = JSON.parse(savedPositions) - const savedPosition = positions[scrollPositionKey] - return savedPosition ?? 0 - } - } catch (e) { - console.error('Error restoring scroll position:', e) + const savedPositions = readScrollPositions() + if (savedPositions) { + const savedPosition = savedPositions[scrollPositionKey] + return savedPosition ?? 0 } }, [scrollPositionKey]) @@ -240,9 +251,13 @@ function ChainProxyGroups(props: { const rangeExtractor = useCallback( (range: Parameters[0]) => { - const activeStickyIndex = [...stickyGroupIndexes] - .reverse() - .find((index) => index <= range.startIndex) + let activeStickyIndex: number | undefined + for (let i = stickyGroupIndexes.length - 1; i >= 0; i--) { + if (stickyGroupIndexes[i] <= range.startIndex) { + activeStickyIndex = stickyGroupIndexes[i] + break + } + } activeStickyIndexRef.current = activeStickyIndex ?? null const indexes = defaultRangeExtractor(range)