mirror of
https://github.com/clash-verge-rev/clash-verge-rev.git
synced 2026-11-24 14:32:13 +08:00
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:
@@ -158,9 +158,11 @@ const EMPTY_DATA: ITrafficDataPoint[] = []
|
||||
export const useTrafficMonitorEnhanced = (options?: {
|
||||
subscribe?: boolean
|
||||
enabled?: boolean
|
||||
onSnapshot?: (message: ITrafficWorkerSnapshotMessage) => void
|
||||
}) => {
|
||||
const subscribeToSnapshots = options?.subscribe ?? true
|
||||
const enabled = options?.enabled ?? true
|
||||
const onSnapshot = options?.onSnapshot
|
||||
const isVisible = useVisibility()
|
||||
const [latestSnapshot, setLatestSnapshot] = useState<{
|
||||
dataPoints: ITrafficDataPoint[]
|
||||
@@ -174,6 +176,8 @@ export const useTrafficMonitorEnhanced = (options?: {
|
||||
const currentRangeRef = useRef<number>(WORKER_CONFIG.defaultRangeMinutes)
|
||||
const isVisibleRef = useRef(isVisible)
|
||||
isVisibleRef.current = isVisible
|
||||
const onSnapshotRef = useRef(onSnapshot)
|
||||
onSnapshotRef.current = onSnapshot
|
||||
|
||||
// 注册引用计数与Worker生命周期
|
||||
useEffect(() => {
|
||||
@@ -186,9 +190,12 @@ export const useTrafficMonitorEnhanced = (options?: {
|
||||
client.start(currentRangeRef.current)
|
||||
|
||||
let unsubscribe: (() => void) | undefined
|
||||
if (subscribeToSnapshots) {
|
||||
if (subscribeToSnapshots || onSnapshot) {
|
||||
unsubscribe = client.onSnapshot((message) => {
|
||||
if (!isVisibleRef.current) return
|
||||
// onSnapshot is the always-live channel (ignores visibility); the
|
||||
// state mirror below keeps its original always-latest contract.
|
||||
onSnapshotRef.current?.(message)
|
||||
if (!subscribeToSnapshots || !isVisibleRef.current) return
|
||||
setLatestSnapshot({
|
||||
dataPoints: message.dataPoints,
|
||||
samplerStats: message.samplerStats,
|
||||
@@ -203,14 +210,14 @@ export const useTrafficMonitorEnhanced = (options?: {
|
||||
client.stop()
|
||||
}
|
||||
}
|
||||
}, [enabled, subscribeToSnapshots])
|
||||
}, [enabled, subscribeToSnapshots, onSnapshot])
|
||||
|
||||
// Re-prime snapshot ingestion after every visible transition
|
||||
useEffect(() => {
|
||||
if (enabled && subscribeToSnapshots && isVisible) {
|
||||
if (enabled && isVisible && (subscribeToSnapshots || onSnapshot)) {
|
||||
clientRef.current?.requestSnapshot()
|
||||
}
|
||||
}, [enabled, subscribeToSnapshots, isVisible])
|
||||
}, [enabled, subscribeToSnapshots, onSnapshot, isVisible])
|
||||
|
||||
// 添加流量数据
|
||||
const appendData = useCallback(
|
||||
@@ -244,8 +251,12 @@ export const useTrafficMonitorEnhanced = (options?: {
|
||||
/**
|
||||
* 图表数据Hook
|
||||
*/
|
||||
export const useTrafficGraphDataEnhanced = () => {
|
||||
const { graphData, samplerStats } = useTrafficMonitorEnhanced()
|
||||
export const useTrafficGraphDataEnhanced = (options?: {
|
||||
subscribe?: boolean
|
||||
enabled?: boolean
|
||||
onSnapshot?: (message: ITrafficWorkerSnapshotMessage) => void
|
||||
}) => {
|
||||
const { graphData, samplerStats } = useTrafficMonitorEnhanced(options)
|
||||
|
||||
return {
|
||||
...graphData,
|
||||
|
||||
Reference in New Issue
Block a user