Merge pull request #15991 from gluxon/cached-Snapshot-iterables

Improve watch performance by using stable identities for Snapshot iterables
This commit is contained in:
Tobias Koppers 2022-07-05 10:28:34 +02:00 committed by GitHub
commit 1132eb3f38
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 77 additions and 13 deletions

View File

@ -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} */
@ -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;
}
}

View File

@ -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();
});
});
});
});