aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main/java/com/google/devtools/build/lib/util
diff options
context:
space:
mode:
authorGravatar shreyax <shreyax@google.com>2018-03-02 15:59:46 -0800
committerGravatar Copybara-Service <copybara-piper@google.com>2018-03-02 16:01:18 -0800
commita6679aead65603162d57ee5d868ab06fed369fab (patch)
tree810b9c0d6718ccdf60d2e6310d540ef42c1020dd /src/main/java/com/google/devtools/build/lib/util
parent74ceac74d540fc4fbc9b4a51a1b817e15057a3bb (diff)
Optimize GC usage of iterating over all elements of GroupedLists when we don't care about the group structure, and simplify the logic for prefetching old deps.
PiperOrigin-RevId: 187681887
Diffstat (limited to 'src/main/java/com/google/devtools/build/lib/util')
-rw-r--r--src/main/java/com/google/devtools/build/lib/util/GroupedList.java45
1 files changed, 39 insertions, 6 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/util/GroupedList.java b/src/main/java/com/google/devtools/build/lib/util/GroupedList.java
index 11bef3fcc2..3bf690a6c2 100644
--- a/src/main/java/com/google/devtools/build/lib/util/GroupedList.java
+++ b/src/main/java/com/google/devtools/build/lib/util/GroupedList.java
@@ -20,6 +20,7 @@ import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.google.devtools.build.lib.collect.compacthashset.CompactHashSet;
+import com.google.devtools.build.lib.concurrent.ThreadSafety.ThreadHostile;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
@@ -196,7 +197,7 @@ public class GroupedList<T> implements Iterable<Collection<T>> {
@SuppressWarnings("unchecked")
public Set<T> toSet() {
- ImmutableSet.Builder<T> builder = ImmutableSet.builder();
+ ImmutableSet.Builder<T> builder = ImmutableSet.builderWithExpectedSize(numElements());
for (Object obj : elements) {
if (obj instanceof List) {
builder.addAll((List<T>) obj);
@@ -262,6 +263,43 @@ public class GroupedList<T> implements Iterable<Collection<T>> {
return first.equals(second) || CompactHashSet.create(first).containsAll(second);
}
+ /** An iterator that loops through every element in each group. */
+ private class UngroupedIterator implements Iterator<T> {
+ private final Iterator<Object> iter = elements.iterator();
+ int counter = 0;
+ List<T> currentGroup;
+ int listCounter = 0;
+
+ @Override
+ public boolean hasNext() {
+ return counter < size;
+ }
+
+ @SuppressWarnings("unchecked") // Cast of Object to List<T> or T.
+ @Override
+ public T next() {
+ counter++;
+ if (currentGroup != null && listCounter < currentGroup.size()) {
+ return currentGroup.get(listCounter++);
+ }
+ Object nextGroup = iter.next();
+ if (nextGroup instanceof List) {
+ currentGroup = (List<T>) nextGroup;
+ listCounter = 1;
+ // GroupedLists shouldn't have empty lists stored.
+ return currentGroup.get(0);
+ } else {
+ currentGroup = null;
+ return (T) nextGroup;
+ }
+ }
+ }
+
+ @ThreadHostile
+ public Iterable<T> getAllElementsAsIterable() {
+ return UngroupedIterator::new;
+ }
+
@Override
public boolean equals(Object other) {
if (other == null) {
@@ -326,11 +364,6 @@ public class GroupedList<T> implements Iterable<Collection<T>> {
}
return ImmutableList.of((T) obj);
}
-
- @Override
- public void remove() {
- throw new UnsupportedOperationException();
- }
}
@Override