Skip to content

Commit

Permalink
chore: cache NodeFSTree read
Browse files Browse the repository at this point in the history
  • Loading branch information
WillieRuemmele committed Sep 18, 2024
1 parent 969c2e4 commit 9b864fc
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
10 changes: 8 additions & 2 deletions src/resolve/treeContainers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,14 @@ export class NodeFSTreeContainer extends TreeContainer {
}

public readFile(fsPath: SourcePath): Promise<Buffer> {
// significant enough performance increase using sync instead of fs.promise version
return Promise.resolve(readFileSync(fsPath));
if (this.fileContentMap.has(fsPath)) {
return Promise.resolve(this.fileContentMap.get(fsPath)!);
} else {
// significant enough performance increase using sync instead of fs.promise version
const content = readFileSync(fsPath);
this.fileContentMap.set(fsPath, content);
return Promise.resolve(content);
}
}

public readFileSync(fsPath: SourcePath): Buffer {
Expand Down
15 changes: 13 additions & 2 deletions test/resolve/treeContainers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,12 +119,23 @@ describe('Tree Containers', () => {
it('should use expected Node API for readFileSync', () => {
const readFileStub = env.stub(fs, 'readFileSync');
// @ts-ignore wants Dirents but string[] works as well
readFileStub.withArgs(path).returns(Buffer.from('test'));
const data = tree.readFileSync(path);
readFileStub.withArgs('myNewPath').returns(Buffer.from('test'));
const data = tree.readFileSync('myNewPath');
expect(data.toString()).to.deep.equal('test');
expect(readFileStub.calledOnce).to.be.true;
});

it('should use cached value for readFileSync', () => {
const readFileStub = env.stub(fs, 'readFileSync');
// @ts-ignore wants Dirents but string[] works as well
readFileStub.withArgs('myNewPath').returns(Buffer.from('test'));
const data = tree.readFileSync('myNewPath');
// returns same value
expect(data.toString()).to.deep.equal('test');
// didn't re-read the file
expect(readFileStub.called).to.be.false;
});

it('should use expected Node API for stream', () => {
const readable = new Readable();
// @ts-ignore wants ReadStream but Readable works for testing
Expand Down

2 comments on commit 9b864fc

@svc-cli-bot
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Benchmark

Benchmark suite Current: 9b864fc Previous: 969c2e4 Ratio
eda-componentSetCreate-linux 236 ms 239 ms 0.99
eda-sourceToMdapi-linux 2283 ms 2245 ms 1.02
eda-sourceToZip-linux 1838 ms 1831 ms 1.00
eda-mdapiToSource-linux 2988 ms 2969 ms 1.01
lotsOfClasses-componentSetCreate-linux 399 ms 389 ms 1.03
lotsOfClasses-sourceToMdapi-linux 3767 ms 3612 ms 1.04
lotsOfClasses-sourceToZip-linux 3100 ms 3121 ms 0.99
lotsOfClasses-mdapiToSource-linux 3575 ms 3531 ms 1.01
lotsOfClassesOneDir-componentSetCreate-linux 680 ms 685 ms 0.99
lotsOfClassesOneDir-sourceToMdapi-linux 6431 ms 6354 ms 1.01
lotsOfClassesOneDir-sourceToZip-linux 5377 ms 5352 ms 1.00
lotsOfClassesOneDir-mdapiToSource-linux 6553 ms 6860 ms 0.96

This comment was automatically generated by workflow using github-action-benchmark.

@svc-cli-bot
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Benchmark

Benchmark suite Current: 9b864fc Previous: 969c2e4 Ratio
eda-componentSetCreate-win32 692 ms 678 ms 1.02
eda-sourceToMdapi-win32 4510 ms 4461 ms 1.01
eda-sourceToZip-win32 3082 ms 3059 ms 1.01
eda-mdapiToSource-win32 6162 ms 6212 ms 0.99
lotsOfClasses-componentSetCreate-win32 1219 ms 1247 ms 0.98
lotsOfClasses-sourceToMdapi-win32 8224 ms 8257 ms 1.00
lotsOfClasses-sourceToZip-win32 5427 ms 5396 ms 1.01
lotsOfClasses-mdapiToSource-win32 8237 ms 8556 ms 0.96
lotsOfClassesOneDir-componentSetCreate-win32 2205 ms 2221 ms 0.99
lotsOfClassesOneDir-sourceToMdapi-win32 14463 ms 14988 ms 0.96
lotsOfClassesOneDir-sourceToZip-win32 9736 ms 10460 ms 0.93
lotsOfClassesOneDir-mdapiToSource-win32 14798 ms 14594 ms 1.01

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.