Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

⚡️ perf(desktop): prewarm local database after navigation (#18218)

Innei0cb2d80a353f
diff --git a/apps/desktop/src/main/core/App.ts b/apps/desktop/src/main/core/App.ts
index 0c32f08c4b5..c2660da563b 100644
--- a/apps/desktop/src/main/core/App.ts
+++ b/apps/desktop/src/main/core/App.ts
@@ -25,6 +25,7 @@ import {
import { generateCliWrapper, getCliWrapperDir } from '@/modules/cliEmbedding';
import { ScreenCaptureManager } from '@/modules/screenCapture/ScreenCaptureManager';
import type { IServiceModule, ServiceLifecycle, ServiceModule } from '@/services';
+import LocalDatabaseService from '@/services/LocalDatabaseSrv';
import { createLogger } from '@/utils/logger';
import * as electronIs from '@/utils/platform';
import { refreshShellPath } from '@/utils/shellPath';
@@ -251,6 +252,7 @@ export class App {
// native menus, local-file services, tray and updater initialization.
await this.makeAppReady();
await this.browserManager.initializeBrowsers();
+ this.prewarmLocalDatabaseAfterNavigation();
await this.runControllerHooks('afterAppReady');
const initializeNativeShell = async () => {
@@ -332,6 +334,25 @@ export class App {
logger.info('Post-first-frame initialization completed');
};
+ private prewarmLocalDatabaseAfterNavigation = () => {
+ // BrowserManager starts the initial loadURL call while constructing the main
+ // window. Yield one event-loop turn so Chromium can begin serving navigation
+ // requests before node:sqlite performs its synchronous open and migrations.
+ setImmediate(() => {
+ if (this.isQuiting) return;
+
+ const startedAt = performance.now();
+ try {
+ this.getService(LocalDatabaseService).initialize();
+ logger.debug(
+ `Local database prewarm completed in ${(performance.now() - startedAt).toFixed(2)}ms`,
+ );
+ } catch (error) {
+ logger.warn('Local database prewarm failed:', error);
+ }
+ });
+ };
+
getService<T>(serviceClass: Class<T>): T {
return this.services.get(serviceClass) as T;
}
diff --git a/apps/desktop/src/main/core/__tests__/App.test.ts b/apps/desktop/src/main/core/__tests__/App.test.ts
index 4132cdc72c8..5e7c818ba81 100644
--- a/apps/desktop/src/main/core/__tests__/App.test.ts
+++ b/apps/desktop/src/main/core/__tests__/App.test.ts
@@ -143,6 +143,7 @@ vi.mock('../browser/BrowserManager', () => ({
BrowserManager: vi.fn().mockImplementation(() => ({
initializeBrowsers: vi.fn(),
getIdentifierByWebContents: vi.fn(),
+ waitForMainWindowFirstFrame: vi.fn(() => new Promise(() => {})),
})),
}));
@@ -207,6 +208,24 @@ describe('App', () => {
expect(destroy).toHaveBeenCalledOnce();
});
+
+ it('prewarms the local database after browser initialization yields to the event loop', async () => {
+ appInstance = new App();
+ const databaseService = appInstance.getService(LocalDatabaseService);
+ const initialize = vi.spyOn(databaseService, 'initialize').mockImplementation(() => {});
+
+ await appInstance.bootstrap();
+
+ expect(appInstance.browserManager.initializeBrowsers).toHaveBeenCalledOnce();
+ expect(initialize).not.toHaveBeenCalled();
+
+ await new Promise((resolve) => setImmediate(resolve));
+
+ expect(initialize).toHaveBeenCalledOnce();
+ expect(
+ vi.mocked(appInstance.browserManager.initializeBrowsers).mock.invocationCallOrder[0],
+ ).toBeLessThan(initialize.mock.invocationCallOrder[0]);
+ });
});
describe('desktop bootstrap identity', () => {