From 6e923a9df83392053d09d803044ddc826b2ba0bb Mon Sep 17 00:00:00 2001 From: Benjamin Pasero Date: Fri, 21 Dec 2018 08:33:31 +0100 Subject: [PATCH] perf - avoid writing state when state did not change --- .../history/electron-main/historyMainService.ts | 7 ++++++- src/vs/platform/menubar/electron-main/menubar.ts | 4 ---- src/vs/platform/state/node/stateService.ts | 16 +++++++++++++--- 3 files changed, 19 insertions(+), 8 deletions(-) diff --git a/src/vs/platform/history/electron-main/historyMainService.ts b/src/vs/platform/history/electron-main/historyMainService.ts index 88e7c2e5b2a..ac90272ec41 100644 --- a/src/vs/platform/history/electron-main/historyMainService.ts +++ b/src/vs/platform/history/electron-main/historyMainService.ts @@ -180,7 +180,6 @@ export class HistoryMainService implements IHistoryMainService { // out of sync quickly over time. the attempted fix is to always set the list fresh // from our MRU history data. So we clear the documents first and then set the documents // again. - app.clearRecentDocuments(); const mru = this.getRecentlyOpened(); @@ -257,6 +256,7 @@ export class HistoryMainService implements IHistoryMainService { if (workspaceOrFile instanceof URI) { return getComparisonKey(workspaceOrFile); } + return workspaceOrFile.id; } @@ -286,6 +286,7 @@ export class HistoryMainService implements IHistoryMainService { } } } + if (Array.isArray(storedRecents.files2)) { for (const file of storedRecents.files2) { if (typeof file === 'string') { @@ -300,11 +301,13 @@ export class HistoryMainService implements IHistoryMainService { } } } + return result; } private saveRecentlyOpened(recent: IRecentlyOpened): void { const serialized: ISerializedRecentlyOpened = { workspaces2: [], files2: [] }; + for (const workspace of recent.workspaces) { if (isSingleFolderWorkspaceIdentifier(workspace)) { serialized.workspaces2.push(workspace.toString()); @@ -312,9 +315,11 @@ export class HistoryMainService implements IHistoryMainService { serialized.workspaces2.push(workspace); } } + for (const file of recent.files) { serialized.files2.push(file.toString()); } + this.stateService.setItem(HistoryMainService.recentlyOpenedStorageKey, serialized); } diff --git a/src/vs/platform/menubar/electron-main/menubar.ts b/src/vs/platform/menubar/electron-main/menubar.ts index e6592a75a30..43c65fcdb05 100644 --- a/src/vs/platform/menubar/electron-main/menubar.ts +++ b/src/vs/platform/menubar/electron-main/menubar.ts @@ -93,10 +93,6 @@ export class Menubar { } private restoreCachedMenubarData() { - // TODO@sbatten remove this at some point down the road - const outdatedKeys = ['lastKnownAdditionalKeybindings', 'lastKnownKeybindings', 'lastKnownMenubar']; - outdatedKeys.forEach(key => this.stateService.removeItem(key)); - const menubarData = this.stateService.getItem(Menubar.lastKnownMenubarStorageKey); if (menubarData) { if (menubarData.menus) { diff --git a/src/vs/platform/state/node/stateService.ts b/src/vs/platform/state/node/stateService.ts index 994e8b22fad..c4b5bc9bad8 100644 --- a/src/vs/platform/state/node/stateService.ts +++ b/src/vs/platform/state/node/stateService.ts @@ -15,6 +15,7 @@ import { readFile } from 'vs/base/node/pfs'; export class FileStorage { private _database: object | null = null; + private lastFlushedSerializedDatabase: string | null = null; constructor(private dbPath: string, private onError: (error: Error) => void) { } @@ -29,7 +30,8 @@ export class FileStorage { init(): Promise { return readFile(this.dbPath).then(contents => { try { - this._database = JSON.parse(contents.toString()); + this.lastFlushedSerializedDatabase = contents.toString(); + this._database = JSON.parse(this.lastFlushedSerializedDatabase); } catch (error) { this._database = {}; } @@ -44,7 +46,9 @@ export class FileStorage { private loadSync(): object { try { - return JSON.parse(fs.readFileSync(this.dbPath).toString()); + this.lastFlushedSerializedDatabase = fs.readFileSync(this.dbPath).toString(); + + return JSON.parse(this.lastFlushedSerializedDatabase); } catch (error) { if (error.code !== 'ENOENT') { this.onError(error); @@ -93,8 +97,14 @@ export class FileStorage { } private saveSync(): void { + const serializedDatabase = JSON.stringify(this.database, null, 4); + if (serializedDatabase === this.lastFlushedSerializedDatabase) { + return; // return early if the database has not changed + } + try { - writeFileAndFlushSync(this.dbPath, JSON.stringify(this.database, null, 4)); // permission issue can happen here + writeFileAndFlushSync(this.dbPath, serializedDatabase); // permission issue can happen here + this.lastFlushedSerializedDatabase = serializedDatabase; } catch (error) { this.onError(error); } -- GitLab