Skip to content

Commit

Permalink
Query performance: use a custom Iterator impl
Browse files Browse the repository at this point in the history
For queries that primarily list the targets in a package or set of
packages, this can be on the critical path. I've seen significant
performance improvements with this change (after a number of other
fixes), and the implementation is not significantly more complex than
before, so I'm calling it a net win.

Also add some basic tests for the values() collection.

PiperOrigin-RevId: 249656957
  • Loading branch information
ulfjack authored and irengrig committed Jun 18, 2019
1 parent bbf5831 commit 96ff196
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import static java.util.stream.Collectors.joining;

import com.google.common.base.Preconditions;
import com.google.common.collect.AbstractIterator;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Streams;
import java.util.AbstractCollection;
Expand All @@ -28,6 +27,7 @@
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Set;
import java.util.stream.Stream;
import javax.annotation.Nullable;
Expand Down Expand Up @@ -153,13 +153,18 @@ public Iterator<V> iterator() {
if (isEmpty()) {
return Collections.emptyIterator();
}
return new AbstractIterator<V>() {
return new Iterator<V>() {
private int currentIndex = 0;

@Override
protected V computeNext() {
public boolean hasNext() {
return currentIndex < values.length;
}

@Override
public V next() {
if (currentIndex >= values.length) {
return endOfData();
throw new NoSuchElementException();
}
return values[currentIndex++];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.google.common.testing.NullPointerTester;
import java.io.Serializable;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import org.junit.Test;
Expand Down Expand Up @@ -249,4 +250,26 @@ public void toStringTest() {
map = ImmutableSortedKeyMap.of();
assertThat(map.toString()).isEqualTo("{}");
}

@Test
public void emptyValuesCollectionTest() {
Map<String, Integer> map = ImmutableSortedKeyMap.of();
assertThat(map.values().size()).isEqualTo(0);
assertThat(map.values()).containsExactly();
Iterator<Integer> it = map.values().iterator();
assertThat(it.hasNext()).isFalse();
}

@Test
public void valuesCollectionTest() {
Map<String, Integer> map = ImmutableSortedKeyMap.of("a", 1, "b", 2);
assertThat(map.values().size()).isEqualTo(2);
assertThat(map.values()).containsExactly(1, 2);
Iterator<Integer> it = map.values().iterator();
assertThat(it.hasNext()).isTrue();
assertThat(it.next()).isEqualTo(1);
assertThat(it.hasNext()).isTrue();
assertThat(it.next()).isEqualTo(2);
assertThat(it.hasNext()).isFalse();
}
}

0 comments on commit 96ff196

Please sign in to comment.