Rephrase AbstractAttributeMapper#visitLabels such that we can avoid creating a temporary Type.LabelVisitor instance per Attribute being visited. Instead, we can now create one temporary object per visitation. Getting rid of this dimension of scaling reduces the amount of garbage created.

RELNOTES: None

PiperOrigin-RevId: 152161836
diff --git a/src/main/java/com/google/devtools/build/lib/packages/AspectDefinition.java b/src/main/java/com/google/devtools/build/lib/packages/AspectDefinition.java
index 8e2ae52..118b2be 100644
--- a/src/main/java/com/google/devtools/build/lib/packages/AspectDefinition.java
+++ b/src/main/java/com/google/devtools/build/lib/packages/AspectDefinition.java
@@ -195,28 +195,27 @@
       final Multimap<Attribute, Label> labelBuilder,
       Aspect aspect,
       DependencyFilter dependencyFilter) {
+    LabelVisitor<Attribute> labelVisitor = new LabelVisitor<Attribute>() {
+      @Override
+      public void visit(Label label, Attribute aspectAttribute) {
+        Label repositoryRelative = maybeGetRepositoryRelativeLabel(from, label);
+        if (repositoryRelative == null) {
+          return;
+        }
+        labelBuilder.put(aspectAttribute, repositoryRelative);
+      }
+    };
     ImmutableMap<String, Attribute> attributes = aspect.getDefinition().getAttributes();
     for (final Attribute aspectAttribute : attributes.values()) {
       if (!dependencyFilter.apply(aspect, aspectAttribute)) {
         continue;
       }
-      Type type = aspectAttribute.getType();
+      Type<?> type = aspectAttribute.getType();
       if (type.getLabelClass() != LabelClass.DEPENDENCY) {
         continue;
       }
       try {
-        type.visitLabels(
-            new LabelVisitor() {
-              @Override
-              public void visit(Label label) {
-                Label repositoryRelative = maybeGetRepositoryRelativeLabel(from, label);
-                if (repositoryRelative == null) {
-                  return;
-                }
-                labelBuilder.put(aspectAttribute, repositoryRelative);
-              }
-            },
-            aspectAttribute.getDefaultValue(from));
+        type.visitLabels(labelVisitor, aspectAttribute.getDefaultValue(from), aspectAttribute);
       } catch (InterruptedException ex) {
         // Because the LabelVisitor does not throw InterruptedException, it should not be thrown
         // by visitLabels here.