Create less garbage in the common case of not finding duplicates.
RELNOTES: None.
@@ -123,13 +123,26 @@ public class AggregatingAttributeMapper extends AbstractAttributeMapper {
return builder.build();
}
+ private static ImmutableSet.Builder<Label> addDuplicateLabels(
+ ImmutableSet.Builder<Label> builder, List<Label> labels) {
+ Set<Label> duplicates = CollectionUtils.duplicatedElementsOf(labels);
+ if (duplicates.isEmpty()) {
+ return builder;
+ }
+ if (builder == null) {
+ builder = ImmutableSet.builderWithExpectedSize(duplicates.size());
+ }
+ builder.addAll(duplicates);
+ return builder;
+ }
+
/**
* Returns the labels that might appear multiple times in the same attribute value.
*/
public Set<Label> checkForDuplicateLabels(Attribute attribute) {
String attrName = attribute.getName();
Type<?> attrType = attribute.getType();
- ImmutableSet.Builder<Label> duplicates = ImmutableSet.builder();
+ ImmutableSet.Builder<Label> duplicates = null;
SelectorList<?> selectorList = getSelectorList(attribute.getName(), attrType);
if (selectorList == null || selectorList.getSelectors().size() == 1) {
@@ -143,7 +156,7 @@ public class AggregatingAttributeMapper extends AbstractAttributeMapper {
if (value != null) {
// TODO(bazel-team): Calculate duplicates directly using attrType.visitLabels in order to
// avoid intermediate collections here.
- duplicates.addAll(CollectionUtils.duplicatedElementsOf(extractLabels(attrType, value)));
+ duplicates = addDuplicateLabels(duplicates, extractLabels(attrType, value));
}
}
} else {
@@ -160,15 +173,15 @@ public class AggregatingAttributeMapper extends AbstractAttributeMapper {
for (Object selectorValue : selector.getEntries().values()) {
List<Label> labelsInSelectorValue = extractLabels(attrType, selectorValue);
// Duplicates within a single path are not okay.
- duplicates.addAll(CollectionUtils.duplicatedElementsOf(labelsInSelectorValue));
+ duplicates = addDuplicateLabels(duplicates, labelsInSelectorValue);
Iterables.addAll(selectorLabels, labelsInSelectorValue);
}
combinedLabels.addAll(selectorLabels);
}
- duplicates.addAll(CollectionUtils.duplicatedElementsOf(combinedLabels));
+ duplicates = addDuplicateLabels(duplicates, combinedLabels);
}
- return duplicates.build();
+ return duplicates == null ? ImmutableSet.of() : duplicates.build();
}
/**
