Skip computing digests when --use_action_cache=false.

Does this by delegating responsibility for constructing ActionCache.Entry
instances to the ActionCache, and having the StubActionCache return null.

Tests show a 1-2% elapsed-time reduction for clean builds: []

Doesn't use interface default methods, because we still need JDK7 for Mac.

--
PiperOrigin-RevId: 147722062
MOS_MIGRATED_REVID=147722062
diff --git a/src/main/java/com/google/devtools/build/lib/actions/ActionCacheChecker.java b/src/main/java/com/google/devtools/build/lib/actions/ActionCacheChecker.java
index 4f2a33c..7170836 100644
--- a/src/main/java/com/google/devtools/build/lib/actions/ActionCacheChecker.java
+++ b/src/main/java/com/google/devtools/build/lib/actions/ActionCacheChecker.java
@@ -267,7 +267,11 @@
     }
     Map<String, String> usedClientEnv = computeUsedClientEnv(action, clientEnv);
     ActionCache.Entry entry =
-        new ActionCache.Entry(action.getKey(), usedClientEnv, action.discoversInputs());
+        actionCache.newEntry(action.getKey(), usedClientEnv, action.discoversInputs());
+    if (entry == null) {
+      // Action cache is disabled, don't generate digests.
+      return;
+    }
     for (Artifact output : action.getOutputs()) {
       // Remove old records from the cache if they used different key.
       String execPath = output.getExecPathString();
@@ -386,12 +390,19 @@
       // Compute the aggregated middleman digest.
       // Since we never validate action key for middlemen, we should not store
       // it in the cache entry and just use empty string instead.
-      entry = new ActionCache.Entry("", ImmutableMap.<String, String>of(), false);
-      for (Artifact input : action.getInputs()) {
-        entry.addFile(input.getExecPath(), metadataHandler.getMetadataMaybe(input));
+      entry = actionCache.newEntry("", ImmutableMap.<String, String>of(), false);
+      if (entry != null) {
+        for (Artifact input : action.getInputs()) {
+          entry.addFile(input.getExecPath(), metadataHandler.getMetadataMaybe(input));
+        }
       }
     }
 
+    // Action cache is disabled, skip the digest.
+    if (entry == null) {
+      return;
+    }
+
     metadataHandler.setDigestForVirtualArtifact(middleman, entry.getFileDigest());
     if (changed) {
       actionCache.put(cacheKey, entry);
diff --git a/src/main/java/com/google/devtools/build/lib/actions/cache/ActionCache.java b/src/main/java/com/google/devtools/build/lib/actions/cache/ActionCache.java
index 1e123e5..0cdec63 100644
--- a/src/main/java/com/google/devtools/build/lib/actions/cache/ActionCache.java
+++ b/src/main/java/com/google/devtools/build/lib/actions/cache/ActionCache.java
@@ -59,6 +59,13 @@
   void remove(String key);
 
   /**
+   * Constructs an {@code Entry}.
+   * @return new {@code Entry} or null if the cache is disabled.
+   */
+  @Nullable
+  Entry newEntry(String key, Map<String, String> usedClientEnv, boolean discoversInputs);
+
+  /**
    * An entry in the ActionCache that contains all action input and output
    * artifact paths and their metadata plus action key itself.
    *
diff --git a/src/main/java/com/google/devtools/build/lib/actions/cache/CompactPersistentActionCache.java b/src/main/java/com/google/devtools/build/lib/actions/cache/CompactPersistentActionCache.java
index a644000..1be6efa 100644
--- a/src/main/java/com/google/devtools/build/lib/actions/cache/CompactPersistentActionCache.java
+++ b/src/main/java/com/google/devtools/build/lib/actions/cache/CompactPersistentActionCache.java
@@ -17,6 +17,7 @@
 
 import com.google.common.collect.ImmutableList;
 import com.google.common.collect.ImmutableMap;
+import com.google.devtools.build.lib.actions.cache.ActionCache.Entry;
 import com.google.devtools.build.lib.concurrent.ThreadSafety.ConditionallyThreadSafe;
 import com.google.devtools.build.lib.profiler.AutoProfiler;
 import com.google.devtools.build.lib.util.Clock;
@@ -337,6 +338,11 @@
     }
   }
 
+  @Override
+  public Entry newEntry(String key, Map<String, String> usedClientEnv, boolean discoversInputs) {
+    return new Entry(key, usedClientEnv, discoversInputs);
+  }
+
   /**
    * @return action data encoded as a byte[] array.
    */
diff --git a/src/main/java/com/google/devtools/build/lib/actions/cache/StubActionCache.java b/src/main/java/com/google/devtools/build/lib/actions/cache/StubActionCache.java
index 9e1cdd3..f1d8c8b 100644
--- a/src/main/java/com/google/devtools/build/lib/actions/cache/StubActionCache.java
+++ b/src/main/java/com/google/devtools/build/lib/actions/cache/StubActionCache.java
@@ -15,6 +15,7 @@
 package com.google.devtools.build.lib.actions.cache;
 
 import java.io.PrintStream;
+import java.util.Map;
 
 /** An {@link ActionCache} which does not store entries. */
 public class StubActionCache implements ActionCache {
@@ -37,4 +38,9 @@
 
   @Override
   public void dump(PrintStream out) {}
+  
+  @Override
+  public Entry newEntry(String key, Map<String, String> usedClientEnv, boolean discoversInputs) {
+    return null;
+  }
 }