From a5a3b4f31e9959b0b8389b00cba64bc03f1d1e76 Mon Sep 17 00:00:00 2001 From: TheMode Date: Tue, 17 Aug 2021 01:55:22 +0200 Subject: [PATCH] Remove unnecessary map lookup for event interfaces --- .../minestom/server/event/EventInterface.java | 17 +++++++++++------ .../minestom/server/event/EventNodeImpl.java | 10 ++++++---- 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/src/main/java/net/minestom/server/event/EventInterface.java b/src/main/java/net/minestom/server/event/EventInterface.java index 529537157..17fadc53d 100644 --- a/src/main/java/net/minestom/server/event/EventInterface.java +++ b/src/main/java/net/minestom/server/event/EventInterface.java @@ -6,6 +6,7 @@ import java.util.Collection; import java.util.HashMap; import java.util.Map; import java.util.function.BiConsumer; +import java.util.function.Consumer; import java.util.function.Predicate; public interface EventInterface { @@ -16,7 +17,8 @@ public interface EventInterface { @NotNull Collection> eventTypes(); - void call(@NotNull E event); + Consumer consumer(Class eventType); + class FilteredBuilder { private final EventFilter filter; @@ -30,6 +32,7 @@ public interface EventInterface { public FilteredBuilder map(@NotNull Class eventType, @NotNull BiConsumer<@NotNull T, @NotNull M> consumer) { + //noinspection unchecked this.mapped.put(eventType, (BiConsumer) consumer); return this; } @@ -44,11 +47,13 @@ public interface EventInterface { } @Override - public void call(@NotNull E event) { - final T handler = filter.getHandler(event); - if (!predicate.test(handler)) return; - final var consumer = copy.get(event.getClass()); - consumer.accept(handler, event); + public Consumer consumer(Class eventType) { + final var consumer = copy.get(eventType); + return event -> { + final T handler = filter.getHandler(event); + if (!predicate.test(handler)) return; + consumer.accept(handler, event); + }; } }; } diff --git a/src/main/java/net/minestom/server/event/EventNodeImpl.java b/src/main/java/net/minestom/server/event/EventNodeImpl.java index e45a50cc3..020d469ba 100644 --- a/src/main/java/net/minestom/server/event/EventNodeImpl.java +++ b/src/main/java/net/minestom/server/event/EventNodeImpl.java @@ -202,7 +202,6 @@ class EventNodeImpl implements EventNode { }); Check.stateCondition(!correct, "The node filter {0} is not compatible with type {1}", nodeType, valueType); synchronized (mappedNodeCache) { - System.out.println("add " + value + " " + nodeImpl + " " + type); entry.mappedNode.put(value, (EventNode) nodeImpl); mappedNodeCache.put(value, entry); } @@ -226,7 +225,9 @@ class EventNodeImpl implements EventNode { synchronized (GLOBAL_CHILD_LOCK) { for (var eventType : eventInterface.eventTypes()) { var entry = getEntry((Class) eventType); + final var consumer = eventInterface.consumer(eventType); entry.interfaces.add((EventInterface) eventInterface); + entry.interfaceConsumers.add((Consumer) consumer); } } } @@ -304,6 +305,7 @@ class EventNodeImpl implements EventNode { final List> filters; final List> listeners = new CopyOnWriteArrayList<>(); final Set> interfaces = new CopyOnWriteArraySet<>(); + final Set> interfaceConsumers = new CopyOnWriteArraySet<>(); final Map> mappedNode = new WeakHashMap<>(); volatile int childCount; @@ -313,9 +315,9 @@ class EventNodeImpl implements EventNode { void call(T event) { // Event interfaces - if (!interfaces.isEmpty()) { - for (EventInterface inter : interfaces) { - inter.call(event); + if (!interfaceConsumers.isEmpty()) { + for (var consumer : interfaceConsumers) { + consumer.accept(event); } } // Mapped listeners