perf(proxy): stable tool callbacks and shared delay entries

Group headers and heads passed freshly-created closures into the
memoized tools, defeating it on every render; the tool callbacks are now
groupName-parameterized and forwarded directly. Cached delay entries are
write-once, so the defensive copy on read and the second copy in the
delay-state reducer were dropped.
This commit is contained in:
Tunglies
2026-11-19 21:45:45 +08:00
parent d162478100
commit d535c1b7d1
5 changed files with 48 additions and 38 deletions
+19 -17
View File
@@ -31,9 +31,9 @@ interface Props {
url?: string
groupName: string
headState: HeadState
onLocation: () => void
onCheckDelay: () => void
onHeadState: (val: Partial<HeadState>) => void
onLocation: (groupName: string) => void
onCheckDelay: (groupName: string) => void
onHeadState: (groupName: string, val: Partial<HeadState>) => void
}
export const ProxyGroupTools = memo(function ProxyGroupTools(props: Props) {
@@ -76,7 +76,7 @@ export const ProxyGroupTools = memo(function ProxyGroupTools(props: Props) {
// 因此对写入 headState 的动作做防抖,避免每输入一个字符就过滤一次。
const { run: applyFilter, flush: flushFilter } = useDebounceFn(
(state: SearchState) => {
onHeadState({
onHeadState(groupName, {
filterText: state.text,
filterMatchCase: state.matchCase,
filterMatchWholeWord: state.matchWholeWord,
@@ -125,7 +125,7 @@ export const ProxyGroupTools = memo(function ProxyGroupTools(props: Props) {
e.preventDefault()
e.stopPropagation()
}}
onChange={(e) => onHeadState({ testUrl: e.target.value })}
onChange={(e) => onHeadState(groupName, { testUrl: e.target.value })}
sx={{ flex: '1 1 auto', input: { py: 0.65, px: 1 } }}
/>
)}
@@ -155,8 +155,8 @@ export const ProxyGroupTools = memo(function ProxyGroupTools(props: Props) {
e.stopPropagation()
if (!headState.open)
// eslint-disable-next-line @eslint-react/dom-no-flush-sync
flushSync(() => onHeadState({ open: true }))
onLocation()
flushSync(() => onHeadState(groupName, { open: true }))
onLocation(groupName)
}}
>
<MyLocationRounded fontSize="inherit" />
@@ -171,16 +171,16 @@ export const ProxyGroupTools = memo(function ProxyGroupTools(props: Props) {
e.stopPropagation()
if (!headState.open)
// eslint-disable-next-line @eslint-react/dom-no-flush-sync
flushSync(() => onHeadState({ open: true }))
flushSync(() => onHeadState(groupName, { open: true }))
// Remind the user that it is custom test url
if (testUrl?.trim() && textState !== 'filter') {
onHeadState({ textState: 'url' })
onHeadState(groupName, { textState: 'url' })
}
if (testUrl?.trim() && !isValidUrl(testUrl)) {
showNotice.warning('proxies.feedback.warnings.invalidTestUrl')
return
}
onCheckDelay()
onCheckDelay(groupName)
}}
>
<NetworkCheckRounded fontSize="inherit" />
@@ -201,8 +201,8 @@ export const ProxyGroupTools = memo(function ProxyGroupTools(props: Props) {
e.stopPropagation()
if (!headState.open)
// eslint-disable-next-line @eslint-react/dom-no-flush-sync
flushSync(() => onHeadState({ open: true }))
onHeadState({
flushSync(() => onHeadState(groupName, { open: true }))
onHeadState(groupName, {
sortType: ((sortType + 1) % 3) as ProxySortType,
})
}}
@@ -219,7 +219,7 @@ export const ProxyGroupTools = memo(function ProxyGroupTools(props: Props) {
onClick={(e) => {
e.preventDefault()
e.stopPropagation()
onHeadState({
onHeadState(groupName, {
textState: textState === 'url' ? null : 'url',
})
setTimeout(() => inputRef.current?.focus())
@@ -245,8 +245,8 @@ export const ProxyGroupTools = memo(function ProxyGroupTools(props: Props) {
e.stopPropagation()
if (!headState.open)
// eslint-disable-next-line @eslint-react/dom-no-flush-sync
flushSync(() => onHeadState({ open: true }))
onHeadState({ showType: !showType })
flushSync(() => onHeadState(groupName, { open: true }))
onHeadState(groupName, { showType: !showType })
}}
>
{showType ? (
@@ -265,8 +265,10 @@ export const ProxyGroupTools = memo(function ProxyGroupTools(props: Props) {
e.stopPropagation()
if (!headState.open && textState !== 'filter')
// eslint-disable-next-line @eslint-react/dom-no-flush-sync
flushSync(() => onHeadState({ open: true }))
onHeadState({ textState: textState === 'filter' ? null : 'filter' })
flushSync(() => onHeadState(groupName, { open: true }))
onHeadState(groupName, {
textState: textState === 'filter' ? null : 'filter',
})
setTimeout(() => inputRef.current?.focus())
}}
>
+18 -12
View File
@@ -30,9 +30,9 @@ interface Props {
url?: string
groupName: string
headState: HeadState
onLocation: () => void
onCheckDelay: () => void
onHeadState: (val: Partial<HeadState>) => void
onLocation: (groupName: string) => void
onCheckDelay: (groupName: string) => void
onHeadState: (groupName: string, val: Partial<HeadState>) => void
}
const defaultSx: SxProps = {}
@@ -81,7 +81,7 @@ export const ProxyHead = ({
size="small"
color="inherit"
title={t('proxies.page.tooltips.locate')}
onClick={onLocation}
onClick={() => onLocation(groupName)}
>
<MyLocationRounded />
</IconButton>
@@ -95,13 +95,13 @@ export const ProxyHead = ({
// Remind the user that it is custom test url
if (testUrl?.trim() && textState !== 'filter') {
debugLog(`[ProxyHead] 使用自定义测试URL: ${testUrl}`)
onHeadState({ textState: 'url' })
onHeadState(groupName, { textState: 'url' })
}
if (testUrl?.trim() && !isValidUrl(testUrl)) {
showNotice.warning('proxies.feedback.warnings.invalidTestUrl')
return
}
onCheckDelay()
onCheckDelay(groupName)
}}
>
<NetworkCheckRounded />
@@ -118,7 +118,9 @@ export const ProxyHead = ({
][sortType]
}
onClick={() =>
onHeadState({ sortType: ((sortType + 1) % 3) as ProxySortType })
onHeadState(groupName, {
sortType: ((sortType + 1) % 3) as ProxySortType,
})
}
>
{sortType !== 1 && sortType !== 2 && <SortRounded />}
@@ -131,7 +133,9 @@ export const ProxyHead = ({
color="inherit"
title={t('proxies.page.tooltips.delayCheckUrl')}
onClick={() =>
onHeadState({ textState: textState === 'url' ? null : 'url' })
onHeadState(groupName, {
textState: textState === 'url' ? null : 'url',
})
}
>
{textState === 'url' ? (
@@ -149,7 +153,7 @@ export const ProxyHead = ({
? t('proxies.page.tooltips.showBasic')
: t('proxies.page.tooltips.showDetail')
}
onClick={() => onHeadState({ showType: !showType })}
onClick={() => onHeadState(groupName, { showType: !showType })}
>
{showType ? <VisibilityRounded /> : <VisibilityOffRounded />}
</IconButton>
@@ -159,7 +163,9 @@ export const ProxyHead = ({
color="inherit"
title={t('proxies.page.tooltips.filter')}
onClick={() =>
onHeadState({ textState: textState === 'filter' ? null : 'filter' })
onHeadState(groupName, {
textState: textState === 'filter' ? null : 'filter',
})
}
>
{textState === 'filter' ? <SearchOffRounded /> : <SearchRounded />}
@@ -176,7 +182,7 @@ export const ProxyHead = ({
useRegularExpression: filterUseRegularExpression,
}}
onSearch={(_, state) =>
onHeadState({
onHeadState(groupName, {
filterText: state.text,
filterMatchCase: state.matchCase,
filterMatchWholeWord: state.matchWholeWord,
@@ -197,7 +203,7 @@ export const ProxyHead = ({
size="small"
variant="outlined"
placeholder={t('proxies.page.placeholders.delayCheckUrl')}
onChange={(e) => onHeadState({ testUrl: e.target.value })}
onChange={(e) => onHeadState(groupName, { testUrl: e.target.value })}
sx={{ ml: 0.5, flex: '1 1 auto', input: { py: 0.65, px: 1 } }}
/>
)}
+7 -7
View File
@@ -33,7 +33,7 @@ interface RenderProps {
item: IRenderItem
stickyed?: boolean
isChainMode?: boolean
onLocation: (group: IRenderItem['group']) => void
onLocation: (groupName: string) => void
onCheckAll: (groupName: string) => void
onHeadState: (groupName: string, patch: Partial<HeadState>) => void
onChangeProxy: (
@@ -179,9 +179,9 @@ const ProxyGroupHeader = (props: RenderProps) => {
url={group.testUrl!}
groupName={group.name}
headState={headState!}
onLocation={() => onLocation(group)}
onCheckDelay={() => onCheckAll(group.name)}
onHeadState={(p) => onHeadState(group.name, p)}
onLocation={onLocation}
onCheckDelay={onCheckAll}
onHeadState={onHeadState}
/>
{!toolsOnLeft && proxyCount}
</ProxyGroupHeaderBlock>
@@ -270,9 +270,9 @@ export const ProxyRender = memo(function ProxyRender(props: RenderProps) {
url={group.testUrl!}
groupName={group.name}
headState={headState!}
onLocation={() => onLocation(group)}
onCheckDelay={() => onCheckAll(group.name)}
onHeadState={(p) => onHeadState(group.name, p)}
onLocation={onLocation}
onCheckDelay={onCheckAll}
onHeadState={onHeadState}
/>
)
}
+1 -1
View File
@@ -61,7 +61,7 @@ export function useProxyDelayState(
}
const cachedUpdate = delayManager.getDelayUpdate(name, groupName)
if (cachedUpdate) {
setDelayState({ ...cachedUpdate })
setDelayState(cachedUpdate)
return
}
+3 -1
View File
@@ -237,7 +237,9 @@ class DelayManager {
return undefined
}
return { ...entry }
// Entries are replaced, never mutated, so the cached object is shared
// directly; readers treat it as immutable.
return entry
}
getDelay(name: string, group: string) {