Startup Performance tuning

This commit is contained in:
2026-10-18 20:17:28 +03:00
parent e63b1c5c8e
commit 640fca8a70
12 changed files with 83 additions and 25 deletions
+45 -11
View File
@@ -1,3 +1,12 @@
const startupTraceEnabled = process.env.EP_MAIL_STARTUP_TRACE === '1';
const startupTraceStartedAt = process.hrtime.bigint();
function traceStartup(message) {
if (!startupTraceEnabled) return;
const elapsed = Number(process.hrtime.bigint() - startupTraceStartedAt) / 1e6;
console.info(`[ep-mail startup +${elapsed.toFixed(1)}ms] ${message}`);
}
traceStartup('main process entered');
const { app, BrowserWindow, shell, ipcMain, safeStorage, dialog, Notification, powerMonitor, Menu, Tray, nativeImage } = require('electron');
const fsSync = require('node:fs');
const path = require('node:path');
@@ -82,10 +91,18 @@ let windowsAppIcon = null;
let tray = null;
function registerIpcHandler(channel, handler) {
return ipcMainHandle(channel, (event, ...args) => {
assertTrustedIpcSender(event, applicationHtmlPath);
validateIpcArguments(channel, args);
return handler(event, ...args);
return ipcMainHandle(channel, async (event, ...args) => {
const startedAt = startupTraceEnabled ? process.hrtime.bigint() : null;
try {
assertTrustedIpcSender(event, applicationHtmlPath);
validateIpcArguments(channel, args);
return await handler(event, ...args);
} finally {
if (startedAt) {
const elapsed = Number(process.hrtime.bigint() - startedAt) / 1e6;
traceStartup(`${channel} IPC completed in ${elapsed.toFixed(1)}ms`);
}
}
});
}
@@ -502,8 +519,10 @@ registerDavIpcHandlers({
randomUUID,
assertDavObjectReference
});
traceStartup('main services and IPC handlers initialized');
function createWindow() {
traceStartup('creating BrowserWindow');
const window = new BrowserWindow({
width: 1440,
height: 920,
@@ -522,9 +541,17 @@ function createWindow() {
}
});
window.once('ready-to-show', () => window.show());
window.webContents.on('dom-ready', () => traceStartup('renderer DOM ready'));
window.webContents.on('did-finish-load', () => traceStartup('renderer load complete'));
window.once('ready-to-show', () => {
traceStartup('BrowserWindow ready-to-show');
window.show();
});
window.loadFile(path.join(__dirname, '..', 'index.html'));
window.loadFile(path.join(__dirname, '..', 'index.html')).catch(error => {
traceStartup(`loadFile failed: ${error.message}`);
console.error('Could not load the application window:', error);
});
window.webContents.setWindowOpenHandler(() => {
return { action: 'deny' };
});
@@ -596,22 +623,29 @@ function repairWindowsStartMenuShortcut() {
}
app.whenReady().then(() => {
traceStartup('Electron app.whenReady');
app.setAppUserModelId(windowsAppUserModelId);
if (process.platform === 'win32') {
windowsAppIcon = nativeImage.createFromPath(path.join(__dirname, '..', 'icon.ico'));
if (windowsAppIcon.isEmpty()) throw new Error('Could not load the Windows application icon.');
repairWindowsStartMenuShortcut();
createWindowsTray();
}
createWindow();
const mainWindow = createWindow();
mainWindow.once('ready-to-show', () => {
if (process.platform === 'win32') {
repairWindowsStartMenuShortcut();
createWindowsTray();
}
void checkSnoozeReminders();
const startupSyncTimer = setTimeout(() => requestBackgroundSync(), 3000);
startupSyncTimer.unref();
traceStartup('post-first-frame work started; background sync scheduled');
});
const syncTimer = setInterval(() => {
void runBackgroundSync(true).catch(error => console.error('Periodic mail sync failed:', error));
}, backgroundSyncIntervalMs);
syncTimer.unref();
const snoozeTimer = setInterval(() => void checkSnoozeReminders(), snoozeCheckIntervalMs);
snoozeTimer.unref();
void checkSnoozeReminders();
requestBackgroundSync();
powerMonitor.on('suspend', () => {
idleWatcherManager.stopAll();
});