mirror of
https://github.com/Minestom/Minestom.git
synced 2025-01-02 14:38:26 +01:00
Use arrays instead of lists for loops
This commit is contained in:
parent
5e27399815
commit
93cbd52313
@ -44,8 +44,8 @@ class EventNodeImpl<T extends Event> implements EventNode<T> {
|
|||||||
final Handle<T> castedHandle = (Handle<T>) handle;
|
final Handle<T> castedHandle = (Handle<T>) handle;
|
||||||
Check.argCondition(castedHandle.node != this, "Invalid handle owner");
|
Check.argCondition(castedHandle.node != this, "Invalid handle owner");
|
||||||
if (!castedHandle.updated) castedHandle.update();
|
if (!castedHandle.updated) castedHandle.update();
|
||||||
final List<Consumer<T>> listeners = castedHandle.listeners;
|
final Consumer<T>[] listeners = castedHandle.listeners;
|
||||||
if (listeners.isEmpty()) return;
|
if (listeners.length == 0) return;
|
||||||
for (Consumer<T> listener : listeners) {
|
for (Consumer<T> listener : listeners) {
|
||||||
listener.accept(event);
|
listener.accept(event);
|
||||||
}
|
}
|
||||||
@ -62,7 +62,7 @@ class EventNodeImpl<T extends Event> implements EventNode<T> {
|
|||||||
public boolean hasListener(@NotNull ListenerHandle<? extends T> handle) {
|
public boolean hasListener(@NotNull ListenerHandle<? extends T> handle) {
|
||||||
final Handle<T> castedHandle = (Handle<T>) handle;
|
final Handle<T> castedHandle = (Handle<T>) handle;
|
||||||
if (!castedHandle.updated) castedHandle.update();
|
if (!castedHandle.updated) castedHandle.update();
|
||||||
return !castedHandle.listeners.isEmpty();
|
return castedHandle.listeners.length > 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -277,7 +277,8 @@ class EventNodeImpl<T extends Event> implements EventNode<T> {
|
|||||||
private static final class Handle<E extends Event> implements ListenerHandle<E> {
|
private static final class Handle<E extends Event> implements ListenerHandle<E> {
|
||||||
private final EventNodeImpl<E> node;
|
private final EventNodeImpl<E> node;
|
||||||
private final Class<E> eventType;
|
private final Class<E> eventType;
|
||||||
private final List<Consumer<E>> listeners = new CopyOnWriteArrayList<>();
|
private Consumer<E>[] listeners = new Consumer[0];
|
||||||
|
private final List<Consumer<E>> listenersCache = new ArrayList<>();
|
||||||
private volatile boolean updated;
|
private volatile boolean updated;
|
||||||
|
|
||||||
Handle(EventNodeImpl<E> node, Class<E> eventType) {
|
Handle(EventNodeImpl<E> node, Class<E> eventType) {
|
||||||
@ -287,8 +288,9 @@ class EventNodeImpl<T extends Event> implements EventNode<T> {
|
|||||||
|
|
||||||
void update() {
|
void update() {
|
||||||
synchronized (GLOBAL_CHILD_LOCK) {
|
synchronized (GLOBAL_CHILD_LOCK) {
|
||||||
this.listeners.clear();
|
this.listenersCache.clear();
|
||||||
recursiveUpdate(node);
|
recursiveUpdate(node);
|
||||||
|
this.listeners = listenersCache.toArray(Consumer[]::new);
|
||||||
this.updated = true;
|
this.updated = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -345,16 +347,16 @@ class EventNodeImpl<T extends Event> implements EventNode<T> {
|
|||||||
if (size == 1) {
|
if (size == 1) {
|
||||||
final var firstFilter = filterList.get(0);
|
final var firstFilter = filterList.get(0);
|
||||||
// Common case where there is only one filter
|
// Common case where there is only one filter
|
||||||
this.listeners.add(event -> mapper.accept(firstFilter, event));
|
this.listenersCache.add(event -> mapper.accept(firstFilter, event));
|
||||||
} else if (size == 2) {
|
} else if (size == 2) {
|
||||||
final var firstFilter = filterList.get(0);
|
final var firstFilter = filterList.get(0);
|
||||||
final var secondFilter = filterList.get(1);
|
final var secondFilter = filterList.get(1);
|
||||||
this.listeners.add(event -> {
|
this.listenersCache.add(event -> {
|
||||||
mapper.accept(firstFilter, event);
|
mapper.accept(firstFilter, event);
|
||||||
mapper.accept(secondFilter, event);
|
mapper.accept(secondFilter, event);
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
this.listeners.add(event -> {
|
this.listenersCache.add(event -> {
|
||||||
for (var filter : filterList) {
|
for (var filter : filterList) {
|
||||||
mapper.accept(filter, event);
|
mapper.accept(filter, event);
|
||||||
}
|
}
|
||||||
@ -374,20 +376,21 @@ class EventNodeImpl<T extends Event> implements EventNode<T> {
|
|||||||
final var predicate = targetNode.predicate;
|
final var predicate = targetNode.predicate;
|
||||||
|
|
||||||
final boolean hasPredicate = predicate != null;
|
final boolean hasPredicate = predicate != null;
|
||||||
final List<EventListener<E>> listenersCopy = List.copyOf(entry.listeners);
|
final EventListener<E>[] listenersCopy = entry.listeners.toArray(EventListener[]::new);
|
||||||
final List<Consumer<E>> bindingsCopy = List.copyOf(entry.bindingConsumers);
|
final Consumer<E>[] bindingsCopy = entry.bindingConsumers.toArray(Consumer[]::new);
|
||||||
if (!hasPredicate && listenersCopy.isEmpty() && bindingsCopy.isEmpty())
|
final boolean listenersEmpty = listenersCopy.length == 0;
|
||||||
|
final boolean bindingsEmpty = bindingsCopy.length == 0;
|
||||||
|
if (!hasPredicate && listenersEmpty && bindingsEmpty)
|
||||||
return; // Nothing to run
|
return; // Nothing to run
|
||||||
|
|
||||||
if (!hasPredicate && bindingsCopy.isEmpty() && listenersCopy.size() == 1) {
|
if (!hasPredicate && bindingsEmpty && listenersCopy.length == 1) {
|
||||||
// Only one normal listener
|
// Only one normal listener
|
||||||
final EventListener<E> listener = listenersCopy.get(0);
|
final EventListener<E> listener = listenersCopy[0];
|
||||||
this.listeners.add(e -> callListener(targetNode, listener, e));
|
this.listenersCache.add(e -> callListener(targetNode, listener, e));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Worse case scenario, try to run everything
|
// Worse case scenario, try to run everything
|
||||||
this.listeners.add(e -> {
|
this.listenersCache.add(e -> {
|
||||||
if (hasPredicate) {
|
if (hasPredicate) {
|
||||||
final Object value = filter.getHandler(e);
|
final Object value = filter.getHandler(e);
|
||||||
try {
|
try {
|
||||||
@ -397,12 +400,12 @@ class EventNodeImpl<T extends Event> implements EventNode<T> {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!listenersCopy.isEmpty()) {
|
if (!listenersEmpty) {
|
||||||
for (EventListener<E> listener : listenersCopy) {
|
for (EventListener<E> listener : listenersCopy) {
|
||||||
callListener(targetNode, listener, e);
|
callListener(targetNode, listener, e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!bindingsCopy.isEmpty()) {
|
if (!bindingsEmpty) {
|
||||||
for (Consumer<E> eConsumer : bindingsCopy) {
|
for (Consumer<E> eConsumer : bindingsCopy) {
|
||||||
try {
|
try {
|
||||||
eConsumer.accept(e);
|
eConsumer.accept(e);
|
||||||
|
Loading…
Reference in New Issue
Block a user