8167070: Performance regression in compound scopes

Extra call to inner scope's hasNext() causes performance regression

Reviewed-by: jlahoda
This commit is contained in:
Maurizio Cimadamore 2016-10-05 13:06:21 +01:00
parent a3f3f797b5
commit 1d29982cfc

View File

@ -46,7 +46,8 @@ public class Iterators {
private final Iterator<I> inputs;
private final Function<I, Iterator<O>> convertor;
private Iterator<O> currentIterator;
@SuppressWarnings("unchecked")
private Iterator<O> currentIterator = EMPTY;
public CompoundIterator(Iterable<I> inputs, Function<I, Iterator<O>> convertor) {
this.inputs = inputs.iterator();
@ -54,10 +55,10 @@ public class Iterators {
}
public boolean hasNext() {
while (inputs.hasNext() && (currentIterator == null || !currentIterator.hasNext())) {
currentIterator = convertor.apply(inputs.next());
if (currentIterator != null && !currentIterator.hasNext()) {
update();
}
return currentIterator != null && currentIterator.hasNext();
return currentIterator != null;
}
public O next() {
@ -70,5 +71,25 @@ public class Iterators {
public void remove() {
throw new UnsupportedOperationException();
}
private void update() {
while (inputs.hasNext()) {
currentIterator = convertor.apply(inputs.next());
if (currentIterator.hasNext()) return;
}
currentIterator = null;
}
}
@SuppressWarnings("rawtypes")
private final static Iterator EMPTY = new Iterator() {
public boolean hasNext() {
return false;
}
@Override
public Object next() {
return null;
}
};
}