Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions src/lsp/ProjectManager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,41 @@ describe('ProjectManager', () => {
});

describe('flushDocumentChanges', () => {
it('reuses cached PathCollection across multiple flushes', async () => {
fsExtra.outputFileSync(`${rootDir}/source/main.brs`, ``);
fsExtra.outputJsonSync(`${rootDir}/bsconfig.json`, {});
await manager.syncProjects([workspaceSettings]);

//spy on PathCollection constructor to count how many are created
const project = manager.projects[0];
//first flush to warm the cache
await manager['flushDocumentChanges']({
actions: [{
srcPath: s`${rootDir}/source/main.brs`,
type: 'set',
fileContents: 'sub main():end sub',
allowStandaloneProject: true
}]
});

//grab the cached filterer
const cachedFilterer = manager['projectFiltererCache'].get(project);
expect(cachedFilterer).to.exist;

//second flush should reuse the same filterer instance
await manager['flushDocumentChanges']({
actions: [{
srcPath: s`${rootDir}/source/main.brs`,
type: 'set',
fileContents: 'sub main2():end sub',
allowStandaloneProject: true
}]
});

const cachedFilterer2 = manager['projectFiltererCache'].get(project);
expect(cachedFilterer2).to.equal(cachedFilterer);
});

it('does not crash when getting undefined back from projects', async () => {
fsExtra.outputFileSync(`${rootDir}/source/main.brs`, ``);
fsExtra.outputJsonSync(`${rootDir}/project1/bsconfig.json`, {
Expand Down
27 changes: 23 additions & 4 deletions src/lsp/ProjectManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,28 @@ export class ProjectManager {

public busyStatusTracker = new BusyStatusTracker<LspProject>();

/**
* Cache for PathCollection instances per project. Avoids recreating PathCollection
* on every document flush, which is wasteful since file patterns only change when a project is reloaded.
*/
private projectFiltererCache = new WeakMap<LspProject, PathCollection>();

/**
* Get or create a cached PathCollection for the given project.
* The filterer is invalidated when the project is removed and garbage collected.
*/
private getProjectFilterer(project: LspProject): PathCollection {
let filterer = this.projectFiltererCache.get(project);
if (!filterer) {
filterer = new PathCollection({
rootDir: project.rootDir,
globs: project.filePatterns
});
this.projectFiltererCache.set(project, filterer);
}
return filterer;
}

/**
* Apply all of the queued document changes. This should only be called as a result of the documentManager flushing changes, and never called manually
* @param event the document changes that have occurred since the last time we applied
Expand Down Expand Up @@ -101,10 +123,7 @@ export class ProjectManager {
//wait for this project to finish activating
await project.whenActivated();

const filterer = new PathCollection({
rootDir: project.rootDir,
globs: project.filePatterns
});
const filterer = this.getProjectFilterer(project);
// only include files that are applicable to this specific project (still allow deletes to flow through since they're cheap)
const projectActions = actions.filter(action => {
return (
Expand Down