From 2b812f5fa510d8f01c719c534b8358ea9f523daf Mon Sep 17 00:00:00 2001 From: Tunglies <77394545+Tunglies@users.noreply.github.com> Date: Thu, 19 Nov 2026 23:16:35 +0800 Subject: [PATCH] perf(ui): hoist hot regexes and stabilize clash mutators The clash log parser and the URI validators recompiled their patterns per call; the patterns are module constants now. mutateClash and invalidateClashConfig are stable callbacks so consumers can keep them in dependency arrays; mutateClash revalidates through the module-level query helper instead of the per-render refetch closure (no caller reads its resolved value). --- src/hooks/use-clash.ts | 34 +++++++++++++++++++++------------ src/services/cmds.ts | 10 ++++++---- src/utils/uri-parser/helpers.ts | 14 +++++++------- 3 files changed, 35 insertions(+), 23 deletions(-) diff --git a/src/hooks/use-clash.ts b/src/hooks/use-clash.ts index 20475103d..212466948 100644 --- a/src/hooks/use-clash.ts +++ b/src/hooks/use-clash.ts @@ -1,5 +1,6 @@ import { useLockFn } from 'ahooks' import i18n from 'i18next' +import { useCallback } from 'react' import { getVersion } from 'tauri-plugin-mihomo-api' import { @@ -86,23 +87,29 @@ export const useClashMode = (shouldFetch: boolean = true) => { } export const useClash = () => { - const { data: clash, refetch } = useRuntimeConfig() + const { data: clash } = useRuntimeConfig() const { data: versionData, refetch: mutateVersion } = useQuery({ queryKey: ['getVersion'], queryFn: getVersion, }) - const mutateClash = (updater?: MutateClashUpdater, revalidate?: boolean) => { - if (updater === undefined) { - return refetch() - } - void setCacheData(['getRuntimeConfig'], updater) - if (revalidate !== false) { - return refetch() - } - return Promise.resolve() - } + // Stable identity: consumers pass mutateClash into their own callbacks. + // revalidateQuery hits the same cache key refetch would, without capturing + // the per-render refetch closure. + const mutateClash = useCallback( + (updater?: MutateClashUpdater, revalidate?: boolean) => { + if (updater === undefined) { + return revalidateQuery(['getRuntimeConfig']) + } + void setCacheData(['getRuntimeConfig'], updater) + if (revalidate !== false) { + return revalidateQuery(['getRuntimeConfig']) + } + return Promise.resolve() + }, + [], + ) const patchClash = useLockFn(async (patch: Partial) => { await mutate(() => patchClashConfig(patch), { @@ -143,7 +150,10 @@ export const useClashInfo = () => { }) }) - const invalidateClashConfig = () => revalidateQuery(['getClashConfig']) + const invalidateClashConfig = useCallback( + () => revalidateQuery(['getClashConfig']), + [], + ) return { clashInfo, diff --git a/src/services/cmds.ts b/src/services/cmds.ts index b5e685032..e38d19652 100644 --- a/src/services/cmds.ts +++ b/src/services/cmds.ts @@ -177,13 +177,15 @@ export async function getProxyView(): Promise { return view } +// getClashLogs matches both patterns against every log line. +const CLASH_LOG_REGEX = /time="(.+?)"\s+level=(.+?)\s+msg="(.+?)"/ +const CLASH_LOG_NEW_REGEX = /(.+?)\s+(.+?)\s+(.+)/ + export async function getClashLogs() { - const regex = /time="(.+?)"\s+level=(.+?)\s+msg="(.+?)"/ - const newRegex = /(.+?)\s+(.+?)\s+(.+)/ const logs = await commands.getClashLogs() return logs.reduce((acc, log) => { - const result = log.match(regex) + const result = log.match(CLASH_LOG_REGEX) if (result) { const [_, _time, type, payload] = result const time = dayjs(_time).format('MM-DD HH:mm:ss') @@ -191,7 +193,7 @@ export async function getClashLogs() { return acc } - const result2 = log.match(newRegex) + const result2 = log.match(CLASH_LOG_NEW_REGEX) if (result2) { const [_, time, type, payload] = result2 acc.push({ time, type, payload }) diff --git a/src/utils/uri-parser/helpers.ts b/src/utils/uri-parser/helpers.ts index 87adc64b8..05d0d4615 100644 --- a/src/utils/uri-parser/helpers.ts +++ b/src/utils/uri-parser/helpers.ts @@ -241,17 +241,17 @@ export function parseUrlLike( : result } +// Both predicates run per parsed proxy URI. +const IPV4_REGEX = /^(?:[0-9]{1,3}\.){3}[0-9]{1,3}$/ +const IPV6_REGEX = + /^([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}$|^::$|^::1$|^([0-9a-fA-F]{1,4}:)*::([0-9a-fA-F]{1,4}:)*[0-9a-fA-F]{1,4}$/ + export function isIPv4(address: string): boolean { - // Check if the address is IPv4 - const ipv4Regex = /^(?:[0-9]{1,3}\.){3}[0-9]{1,3}$/ - return ipv4Regex.test(address) + return IPV4_REGEX.test(address) } export function isIPv6(address: string): boolean { - // Check if the address is IPv6 - simplified regex to avoid backreference issues - const ipv6Regex = - /^([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}$|^::$|^::1$|^([0-9a-fA-F]{1,4}:)*::([0-9a-fA-F]{1,4}:)*[0-9a-fA-F]{1,4}$/ - return ipv6Regex.test(address) + return IPV6_REGEX.test(address) } export function decodeBase64OrOriginal(str: string): string {