perf: improve core startup performance

This commit is contained in:
Memory 2026-02-04 10:20:59 +08:00 committed by GitHub
parent dae4939390
commit 4bfa02023c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 81 additions and 45 deletions

View File

@ -160,7 +160,9 @@ async function prepareCore(detached: boolean, skipStop = false): Promise<CoreCon
}
// 管理 Smart 内核覆写配置
if (core === 'mihomo-smart') {
await manageSmartOverride()
}
// generateProfile 返回实际使用的 current
const current = await generateProfile()

View File

@ -9,7 +9,7 @@ const execPromise = promisify(exec)
// 常量
const CORE_READY_MAX_RETRIES = 30
const CORE_READY_RETRY_INTERVAL_MS = 500
const CORE_READY_RETRY_INTERVAL_MS = 100
export async function cleanupSocketFile(): Promise<void> {
if (process.platform === 'win32') {

View File

@ -128,9 +128,7 @@ app.on('open-url', async (_event, url) => {
await handleDeepLink(url)
})
app.whenReady().then(async () => {
electronApp.setAppUserModelId('party.mihomo.app')
const initPromise = (async () => {
await initBasic()
await checkHighPrivilegeCoreEarly()
await initAdminStatus()
@ -144,11 +142,29 @@ app.whenReady().then(async () => {
appConfig.language = systemLanguage
}
await initI18n({ lng: appConfig.language })
return appConfig
} catch (e) {
safeShowErrorBox('common.error.initFailed', `${e}`)
app.quit()
throw e
}
})()
app.whenReady().then(async () => {
electronApp.setAppUserModelId('party.mihomo.app')
const appConfig = await initPromise
app.on('browser-window-created', (_, window) => {
optimizer.watchWindowShortcuts(window)
})
registerIpcMainHandlers()
const createWindowPromise = createWindow()
let coreStarted = false
const coreStartPromise = (async (): Promise<void> => {
try {
initCoreWatcher()
const startPromises = await startCore()
@ -159,37 +175,47 @@ app.whenReady().then(async () => {
await checkAdminRestartForTun()
})
}
coreStarted = true
} catch (e) {
safeShowErrorBox('mihomo.error.coreStartFailed', `${e}`)
}
})()
const monitorPromise = (async (): Promise<void> => {
try {
await startMonitor()
} catch {
// ignore
}
})()
app.on('browser-window-created', (_, window) => {
optimizer.watchWindowShortcuts(window)
})
await createWindowPromise
const { showFloatingWindow: showFloating = false, disableTray = false } = await getAppConfig()
registerIpcMainHandlers()
await createWindow()
const { showFloatingWindow: showFloating = false, disableTray = false } = appConfig
const uiTasks: Promise<void>[] = [initShortcut()]
if (showFloating) {
uiTasks.push(
(async () => {
try {
await showFloatingWindow()
} catch (error) {
await logger.error('Failed to create floating window on startup', error)
}
})()
)
}
if (!disableTray) {
await createTray()
uiTasks.push(createTray())
}
await initShortcut()
await Promise.all(uiTasks)
await Promise.all([coreStartPromise, monitorPromise])
if (coreStarted) {
mainWindow?.webContents.send('core-started')
}
app.on('activate', () => {
showMainWindow()

View File

@ -165,7 +165,7 @@ async function killOldMihomoProcesses(): Promise<void> {
}
}
await new Promise((resolve) => setTimeout(resolve, 500))
await new Promise((resolve) => setTimeout(resolve, 200))
} catch {
// 忽略错误
}
@ -389,10 +389,16 @@ export async function initBasic(): Promise<void> {
}
export async function init(): Promise<void> {
await startSubStoreFrontendServer()
await startSubStoreBackendServer()
const { sysProxy } = await getAppConfig()
const initTasks: Promise<void>[] = [
startSubStoreFrontendServer(),
startSubStoreBackendServer(),
startSSIDCheck()
]
initTasks.push(
(async (): Promise<void> => {
try {
if (sysProxy.enable) {
await startPacServer()
@ -401,7 +407,9 @@ export async function init(): Promise<void> {
} catch {
// ignore
}
})()
)
await startSSIDCheck()
await Promise.all(initTasks)
initDeeplink()
}