Skip to content

Commit ed0f9c9

Browse files
committed
refactor(project): Only store new or modified cache entries
1 parent 23f823c commit ed0f9c9

2 files changed

Lines changed: 30 additions & 6 deletions

File tree

packages/project/lib/build/cache/BuildTaskCache.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ export default class BuildTaskCache {
4343
#readTaskMetadataCache;
4444
#treeRegistries = [];
4545
#useDifferentialUpdate = true;
46+
#isNewOrModified;
4647

4748
// ===== LIFECYCLE =====
4849

@@ -66,6 +67,7 @@ export default class BuildTaskCache {
6667
if (!this.#readTaskMetadataCache) {
6768
// No cache reader provided, start with empty graph
6869
this.#resourceRequests = new ResourceRequestGraph();
70+
this.#isNewOrModified = true;
6971
return;
7072
}
7173

@@ -76,6 +78,7 @@ export default class BuildTaskCache {
7678
`of project '${this.#projectName}'`);
7779
}
7880
this.#resourceRequests = this.#restoreGraphFromCache(taskMetadata);
81+
this.#isNewOrModified = false;
7982
}
8083

8184
// ===== METADATA ACCESS =====
@@ -89,6 +92,10 @@ export default class BuildTaskCache {
8992
return this.#taskName;
9093
}
9194

95+
isNewOrModified() {
96+
return this.#isNewOrModified;
97+
}
98+
9299
/**
93100
* Updates resource indices for request sets affected by changed resources
94101
*
@@ -284,14 +291,14 @@ export default class BuildTaskCache {
284291
let setId = this.#resourceRequests.findExactMatch(requests);
285292
let resourceIndex;
286293
if (setId) {
294+
// Reuse existing resource index.
295+
// Note: This index has already been updated before the task executed, so no update is necessary here
287296
resourceIndex = this.#resourceRequests.getMetadata(setId).resourceIndex;
288-
// Index was already updated before the task executed
289297
} else {
290298
// New request set, check whether we can create a delta
291299
const metadata = {}; // Will populate with resourceIndex below
292300
setId = this.#resourceRequests.addRequestSet(requests, metadata);
293301

294-
295302
const requestSet = this.#resourceRequests.getNode(setId);
296303
const parentId = requestSet.getParentId();
297304
if (parentId) {
@@ -398,6 +405,8 @@ export default class BuildTaskCache {
398405
if (!relevantTree) {
399406
return;
400407
}
408+
this.#isNewOrModified = true;
409+
401410
// Update signatures for affected request sets
402411
const {requestSetId, signature: originalSignature} = trees.get(relevantTree);
403412
const newSignature = relevantTree.getRootHash();

packages/project/lib/build/cache/ProjectBuildCache.js

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ export default class ProjectBuildCache {
3333
#dependencyReader;
3434
#resourceIndex;
3535
#requiresInitialBuild;
36+
#isNewOrModified;
3637

3738
#invalidatedTasks = new Map();
3839

@@ -332,6 +333,7 @@ export default class ProjectBuildCache {
332333
}
333334
// Reset current project reader
334335
this.#currentProjectReader = null;
336+
this.#isNewOrModified = true;
335337
}
336338

337339
/**
@@ -579,6 +581,7 @@ export default class ProjectBuildCache {
579581
stageId, stageSignature, stageCache.resourceMetadata);
580582
this.#project.setResultStage(reader);
581583
this.#project.useResultStage();
584+
this.#isNewOrModified = false;
582585
return true;
583586
}
584587

@@ -695,9 +698,10 @@ export default class ProjectBuildCache {
695698
* @returns {Promise<void>}
696699
*/
697700
async storeCache(buildManifest) {
698-
log.verbose(`Storing build cache for project ${this.#project.getName()} ` +
699-
`with build signature ${this.#buildSignature}`);
700701
if (!this.#buildManifest) {
702+
log.verbose(`Storing build manifest for project ${this.#project.getName()} ` +
703+
`with build signature ${this.#buildSignature}`);
704+
// Write build manifest if it wasn't loaded from cache before
701705
this.#buildManifest = buildManifest;
702706
await this.#cacheManager.writeBuildManifest(this.#project.getId(), this.#buildSignature, buildManifest);
703707
}
@@ -707,11 +711,20 @@ export default class ProjectBuildCache {
707711

708712
// Store task caches
709713
for (const [taskName, taskCache] of this.#taskCache) {
710-
await this.#cacheManager.writeTaskMetadata(this.#project.getId(), this.#buildSignature, taskName,
711-
taskCache.toCacheObject());
714+
if (taskCache.isNewOrModified()) {
715+
log.verbose(`Storing task cache metadata for task ${taskName} in project ${this.#project.getName()} ` +
716+
`with build signature ${this.#buildSignature}`);
717+
await this.#cacheManager.writeTaskMetadata(this.#project.getId(), this.#buildSignature, taskName,
718+
taskCache.toCacheObject());
719+
}
712720
}
713721

722+
if (!this.#isNewOrModified) {
723+
return;
724+
}
714725
// Store stage caches
726+
log.verbose(`Storing stage caches for project ${this.#project.getName()} ` +
727+
`with build signature ${this.#buildSignature}`);
715728
const stageQueue = this.#stageCache.flushCacheQueue();
716729
await Promise.all(stageQueue.map(async ([stageId, stageSignature]) => {
717730
const {stage} = this.#stageCache.getCacheForSignature(stageId, stageSignature);
@@ -739,6 +752,8 @@ export default class ProjectBuildCache {
739752
}));
740753

741754
// Finally store index cache
755+
log.verbose(`Storing resource index cache for project ${this.#project.getName()} ` +
756+
`with build signature ${this.#buildSignature}`);
742757
const indexMetadata = this.#resourceIndex.toCacheObject();
743758
await this.#cacheManager.writeIndexCache(this.#project.getId(), this.#buildSignature, {
744759
...indexMetadata,

0 commit comments

Comments
 (0)