mirror of
https://github.com/clash-verge-rev/clash-verge-rev.git
synced 2026-11-19 18:32:10 +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)
|
||||
|
||||
Reference in New Issue
Block a user