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.
This commit is contained in:
Tunglies
2026-11-19 10:09:55 +08:00
parent 02a1998b8d
commit 71444c9815
2 changed files with 49 additions and 42 deletions
+15 -6
View File
@@ -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 = <ArrowUpwardRounded fontSize="small" />
const downloadSpeedIcon = <ArrowDownwardRounded fontSize="small" />
const activeConnectionsIcon = <LinkRounded fontSize="small" />
const uploadedIcon = <CloudUploadRounded fontSize="small" />
const downloadedIcon = <CloudDownloadRounded fontSize="small" />
const memoryUsageIcon = <MemoryRounded fontSize="small" />
const fallbackSx = {
p: 2,
minHeight: 200,
@@ -252,35 +261,35 @@ export const EnhancedTrafficStats = () => {
const statCards = useMemo(() => {
const cards: StatCardProps[] = [
{
icon: <ArrowUpwardRounded fontSize="small" />,
icon: uploadSpeedIcon,
title: t('home.components.traffic.metrics.uploadSpeed'),
value: parsedData.up,
unit: `${parsedData.upUnit}/s`,
color: 'secondary' as const,
},
{
icon: <ArrowDownwardRounded fontSize="small" />,
icon: downloadSpeedIcon,
title: t('home.components.traffic.metrics.downloadSpeed'),
value: parsedData.down,
unit: `${parsedData.downUnit}/s`,
color: 'primary' as const,
},
{
icon: <LinkRounded fontSize="small" />,
icon: activeConnectionsIcon,
title: t('home.components.traffic.metrics.activeConnections'),
value: parsedData.connectionsCount,
unit: '',
color: 'success' as const,
},
{
icon: <CloudUploadRounded fontSize="small" />,
icon: uploadedIcon,
title: t('shared.labels.uploaded'),
value: parsedData.uploadTotal,
unit: parsedData.uploadTotalUnit,
color: 'secondary' as const,
},
{
icon: <CloudDownloadRounded fontSize="small" />,
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: <MemoryRounded fontSize="small" />,
icon: memoryUsageIcon,
title: t('home.components.traffic.metrics.memoryUsage'),
value: parsedData.inuse,
unit: parsedData.inuseUnit,
+34 -36
View File
@@ -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<BoxProps, 'sx'> = {
sx: {
display: 'flex',
alignItems: 'center',
whiteSpace: 'nowrap',
},
}
const memoryBoxStyle: Pick<BoxProps, 'sx'> = {
sx: {
...boxStyle.sx,
cursor: 'auto',
},
}
const iconStyle: Pick<SvgIconProps, 'sx'> = {
sx: { mr: '8px', fontSize: 16 },
}
const valStyle: Pick<TypographyProps, 'component' | 'sx'> = {
component: 'span',
sx: { flex: '1 1 56px', userSelect: 'none', textAlign: 'center' },
}
const unitStyle: Pick<TypographyProps, 'component' | 'color' | 'sx'> = {
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<BoxProps, 'sx'> = {
sx: {
display: 'flex',
alignItems: 'center',
whiteSpace: 'nowrap',
},
}
const iconStyle: Pick<SvgIconProps, 'sx'> = {
sx: { mr: '8px', fontSize: 16 },
}
const valStyle: Pick<TypographyProps, 'component' | 'sx'> = {
component: 'span',
sx: { flex: '1 1 56px', userSelect: 'none', textAlign: 'center' },
}
const unitStyle: Pick<TypographyProps, 'component' | 'color' | 'sx'> = {
component: 'span',
color: 'grey.500',
sx: {
flex: '0 1 27px',
userSelect: 'none',
fontSize: '12px',
textAlign: 'right',
},
}
return (
<ErrorBoundary
FallbackComponent={TrafficUnavailableFallback}
@@ -120,9 +128,6 @@ export const LayoutTraffic = () => {
<Box
title={`${t('home.components.traffic.metrics.uploadSpeed')}`}
{...boxStyle}
sx={{
...boxStyle.sx,
}}
>
<ArrowUpwardRounded
{...iconStyle}
@@ -137,9 +142,6 @@ export const LayoutTraffic = () => {
<Box
title={`${t('home.components.traffic.metrics.downloadSpeed')}`}
{...boxStyle}
sx={{
...boxStyle.sx,
}}
>
<ArrowDownwardRounded
{...iconStyle}
@@ -154,11 +156,7 @@ export const LayoutTraffic = () => {
{displayMemory && (
<Box
title={`${t('home.components.traffic.metrics.memoryUsage')} `}
{...boxStyle}
sx={{
...boxStyle.sx,
cursor: 'auto',
}}
{...memoryBoxStyle}
color={'disabled'}
>
<MemoryRounded {...iconStyle} />