mirror of
https://github.com/clash-verge-rev/clash-verge-rev.git
synced 2026-11-30 18:10:04 +08:00
perf: reduce traffic update allocations
This commit is contained in:
@@ -8,6 +8,26 @@ export interface IMemoryUsageItem {
|
||||
}
|
||||
|
||||
const FALLBACK_MEMORY_USAGE: IMemoryUsageItem = { inuse: 0 }
|
||||
const DUPLICATE_MEMORY_WINDOW_MS = 50
|
||||
|
||||
let lastMemorySignature = ''
|
||||
let lastMemoryTimestamp = 0
|
||||
|
||||
const shouldSkipDuplicateMemory = (memory: IMemoryUsageItem) => {
|
||||
const now = Date.now()
|
||||
const signature = `${memory.inuse}:${memory.oslimit ?? ''}`
|
||||
|
||||
if (
|
||||
signature === lastMemorySignature &&
|
||||
now - lastMemoryTimestamp <= DUPLICATE_MEMORY_WINDOW_MS
|
||||
) {
|
||||
return true
|
||||
}
|
||||
|
||||
lastMemorySignature = signature
|
||||
lastMemoryTimestamp = now
|
||||
return false
|
||||
}
|
||||
|
||||
export const useMemoryData = () => {
|
||||
const { response, refresh } = useMihomoWsSubscription<IMemoryUsageItem>({
|
||||
@@ -26,6 +46,9 @@ export const useMemoryData = () => {
|
||||
|
||||
try {
|
||||
const parsed = JSON.parse(data) as IMemoryUsageItem
|
||||
if (shouldSkipDuplicateMemory(parsed)) {
|
||||
return
|
||||
}
|
||||
next(null, parsed)
|
||||
} catch (error) {
|
||||
next(error, FALLBACK_MEMORY_USAGE)
|
||||
|
||||
@@ -4,6 +4,26 @@ import { useMihomoWsSubscription } from './use-mihomo-ws-subscription'
|
||||
import { useTrafficMonitorEnhanced } from './use-traffic-monitor'
|
||||
|
||||
const FALLBACK_TRAFFIC: Traffic = { up: 0, down: 0 }
|
||||
const DUPLICATE_TRAFFIC_WINDOW_MS = 50
|
||||
|
||||
let lastTrafficSignature = ''
|
||||
let lastTrafficTimestamp = 0
|
||||
|
||||
const shouldSkipDuplicateTraffic = (traffic: Traffic) => {
|
||||
const now = Date.now()
|
||||
const signature = `${traffic.up}:${traffic.down}`
|
||||
|
||||
if (
|
||||
signature === lastTrafficSignature &&
|
||||
now - lastTrafficTimestamp <= DUPLICATE_TRAFFIC_WINDOW_MS
|
||||
) {
|
||||
return true
|
||||
}
|
||||
|
||||
lastTrafficSignature = signature
|
||||
lastTrafficTimestamp = now
|
||||
return false
|
||||
}
|
||||
|
||||
export const useTrafficData = (options?: { enabled?: boolean }) => {
|
||||
const enabled = options?.enabled ?? true
|
||||
@@ -27,6 +47,9 @@ export const useTrafficData = (options?: { enabled?: boolean }) => {
|
||||
|
||||
try {
|
||||
const parsed = JSON.parse(data) as Traffic
|
||||
if (shouldSkipDuplicateTraffic(parsed)) {
|
||||
return
|
||||
}
|
||||
appendData(parsed)
|
||||
next(null, parsed)
|
||||
} catch (error) {
|
||||
|
||||
Reference in New Issue
Block a user