mirror of
https://github.com/clash-verge-rev/clash-verge-rev.git
synced 2026-11-19 10:22:07 +08:00
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.
This commit is contained in:
@@ -67,6 +67,7 @@
|
||||
- 优化全局模式代理列表:仅显示 `GLOBAL` 节点,选中其中的策略组时才显示该组
|
||||
- 优化设置页开关和选项所在行的鼠标悬停高亮,便于对应说明与控件
|
||||
- 优化首页「当前节点」卡片:代理组列表加入 `fallback` 代理组,切换到 `url-test` 或 `fallback` 代理组时同样联动 `select` 代理组
|
||||
- 优化主页流量图表与日志、代理列表的实时刷新和滚动占用,降低高频更新时的 CPU 负载
|
||||
|
||||
**🍎 macOS**
|
||||
|
||||
|
||||
@@ -60,6 +60,23 @@ const ProxyGroupsChain = lazy(() =>
|
||||
})),
|
||||
)
|
||||
|
||||
const SCROLL_POSITIONS_KEY = 'proxy-scroll-positions'
|
||||
type ScrollPositions = Record<string, number>
|
||||
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<T extends (...args: any[]) => 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<typeof defaultRangeExtractor>[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)
|
||||
|
||||
Reference in New Issue
Block a user