mirror of
https://github.com/Minestom/Minestom.git
synced 2024-12-27 03:27:56 +01:00
Optimize Set#toArray for viewers
Signed-off-by: TheMode <themode@outlook.fr>
This commit is contained in:
parent
876f716e25
commit
affd4f10e6
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user