From 71444c981500119cbe90e8a52407ea908765d1a0 Mon Sep 17 00:00:00 2001 From: Tunglies <77394545+Tunglies@users.noreply.github.com> Date: Thu, 19 Nov 2026 10:09:55 +0800 Subject: [PATCH] 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. --- .../home/enhanced-traffic-stats.tsx | 21 ++++-- src/components/layout/layout-traffic.tsx | 70 +++++++++---------- 2 files changed, 49 insertions(+), 42 deletions(-) diff --git a/src/components/home/enhanced-traffic-stats.tsx b/src/components/home/enhanced-traffic-stats.tsx index dd5c5ebd4..87387d491 100644 --- a/src/components/home/enhanced-traffic-stats.tsx +++ b/src/components/home/enhanced-traffic-stats.tsx @@ -131,6 +131,15 @@ const CompactStatCard = memo( // 添加显示名称 CompactStatCard.displayName = 'CompactStatCard' +// Element identity is stable, so CompactStatCard's memo comparison holds +// across per-second parsedData updates. +const uploadSpeedIcon = +const downloadSpeedIcon = +const activeConnectionsIcon = +const uploadedIcon = +const downloadedIcon = +const memoryUsageIcon = + const fallbackSx = { p: 2, minHeight: 200, @@ -252,35 +261,35 @@ export const EnhancedTrafficStats = () => { const statCards = useMemo(() => { const cards: StatCardProps[] = [ { - icon: , + icon: uploadSpeedIcon, title: t('home.components.traffic.metrics.uploadSpeed'), value: parsedData.up, unit: `${parsedData.upUnit}/s`, color: 'secondary' as const, }, { - icon: , + icon: downloadSpeedIcon, title: t('home.components.traffic.metrics.downloadSpeed'), value: parsedData.down, unit: `${parsedData.downUnit}/s`, color: 'primary' as const, }, { - icon: , + icon: activeConnectionsIcon, title: t('home.components.traffic.metrics.activeConnections'), value: parsedData.connectionsCount, unit: '', color: 'success' as const, }, { - icon: , + icon: uploadedIcon, title: t('shared.labels.uploaded'), value: parsedData.uploadTotal, unit: parsedData.uploadTotalUnit, color: 'secondary' as const, }, { - icon: , + icon: downloadedIcon, title: t('shared.labels.downloaded'), value: parsedData.downloadTotal, unit: parsedData.downloadTotalUnit, @@ -290,7 +299,7 @@ export const EnhancedTrafficStats = () => { if (displayMemory) { cards.push({ - icon: , + icon: memoryUsageIcon, title: t('home.components.traffic.metrics.memoryUsage'), value: parsedData.inuse, unit: parsedData.inuseUnit, diff --git a/src/components/layout/layout-traffic.tsx b/src/components/layout/layout-traffic.tsx index 9c6564edc..d203f1007 100644 --- a/src/components/layout/layout-traffic.tsx +++ b/src/components/layout/layout-traffic.tsx @@ -42,6 +42,39 @@ const TrafficUnavailableFallback = () => { ) } +// Stable identities keep the emotion sx cache effective across the ~1Hz +// traffic-driven re-renders of LayoutTraffic. +const boxStyle: Pick = { + sx: { + display: 'flex', + alignItems: 'center', + whiteSpace: 'nowrap', + }, +} +const memoryBoxStyle: Pick = { + sx: { + ...boxStyle.sx, + cursor: 'auto', + }, +} +const iconStyle: Pick = { + sx: { mr: '8px', fontSize: 16 }, +} +const valStyle: Pick = { + component: 'span', + sx: { flex: '1 1 56px', userSelect: 'none', textAlign: 'center' }, +} +const unitStyle: Pick = { + component: 'span', + color: 'grey.500', + sx: { + flex: '0 1 27px', + userSelect: 'none', + fontSize: '12px', + textAlign: 'right', + }, +} + export const LayoutTraffic = () => { const { t } = useTranslation() const { verge } = useVerge() @@ -74,31 +107,6 @@ export const LayoutTraffic = () => { const [down, downUnit] = parseTraffic(traffic?.down || 0) const [inuse, inuseUnit] = parseTraffic(memory?.inuse || 0) - const boxStyle: Pick = { - sx: { - display: 'flex', - alignItems: 'center', - whiteSpace: 'nowrap', - }, - } - const iconStyle: Pick = { - sx: { mr: '8px', fontSize: 16 }, - } - const valStyle: Pick = { - component: 'span', - sx: { flex: '1 1 56px', userSelect: 'none', textAlign: 'center' }, - } - const unitStyle: Pick = { - component: 'span', - color: 'grey.500', - sx: { - flex: '0 1 27px', - userSelect: 'none', - fontSize: '12px', - textAlign: 'right', - }, - } - return ( { { { {displayMemory && (