mirror of
https://github.com/clash-verge-rev/clash-verge-rev.git
synced 2026-11-20 19:12:09 +08:00
perf(connections): reduce snapshot and table allocation churn (#8613)
* perf(connections): reduce snapshot and table allocation churn Reuse unchanged connection lists, sort traffic columns without display snapshots, and suspend the relative-time clock while hidden. Keep rule rows indexed and release resize listeners on unmount. * test(connections): limit coverage to changed behavior Keep snapshot sharing, speed decay, bounded history, numeric sorting, and visibility regression guards. Remove unrelated clear-history and pending-delivery assertions. * test(connections): focus clock regression on timer cost Replace behavioral clock assertions with one resource-count check for shared, hidden, and released intervals. * test(connections): retain only allocation and formatting guards Reduce snapshot and table tests to reference reuse, notification suppression, and visible-row formatting cost. * remove useless test * no need changelog
This commit is contained in:
@@ -8,7 +8,7 @@ let timerId: number | null = null
|
||||
const listeners = new Set<RelativeTimeListener>()
|
||||
|
||||
const startTimer = () => {
|
||||
if (timerId !== null) return
|
||||
if (timerId !== null || document.hidden || listeners.size === 0) return
|
||||
|
||||
currentTime = Date.now()
|
||||
timerId = window.setInterval(() => {
|
||||
@@ -18,19 +18,34 @@ const startTimer = () => {
|
||||
}
|
||||
|
||||
const stopTimer = () => {
|
||||
if (listeners.size > 0 || timerId === null) return
|
||||
if (timerId === null) return
|
||||
|
||||
window.clearInterval(timerId)
|
||||
timerId = null
|
||||
}
|
||||
|
||||
const handleVisibilityChange = () => {
|
||||
if (document.hidden) {
|
||||
stopTimer()
|
||||
} else {
|
||||
startTimer()
|
||||
listeners.forEach((listener) => listener())
|
||||
}
|
||||
}
|
||||
|
||||
const subscribeRelativeTime = (listener: RelativeTimeListener) => {
|
||||
listeners.add(listener)
|
||||
if (listeners.size === 1) {
|
||||
document.addEventListener('visibilitychange', handleVisibilityChange)
|
||||
}
|
||||
startTimer()
|
||||
|
||||
return () => {
|
||||
listeners.delete(listener)
|
||||
stopTimer()
|
||||
if (listeners.size === 0) {
|
||||
stopTimer()
|
||||
document.removeEventListener('visibilitychange', handleVisibilityChange)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -223,6 +223,16 @@ const compareConnectionCellValue = (
|
||||
right: IConnectionsItem,
|
||||
getSnapshot: (row: IConnectionsItem) => TableRowSnapshot,
|
||||
) => {
|
||||
switch (field) {
|
||||
case 'download':
|
||||
return (left.download ?? 0) - (right.download ?? 0)
|
||||
case 'upload':
|
||||
return (left.upload ?? 0) - (right.upload ?? 0)
|
||||
case 'dlSpeed':
|
||||
return (left.curDownload ?? 0) - (right.curDownload ?? 0)
|
||||
case 'ulSpeed':
|
||||
return (left.curUpload ?? 0) - (right.curUpload ?? 0)
|
||||
}
|
||||
const leftValue = getConnectionCellValue(field, getSnapshot(left))
|
||||
const rightValue = getConnectionCellValue(field, getSnapshot(right))
|
||||
|
||||
@@ -491,6 +501,8 @@ export const ConnectionTable = (props: Props) => {
|
||||
const [sorting, setSorting] = useState<SortingState | null>(null)
|
||||
const [viewport, setViewport] = useState({ scrollTop: 0, height: 0 })
|
||||
const scrollContainerRef = useRef<HTMLDivElement | null>(null)
|
||||
const resizeCleanupRef = useRef<(() => void) | null>(null)
|
||||
useEffect(() => () => resizeCleanupRef.current?.(), [])
|
||||
const rowSnapshotCacheRef = useRef(new Map<string, TableRowSnapshot>())
|
||||
const getRowSnapshot = useCallback((row: IConnectionsItem) => {
|
||||
const cache = rowSnapshotCacheRef.current
|
||||
@@ -664,6 +676,7 @@ export const ConnectionTable = (props: Props) => {
|
||||
minWidth: number,
|
||||
maxWidth: number | undefined,
|
||||
) => {
|
||||
resizeCleanupRef.current?.()
|
||||
const handleMove = (clientX: number) => {
|
||||
const nextWidth = Math.max(
|
||||
minWidth,
|
||||
@@ -686,8 +699,10 @@ export const ConnectionTable = (props: Props) => {
|
||||
window.removeEventListener('touchmove', handleTouchMove)
|
||||
window.removeEventListener('touchend', cleanup)
|
||||
window.removeEventListener('touchcancel', cleanup)
|
||||
resizeCleanupRef.current = null
|
||||
}
|
||||
|
||||
resizeCleanupRef.current = cleanup
|
||||
window.addEventListener('mousemove', handleMouseMove)
|
||||
window.addEventListener('mouseup', cleanup)
|
||||
window.addEventListener('touchmove', handleTouchMove, { passive: true })
|
||||
|
||||
@@ -16,7 +16,8 @@ const COLOR = [
|
||||
]
|
||||
|
||||
interface Props {
|
||||
value: Rule & { lineNo: number }
|
||||
value: Rule
|
||||
lineNo: number
|
||||
}
|
||||
|
||||
const parseColor = (text: string) => {
|
||||
@@ -31,7 +32,7 @@ const parseColor = (text: string) => {
|
||||
}
|
||||
|
||||
const RuleItem = (props: Props) => {
|
||||
const { value } = props
|
||||
const { value, lineNo } = props
|
||||
|
||||
return (
|
||||
<Item sx={{ borderBottom: '1px solid var(--divider-color)' }}>
|
||||
@@ -40,7 +41,7 @@ const RuleItem = (props: Props) => {
|
||||
variant="body2"
|
||||
sx={{ lineHeight: 2, minWidth: 30, mr: 2.25, textAlign: 'center' }}
|
||||
>
|
||||
{value.lineNo}
|
||||
{lineNo}
|
||||
</Typography>
|
||||
|
||||
<Box sx={{ userSelect: 'text' }}>
|
||||
|
||||
Reference in New Issue
Block a user