Skip to content

Commit

Permalink
Merge pull request #15991 from gluxon/cached-Snapshot-iterables
Browse files Browse the repository at this point in the history
Improve watch performance by using stable identities for Snapshot iterables
  • Loading branch information
sokra committed Jul 5, 2022
2 parents 21ead2f + 7b3f4c0 commit 1132eb3
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 13 deletions.
44 changes: 31 additions & 13 deletions lib/FileSystemInfo.js
Expand Up @@ -208,6 +208,12 @@ class SnapshotIterable {
class Snapshot {
constructor() {
this._flags = 0;
/** @type {Iterable<string> | undefined} */
this._cachedFileIterable = undefined;
/** @type {Iterable<string> | undefined} */
this._cachedContextIterable = undefined;
/** @type {Iterable<string> | undefined} */
this._cachedMissingIterable = undefined;
/** @type {number | undefined} */
this.startTime = undefined;
/** @type {Map<string, FileSystemInfoEntry | null> | undefined} */
Expand Down Expand Up @@ -418,31 +424,43 @@ class Snapshot {
* @returns {Iterable<string>} iterable
*/
getFileIterable() {
return this._createIterable(s => [
s.fileTimestamps,
s.fileHashes,
s.fileTshs,
s.managedFiles
]);
if (this._cachedFileIterable === undefined) {
this._cachedFileIterable = this._createIterable(s => [
s.fileTimestamps,
s.fileHashes,
s.fileTshs,
s.managedFiles
]);
}
return this._cachedFileIterable;
}

/**
* @returns {Iterable<string>} iterable
*/
getContextIterable() {
return this._createIterable(s => [
s.contextTimestamps,
s.contextHashes,
s.contextTshs,
s.managedContexts
]);
if (this._cachedContextIterable === undefined) {
this._cachedContextIterable = this._createIterable(s => [
s.contextTimestamps,
s.contextHashes,
s.contextTshs,
s.managedContexts
]);
}
return this._cachedContextIterable;
}

/**
* @returns {Iterable<string>} iterable
*/
getMissingIterable() {
return this._createIterable(s => [s.missingExistence, s.managedMissing]);
if (this._cachedMissingIterable === undefined) {
this._cachedMissingIterable = this._createIterable(s => [
s.missingExistence,
s.managedMissing
]);
}
return this._cachedMissingIterable;
}
}

Expand Down
46 changes: 46 additions & 0 deletions test/FileSystemInfo.unittest.js
Expand Up @@ -363,4 +363,50 @@ ${details(snapshot)}`)
}
});
}

describe("stable iterables identity", () => {
const options = { timestamp: true };

/**
* @param {function((WebpackError | null)=, (Snapshot | null)=): void} callback callback function
*/
function getSnapshot(callback) {
const fs = createFs();
const fsInfo = createFsInfo(fs);
fsInfo.createSnapshot(
Date.now() + 10000,
files,
directories,
missing,
options,
callback
);
}

it("should return same iterable for getFileIterable()", done => {
getSnapshot((err, snapshot) => {
if (err) done(err);
expect(snapshot.getFileIterable()).toEqual(snapshot.getFileIterable());
done();
});
});

it("should return same iterable for getContextIterable()", done => {
getSnapshot((err, snapshot) => {
if (err) done(err);
expect(snapshot.getContextIterable()).toEqual(
snapshot.getContextIterable()
);
done();
});
});

it("should return same iterable for getMissingIterable()", done => {
getSnapshot((err, snapshot) => {
if (err) done(err);
expect(snapshot.getFileIterable()).toEqual(snapshot.getFileIterable());
done();
});
});
});
});

0 comments on commit 1132eb3

Please sign in to comment.