Skip to content

Commit dc968e1

Browse files
committed
refactor(project): Improve stage change handling
1 parent 2bf0409 commit dc968e1

2 files changed

Lines changed: 39 additions & 21 deletions

File tree

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

Lines changed: 37 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -294,9 +294,19 @@ export default class ProjectBuildCache {
294294
this.#project.useResultStage();
295295
const writtenResourcePaths = new Set();
296296
for (const [stageName, stageCache] of importedStages) {
297-
this.#project.setStage(stageName, stageCache.stage);
298-
for (const resourcePath of stageCache.writtenResourcePaths) {
299-
writtenResourcePaths.add(resourcePath);
297+
// Check whether the stage differs form the one currently in use
298+
if (this.#currentStageSignatures.get(stageName)?.join("-") !== stageCache.signature) {
299+
// Set stage
300+
this.#project.setStage(stageName, stageCache.stage);
301+
302+
// Store signature for later use in result stage signature calculation
303+
this.#currentStageSignatures.set(stageName, stageCache.signature.split("-"));
304+
305+
// Cached stage likely differs from the previous one (if any)
306+
// Add all resources written by the cached stage to the set of written/potentially changed resources
307+
for (const resourcePath of stageCache.writtenResourcePaths) {
308+
writtenResourcePaths.add(resourcePath);
309+
}
300310
}
301311
}
302312
return Array.from(writtenResourcePaths);
@@ -391,15 +401,17 @@ export default class ProjectBuildCache {
391401
});
392402

393403
const stageCache = await this.#findStageCache(stageName, stageSignatures);
404+
const oldStageSig = this.#currentStageSignatures.get(stageName)?.join("-");
394405
if (stageCache) {
395-
const stageChanged = this.#project.setStage(stageName, stageCache.stage);
406+
// Check whether the stage actually changed
407+
if (stageCache.signature !== oldStageSig) {
408+
this.#project.setStage(stageName, stageCache.stage);
396409

397-
// Store dependency signature for later use in result stage signature calculation
398-
this.#currentStageSignatures.set(stageName, stageCache.signature.split("-"));
410+
// Store new stage signature for later use in result stage signature calculation
411+
this.#currentStageSignatures.set(stageName, stageCache.signature.split("-"));
399412

400-
// Cached stage might differ from the previous one
401-
// Add all resources written by the cached stage to the set of written/potentially changed resources
402-
if (stageChanged) {
413+
// Cached stage likely differs from the previous one (if any)
414+
// Add all resources written by the cached stage to the set of written/potentially changed resources
403415
for (const resourcePath of stageCache.writtenResourcePaths) {
404416
if (!this.#writtenResultResourcePaths.includes(resourcePath)) {
405417
this.#writtenResultResourcePaths.push(resourcePath);
@@ -439,22 +451,28 @@ export default class ProjectBuildCache {
439451
if (deltaStageCache) {
440452
// Store dependency signature for later use in result stage signature calculation
441453
const [foundProjectSig, foundDepSig] = deltaStageCache.signature.split("-");
442-
this.#currentStageSignatures.set(stageName, [foundProjectSig, foundDepSig]);
454+
455+
// Check whether the stage actually changed
456+
if (oldStageSig !== deltaStageCache.signature) {
457+
this.#currentStageSignatures.set(stageName, [foundProjectSig, foundDepSig]);
458+
459+
// Cached stage likely differs from the previous one (if any)
460+
// Add all resources written by the cached stage to the set of written/potentially changed resources
461+
for (const resourcePath of deltaStageCache.writtenResourcePaths) {
462+
if (!this.#writtenResultResourcePaths.includes(resourcePath)) {
463+
this.#writtenResultResourcePaths.push(resourcePath);
464+
}
465+
}
466+
}
467+
468+
// Create new signature and determine changed resource paths
443469
const projectDeltaInfo = projectDeltas.get(foundProjectSig);
444470
const dependencyDeltaInfo = depDeltas.get(foundDepSig);
445471

446472
const newSignature = createStageSignature(
447473
projectDeltaInfo?.newSignature ?? foundProjectSig,
448474
dependencyDeltaInfo?.newSignature ?? foundDepSig);
449475

450-
// Using cached stage which might differ from the previous one
451-
// Add all resources written by the cached stage to the set of written/potentially changed resources
452-
for (const resourcePath of deltaStageCache.writtenResourcePaths) {
453-
if (!this.#writtenResultResourcePaths.includes(resourcePath)) {
454-
this.#writtenResultResourcePaths.push(resourcePath);
455-
}
456-
}
457-
458476
log.verbose(
459477
`Using delta cached stage for task ${taskName} in project ${this.#project.getName()} ` +
460478
`with original signature ${deltaStageCache.signature} (now ${newSignature}) ` +
@@ -616,8 +634,8 @@ export default class ProjectBuildCache {
616634

617635
log.verbose(`Caching stage for task ${taskName} in project ${this.#project.getName()} ` +
618636
`with signature ${stageSignature}`);
637+
619638
// Store resulting stage in stage cache
620-
// TODO: Check whether signature already exists and avoid invalidating following tasks
621639
this.#stageCache.addSignature(
622640
this.#getStageNameForTask(taskName), stageSignature, this.#project.getStage(),
623641
writtenResourcePaths);

packages/project/lib/specifications/Project.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -456,15 +456,15 @@ class Project extends Specification {
456456
if (stageOrCachedWriter instanceof Stage) {
457457
newStage = stageOrCachedWriter;
458458
if (oldStage === newStage) {
459-
// Same stage as before
459+
// Same stage as before, nothing to do
460460
return false; // Stored stage has not changed
461461
}
462462
} else {
463463
newStage = new Stage(stageId, undefined, stageOrCachedWriter);
464464
}
465465
this.#stages[stageIdx] = newStage;
466466

467-
// Update current stage reference if necessary
467+
// If we are updating the current stage, make sure to update and reset all relevant references
468468
if (oldStage === this.#currentStage) {
469469
this.#currentStage = newStage;
470470
// Unset "current" reader/writer. They might be outdated

0 commit comments

Comments
 (0)