Skip to content

Commit

Permalink
Added temporary uris caching to increase performance in handleDidChan…
Browse files Browse the repository at this point in the history
…geTextDocument
  • Loading branch information
kbysiec committed Jul 16, 2021
1 parent 648c407 commit ee56268
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 5 deletions.
23 changes: 21 additions & 2 deletions src/dataService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ class DataService {
private itemsFilter!: ItemsFilter;
private patternProvider!: PatternProvider;

private uris: vscode.Uri[] | null = null;

private onDidItemIndexedEventEmitter: vscode.EventEmitter<number> =
new vscode.EventEmitter();
readonly onDidItemIndexed: vscode.Event<number> =
Expand Down Expand Up @@ -43,13 +45,23 @@ class DataService {
return workspaceData;
}

async isUriExistingInWorkspace(uri: vscode.Uri): Promise<boolean> {
const uris = await this.fetchUris(false);
async isUriExistingInWorkspace(
uri: vscode.Uri,
checkInCache: boolean = false
): Promise<boolean> {
const uris = checkInCache
? await this.getCachedUris()
: await this.fetchUris(false);

return uris.some(
(existingUri: vscode.Uri) => existingUri.fsPath === uri.fsPath
);
}

clearCachedUris(): void {
this.uris = null;
}

private async fetchUris(
shouldClearGitignoreExcludePatterns: boolean = true
): Promise<vscode.Uri[]> {
Expand All @@ -69,6 +81,13 @@ class DataService {
return uris && uris.length ? uris : await this.fetchUris();
}

private async getCachedUris(): Promise<vscode.Uri[]> {
if (!this.uris || !this.uris.length) {
this.uris = await this.fetchUris();
}
return this.uris;
}

private async includeSymbols(
workspaceData: WorkspaceData,
uris: vscode.Uri[]
Expand Down
28 changes: 26 additions & 2 deletions src/test/test/dataService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,18 +120,42 @@ describe("DataService", () => {
});

describe("isUriExistingInWorkspace", () => {
it("1: should return true if uri exists in workspace", async () => {
it(`1: should return true if uri exists in workspace
and cache should not be checked`, async () => {
setups.isUriExistingInWorkspace1();

const item = getItem();
assert.equal(await dataService.isUriExistingInWorkspace(item), true);
});

it("2: should return false if uri does not exist in workspace", async () => {
it(`2: should return false if uri does not exist in workspace
and cache should not be checked`, async () => {
setups.isUriExistingInWorkspace2();

const item = getItem("./test/path/to/workspace");
assert.equal(await dataService.isUriExistingInWorkspace(item), false);
});

it(`3: should return true if uri exists in workspace, cache is checked
and cache is not empty`, async () => {
setups.isUriExistingInWorkspace3();

const item = getItem();
assert.equal(
await dataService.isUriExistingInWorkspace(item, true),
true
);
});

it(`4: should return true if uri exists in workspace, cache is checked
and cache is empty`, async () => {
setups.isUriExistingInWorkspace4();

const item = getItem();
assert.equal(
await dataService.isUriExistingInWorkspace(item, true),
true
);
});
});
});
25 changes: 25 additions & 0 deletions src/test/testSetup/dataService.testSetup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -324,5 +324,30 @@ export const getTestSetups = (dataService: DataService) => {
},
]);
},
isUriExistingInWorkspace3: () => {
stubMultiple([
{
object: dataServiceAny,
method: "uris",
returns: getItems(),
isNotMethod: true,
},
]);
},
isUriExistingInWorkspace4: () => {
stubMultiple([
{
object: dataServiceAny,
method: "fetchUris",
returns: Promise.resolve(getItems()),
},
{
object: dataServiceAny,
method: "uris",
returns: [],
isNotMethod: true,
},
]);
},
};
};
10 changes: 9 additions & 1 deletion src/workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ class Workspace {
) => {
const uri = event.document.uri;
const isUriExistingInWorkspace =
await this.dataService.isUriExistingInWorkspace(uri);
await this.dataService.isUriExistingInWorkspace(uri, true);
const hasContentChanged = event.contentChanges.length;

if (isUriExistingInWorkspace && hasContentChanged) {
Expand All @@ -152,6 +152,8 @@ class Workspace {
visual studio code issue.
*/
private handleDidRenameFiles = async (event: vscode.FileRenameEvent) => {
this.dataService.clearCachedUris();

const uri = event.files[0].oldUri;
const hasWorkspaceMoreThanOneFolder =
this.utils.hasWorkspaceMoreThanOneFolder();
Expand All @@ -175,6 +177,8 @@ class Workspace {
};

private handleDidFileSave = async (uri: vscode.Uri) => {
this.dataService.clearCachedUris();

const isUriExistingInWorkspace =
await this.dataService.isUriExistingInWorkspace(uri);
if (isUriExistingInWorkspace) {
Expand All @@ -191,6 +195,8 @@ class Workspace {
// necessary to invoke updateCacheByPath after removeCacheByPath
await this.utils.sleep(1);

this.dataService.clearCachedUris();

await this.common.registerAction(
ActionType.Update,
this.updater.updateCacheByPath.bind(this.updater, uri),
Expand All @@ -200,6 +206,8 @@ class Workspace {
};

private handleDidFileFolderDelete = async (uri: vscode.Uri) => {
this.dataService.clearCachedUris();

await this.common.registerAction(
ActionType.Remove,
this.remover.removeFromCacheByPath.bind(this.remover, uri),
Expand Down
2 changes: 2 additions & 0 deletions src/workspaceCommon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ class WorkspaceCommon {
}

async index(comment: string): Promise<void> {
this.dataService.clearCachedUris();

await this.registerAction(
ActionType.Rebuild,
this.indexWithProgress.bind(this),
Expand Down

0 comments on commit ee56268

Please sign in to comment.