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 内核覆写配置 // 管理 Smart 内核覆写配置
await manageSmartOverride() if (core === 'mihomo-smart') {
await manageSmartOverride()
}
// generateProfile 返回实际使用的 current // generateProfile 返回实际使用的 current
const current = await generateProfile() const current = await generateProfile()

View File

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

View File

@ -128,9 +128,7 @@ app.on('open-url', async (_event, url) => {
await handleDeepLink(url) await handleDeepLink(url)
}) })
app.whenReady().then(async () => { const initPromise = (async () => {
electronApp.setAppUserModelId('party.mihomo.app')
await initBasic() await initBasic()
await checkHighPrivilegeCoreEarly() await checkHighPrivilegeCoreEarly()
await initAdminStatus() await initAdminStatus()
@ -144,52 +142,80 @@ app.whenReady().then(async () => {
appConfig.language = systemLanguage appConfig.language = systemLanguage
} }
await initI18n({ lng: appConfig.language }) await initI18n({ lng: appConfig.language })
return appConfig
} catch (e) { } catch (e) {
safeShowErrorBox('common.error.initFailed', `${e}`) safeShowErrorBox('common.error.initFailed', `${e}`)
app.quit() app.quit()
throw e
} }
})()
try { app.whenReady().then(async () => {
initCoreWatcher() electronApp.setAppUserModelId('party.mihomo.app')
const startPromises = await startCore()
if (startPromises.length > 0) {
startPromises[0].then(async () => {
await initProfileUpdater()
await initWebdavBackupScheduler()
await checkAdminRestartForTun()
})
}
} catch (e) {
safeShowErrorBox('mihomo.error.coreStartFailed', `${e}`)
}
try { const appConfig = await initPromise
await startMonitor()
} catch {
// ignore
}
app.on('browser-window-created', (_, window) => { app.on('browser-window-created', (_, window) => {
optimizer.watchWindowShortcuts(window) optimizer.watchWindowShortcuts(window)
}) })
const { showFloatingWindow: showFloating = false, disableTray = false } = await getAppConfig()
registerIpcMainHandlers() registerIpcMainHandlers()
await createWindow()
const createWindowPromise = createWindow()
let coreStarted = false
const coreStartPromise = (async (): Promise<void> => {
try {
initCoreWatcher()
const startPromises = await startCore()
if (startPromises.length > 0) {
startPromises[0].then(async () => {
await initProfileUpdater()
await initWebdavBackupScheduler()
await checkAdminRestartForTun()
})
}
coreStarted = true
} catch (e) {
safeShowErrorBox('mihomo.error.coreStartFailed', `${e}`)
}
})()
const monitorPromise = (async (): Promise<void> => {
try {
await startMonitor()
} catch {
// ignore
}
})()
await createWindowPromise
const { showFloatingWindow: showFloating = false, disableTray = false } = appConfig
const uiTasks: Promise<void>[] = [initShortcut()]
if (showFloating) { if (showFloating) {
try { uiTasks.push(
await showFloatingWindow() (async () => {
} catch (error) { try {
await logger.error('Failed to create floating window on startup', error) await showFloatingWindow()
} } catch (error) {
await logger.error('Failed to create floating window on startup', error)
}
})()
)
} }
if (!disableTray) { 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', () => { app.on('activate', () => {
showMainWindow() 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 { } catch {
// 忽略错误 // 忽略错误
} }
@ -389,19 +389,27 @@ export async function initBasic(): Promise<void> {
} }
export async function init(): Promise<void> { export async function init(): Promise<void> {
await startSubStoreFrontendServer()
await startSubStoreBackendServer()
const { sysProxy } = await getAppConfig() const { sysProxy } = await getAppConfig()
try {
if (sysProxy.enable) {
await startPacServer()
}
await triggerSysProxy(sysProxy.enable)
} catch {
// ignore
}
await startSSIDCheck() const initTasks: Promise<void>[] = [
startSubStoreFrontendServer(),
startSubStoreBackendServer(),
startSSIDCheck()
]
initTasks.push(
(async (): Promise<void> => {
try {
if (sysProxy.enable) {
await startPacServer()
}
await triggerSysProxy(sysProxy.enable)
} catch {
// ignore
}
})()
)
await Promise.all(initTasks)
initDeeplink() initDeeplink()
} }