Optimize Set#toArray for viewers

Signed-off-by: TheMode <themode@outlook.fr>
This commit is contained in:
TheMode 2021-12-09 14:59:18 +01:00
parent 876f716e25
commit affd4f10e6

View File

@ -14,6 +14,7 @@ import java.util.AbstractSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
import java.util.function.Consumer;
import java.util.function.Predicate;
@ -290,6 +291,34 @@ public final class ViewEngine {
}
}
private static final Object[] EMPTY = new Object[0];
@Override
public @NotNull Object @NotNull [] toArray() {
synchronized (mutex) {
final int size = size();
if (size == 0) return EMPTY;
Object[] array = new Object[size];
AtomicInteger index = new AtomicInteger();
forEach(player -> array[index.getAndIncrement()] = player);
return array;
}
}
@Override
@SuppressWarnings("unchecked")
public <T> @NotNull T @NotNull [] toArray(@NotNull T @NotNull [] a) {
synchronized (mutex) {
final int size = size();
T[] array = a.length >= size ? a :
(T[]) java.lang.reflect.Array.newInstance(a.getClass().getComponentType(), size);
AtomicInteger index = new AtomicInteger();
forEach(player -> array[index.getAndIncrement()] = (T) player);
return array;
}
}
final class It implements Iterator<Player> {
private Iterator<Player> current = ViewEngine.this.manualViewers.iterator();
private boolean autoIterator = false; // True if the current iterator comes from the auto-viewable references