Micro-optimization for TransitiveBaseTraversalFunction

No need to create 2-3 collections when we only need one.

PiperOrigin-RevId: 164273126
diff --git a/src/main/java/com/google/devtools/build/lib/skyframe/TransitiveBaseTraversalFunction.java b/src/main/java/com/google/devtools/build/lib/skyframe/TransitiveBaseTraversalFunction.java
index 3bc1f30..c622340 100644
--- a/src/main/java/com/google/devtools/build/lib/skyframe/TransitiveBaseTraversalFunction.java
+++ b/src/main/java/com/google/devtools/build/lib/skyframe/TransitiveBaseTraversalFunction.java
@@ -13,6 +13,8 @@
 // limitations under the License.
 package com.google.devtools.build.lib.skyframe;
 
+import com.google.common.collect.Collections2;
+import com.google.common.collect.ImmutableSet;
 import com.google.common.collect.Lists;
 import com.google.common.collect.Multimap;
 import com.google.devtools.build.lib.cmdline.Label;
@@ -123,7 +125,8 @@
     TProcessedTargets processedTargets = processTarget(label, targetAndErrorIfAny);
 
     // Process deps from attributes.
-    Iterable<SkyKey> labelDepKeys = getLabelDepKeys(targetAndErrorIfAny.getTarget());
+    Collection<SkyKey> labelDepKeys =
+        Collections2.transform(getLabelDeps(targetAndErrorIfAny.getTarget()), this::getKey);
 
     Map<SkyKey, ValueOrException2<NoSuchPackageException, NoSuchTargetException>> depMap =
         env.getValuesOrThrow(labelDepKeys, NoSuchPackageException.class,
@@ -195,42 +198,41 @@
       Environment env)
       throws InterruptedException;
 
-  private Iterable<SkyKey> getLabelDepKeys(Target target) throws InterruptedException {
-    List<SkyKey> depKeys = Lists.newArrayList();
-    for (Label depLabel : getLabelDeps(target)) {
-      depKeys.add(getKey(depLabel));
-    }
-    return depKeys;
-  }
-
   // TODO(bazel-team): Unify this logic with that in LabelVisitor, and possibly DependencyResolver.
-  private static Iterable<Label> getLabelDeps(Target target) throws InterruptedException {
-    final Set<Label> labels = new HashSet<>();
+  private static Collection<Label> getLabelDeps(Target target) throws InterruptedException {
     if (target instanceof OutputFile) {
       Rule rule = ((OutputFile) target).getGeneratingRule();
-      labels.add(rule.getLabel());
-      visitTargetVisibility(target, labels);
+      List<Label> visibilityLabels = visitTargetVisibility(target);
+      HashSet<Label> result = new HashSet<>(visibilityLabels.size() + 1);
+      result.add(rule.getLabel());
+      result.addAll(visibilityLabels);
+      return result;
     } else if (target instanceof InputFile) {
-      visitTargetVisibility(target, labels);
+      return new HashSet<>(visitTargetVisibility(target));
     } else if (target instanceof Rule) {
-      visitTargetVisibility(target, labels);
-      visitRule(target, labels);
+      List<Label> visibilityLabels = visitTargetVisibility(target);
+      Collection<Label> ruleLabels = visitRule(target);
+      HashSet<Label> result = new HashSet<>(visibilityLabels.size() + ruleLabels.size());
+      result.addAll(visibilityLabels);
+      result.addAll(ruleLabels);
+      return result;
     } else if (target instanceof PackageGroup) {
-      visitPackageGroup((PackageGroup) target, labels);
+      return new HashSet<>(visitPackageGroup((PackageGroup) target));
+    } else {
+      return ImmutableSet.of();
     }
-    return labels;
   }
 
-  private static void visitRule(Target target, Set<Label> labels) throws InterruptedException {
-    labels.addAll(((Rule) target).getTransitions(DependencyFilter.NO_NODEP_ATTRIBUTES).values());
+  private static Collection<Label> visitRule(Target target) throws InterruptedException {
+    return ((Rule) target).getTransitions(DependencyFilter.NO_NODEP_ATTRIBUTES).values();
   }
 
-  private static void visitTargetVisibility(Target target, Set<Label> labels) {
-    labels.addAll(target.getVisibility().getDependencyLabels());
+  private static List<Label> visitTargetVisibility(Target target) {
+    return target.getVisibility().getDependencyLabels();
   }
 
-  private static void visitPackageGroup(PackageGroup packageGroup, Set<Label> labels) {
-    labels.addAll(packageGroup.getIncludes());
+  private static List<Label> visitPackageGroup(PackageGroup packageGroup) {
+    return packageGroup.getIncludes();
   }
 
   enum LoadTargetResultsType {