perf(ui): remove per-second allocations in traffic, log and proxy render paths (#8689)

* perf(home): draw the traffic graph from refs instead of per-snapshot state

Worker snapshots re-rendered the whole graph tree per update and the
sampler sliced its full buffer every second just to compare for
equality. Snapshots now feed display data and the y-scale through refs
(React state only tracks the diagnostics badge inputs, skipped while
hidden), the draw callback chain is identity-stable, per-draw axis
ticks/labels and gradient strings go through pooled buffers and bounded
caches, and the sampler version counter plus an oldest-point age check
let the idle poll skip unchanged slices.

* perf(ui): hoist static icons and style identities in traffic widgets

Stat-card icons were rebuilt per parsed-data update, defeating the
memoized card comparison, and the layout traffic widget reallocated its
style objects (with a redundant double sx spread) on every tick.

* perf(log): compile the highlight regex once per search state

Each visible row recompiled (and re-escaped) the same regex three times
per render while logs stream in; the compiled regex is now cached per
search state identity.

* 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.

* test(perf): add proxy scroll scene to the perf adapter

Adds a proxy page scenario to the measurement adapter: a core-module
shim serves synthetic proxy/clash/run-state data at the invoke seam
(active only for the proxy scenario; other scenarios stay
byte-identical), and a sawtooth scroll driver keeps the virtualizer
measuring and the throttled scroll-position save running. Running the
scene additionally requires the harness-side scenario contract and
schema v10 from the clash-verge-rev-harness repository.

* chore(changelog): drop the hot-path refresh entries

* test(perf): move the proxy adapter scene to a separate change
This commit is contained in:
Tunglies
2026-11-23 07:45:22 +00:00
committed by GitHub
parent 34103b3eba
commit e4abdb8c0d
8 changed files with 527 additions and 407 deletions
+31 -16
View File
@@ -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)