提交 6e923a9d 编写于 作者: B Benjamin Pasero

perf - avoid writing state when state did not change

上级 7024e62d
......@@ -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);
}
......
......@@ -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<IMenubarData>(Menubar.lastKnownMenubarStorageKey);
if (menubarData) {
if (menubarData.menus) {
......
......@@ -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<void> {
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);
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册