Skip to content

Instantly share code, notes, and snippets.

@lptr
Created February 5, 2024 13:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lptr/c84227f57ee8c5537b324057dd3c3605 to your computer and use it in GitHub Desktop.
Save lptr/c84227f57ee8c5537b324057dd3c3605 to your computer and use it in GitHub Desktop.
diff --git a/platforms/core-execution/execution/src/main/java/org/gradle/internal/execution/steps/AbstractResolveCachingStateStep.java b/platforms/core-execution/execution/src/main/java/org/gradle/internal/execution/steps/AbstractResolveCachingStateStep.java
index 1436c1e1b83..53c976c736a 100644
--- a/platforms/core-execution/execution/src/main/java/org/gradle/internal/execution/steps/AbstractResolveCachingStateStep.java
+++ b/platforms/core-execution/execution/src/main/java/org/gradle/internal/execution/steps/AbstractResolveCachingStateStep.java
@@ -52,8 +52,8 @@ public AbstractResolveCachingStateStep(BuildCacheController buildCache) {
@Override
public CachingResult execute(UnitOfWork work, C context) {
CachingState cachingState;
- cachingState = determineCacheKeyWithBeforeExecutionState(context)
- .map(cacheKeyAndBeforeExecutionState -> calculateCachingState(work, context, cacheKeyAndBeforeExecutionState.getCacheKey(), cacheKeyAndBeforeExecutionState.getBeforeExecutionState()))
+ cachingState = context.getBeforeExecutionState()
+ .map(beforeExecutionState -> calculateCachingState(work, context, beforeExecutionState))
.orElseGet(() -> !context.getValidationProblems().isEmpty()
? VALIDATION_FAILED_STATE
: calculateCachingStateWithNoCapturedInputs(work));
@@ -67,17 +67,12 @@ public CachingResult execute(UnitOfWork work, C context) {
return new CachingResult(result, cachingState);
}
- protected interface CacheKeyWithBeforeExecutionState {
- Optional<HashCode> getCacheKey();
- BeforeExecutionState getBeforeExecutionState();
- }
-
- private CachingState calculateCachingState(UnitOfWork work, C context, Optional<HashCode> maybeCacheKey, BeforeExecutionState beforeExecutionState) {
+ private CachingState calculateCachingState(UnitOfWork work, C context, BeforeExecutionState beforeExecutionState) {
Logger logger = buildCache.isEmitDebugLogging()
? LOGGER
: NOPLogger.NOP_LOGGER;
CachingStateFactory cachingStateFactory = new DefaultCachingStateFactory(logger);
- HashCode cacheKey = maybeCacheKey
+ HashCode cacheKey = getPreviousCacheKeyIfApplicable(context)
.orElseGet(() -> cachingStateFactory.calculateCacheKey(beforeExecutionState));
ImmutableList.Builder<CachingDisabledReason> cachingDisabledReasonsBuilder = ImmutableList.builder();
if (!context.getValidationProblems().isEmpty()) {
@@ -94,7 +89,10 @@ private CachingState calculateCachingState(UnitOfWork work, C context, Optional<
return cachingStateFactory.createCachingState(beforeExecutionState, cacheKey, cachingDisabledReasonsBuilder.build());
}
- protected abstract Optional<CacheKeyWithBeforeExecutionState> determineCacheKeyWithBeforeExecutionState(C context);
+ /**
+ * Return cache key from previous build if there are no changes.
+ */
+ protected abstract Optional<HashCode> getPreviousCacheKeyIfApplicable(C context);
protected abstract UpToDateResult executeDelegate(UnitOfWork work, C context, CachingState cachingState);
diff --git a/platforms/core-execution/execution/src/main/java/org/gradle/internal/execution/steps/ResolveIncrementalCachingStateStep.java b/platforms/core-execution/execution/src/main/java/org/gradle/internal/execution/steps/ResolveIncrementalCachingStateStep.java
index 201015d78ac..321f2c31508 100644
--- a/platforms/core-execution/execution/src/main/java/org/gradle/internal/execution/steps/ResolveIncrementalCachingStateStep.java
+++ b/platforms/core-execution/execution/src/main/java/org/gradle/internal/execution/steps/ResolveIncrementalCachingStateStep.java
@@ -19,7 +19,6 @@
import org.gradle.caching.internal.controller.BuildCacheController;
import org.gradle.internal.execution.UnitOfWork;
import org.gradle.internal.execution.caching.CachingState;
-import org.gradle.internal.execution.history.BeforeExecutionState;
import org.gradle.internal.execution.history.PreviousExecutionState;
import org.gradle.internal.hash.HashCode;
@@ -37,21 +36,11 @@ public ResolveIncrementalCachingStateStep(
}
@Override
- protected Optional<CacheKeyWithBeforeExecutionState> determineCacheKeyWithBeforeExecutionState(C context) {
+ protected Optional<HashCode> getPreviousCacheKeyIfApplicable(C context) {
return context.getChanges()
- .map(changes -> new CacheKeyWithBeforeExecutionState() {
- @Override
- public Optional<HashCode> getCacheKey() {
- return context.getPreviousExecutionState()
- .filter(__ -> changes.getChangeDescriptions().isEmpty())
- .map(PreviousExecutionState::getCacheKey);
- }
-
- @Override
- public BeforeExecutionState getBeforeExecutionState() {
- return changes.getBeforeExecutionState();
- }
- });
+ .flatMap(changes -> context.getPreviousExecutionState()
+ .filter(__ -> changes.getChangeDescriptions().isEmpty())
+ .map(PreviousExecutionState::getCacheKey));
}
@Override
diff --git a/platforms/core-execution/execution/src/main/java/org/gradle/internal/execution/steps/ResolveNonIncrementalCachingStateStep.java b/platforms/core-execution/execution/src/main/java/org/gradle/internal/execution/steps/ResolveNonIncrementalCachingStateStep.java
index a7d1651eaa5..de453aac807 100644
--- a/platforms/core-execution/execution/src/main/java/org/gradle/internal/execution/steps/ResolveNonIncrementalCachingStateStep.java
+++ b/platforms/core-execution/execution/src/main/java/org/gradle/internal/execution/steps/ResolveNonIncrementalCachingStateStep.java
@@ -20,7 +20,6 @@
import org.gradle.caching.internal.controller.NoOpBuildCacheController;
import org.gradle.internal.execution.UnitOfWork;
import org.gradle.internal.execution.caching.CachingState;
-import org.gradle.internal.execution.history.BeforeExecutionState;
import org.gradle.internal.hash.HashCode;
import java.util.Optional;
@@ -41,19 +40,8 @@ public ResolveNonIncrementalCachingStateStep(Step<? super NonIncrementalCachingC
}
@Override
- protected Optional<CacheKeyWithBeforeExecutionState> determineCacheKeyWithBeforeExecutionState(C context) {
- return context.getBeforeExecutionState()
- .map(beforeExecutionState -> new CacheKeyWithBeforeExecutionState() {
- @Override
- public Optional<HashCode> getCacheKey() {
- return Optional.empty();
- }
-
- @Override
- public BeforeExecutionState getBeforeExecutionState() {
- return beforeExecutionState;
- }
- });
+ protected Optional<HashCode> getPreviousCacheKeyIfApplicable(C context) {
+ return Optional.empty();
}
@Override
diff --git a/platforms/core-execution/execution/src/test/groovy/org/gradle/internal/execution/steps/ResolveIncrementalCachingStateStepTest.groovy b/platforms/core-execution/execution/src/test/groovy/org/gradle/internal/execution/steps/ResolveIncrementalCachingStateStepTest.groovy
index 4d779dec6fd..a2329caa76b 100644
--- a/platforms/core-execution/execution/src/test/groovy/org/gradle/internal/execution/steps/ResolveIncrementalCachingStateStepTest.groovy
+++ b/platforms/core-execution/execution/src/test/groovy/org/gradle/internal/execution/steps/ResolveIncrementalCachingStateStepTest.groovy
@@ -41,7 +41,7 @@ class ResolveIncrementalCachingStateStepTest extends AbstractResolveCachingState
then:
_ * buildCache.enabled >> buildCacheEnabled
_ * context.changes >> Optional.of(executionStateChanges)
- 1 * executionStateChanges.beforeExecutionState >> beforeExecutionState
+ _ * context.beforeExecutionState >> Optional.of(beforeExecutionState)
_ * executionStateChanges.changeDescriptions >> ImmutableList.of()
_ * context.previousExecutionState >> Optional.of(previousExecutionState)
@@ -67,7 +67,7 @@ class ResolveIncrementalCachingStateStepTest extends AbstractResolveCachingState
then:
_ * buildCache.enabled >> buildCacheEnabled
_ * context.changes >> Optional.of(executionStateChanges)
- 1 * executionStateChanges.beforeExecutionState >> beforeExecutionState
+ _ * context.beforeExecutionState >> Optional.of(beforeExecutionState)
_ * executionStateChanges.changeDescriptions >> ImmutableList.of("Out-of-date")
_ * context.previousExecutionState >> Optional.of(previousExecutionState)
@@ -92,7 +92,7 @@ class ResolveIncrementalCachingStateStepTest extends AbstractResolveCachingState
then:
_ * buildCache.enabled >> buildCacheEnabled
_ * context.changes >> Optional.of(executionStateChanges)
- _ * executionStateChanges.beforeExecutionState >> beforeExecutionState
+ _ * context.beforeExecutionState >> Optional.of(beforeExecutionState)
_ * context.previousExecutionState >> Optional.empty()
_ * context.validationProblems >> ImmutableList.of()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment