From 913fb17dcdfcdc5fcf8eafb1cecba9b165f64a4a Mon Sep 17 00:00:00 2001 From: Paint_Ninja Date: Wed, 25 Dec 2024 07:41:54 +0000 Subject: [PATCH] Simplify listener class checks in posting hot-path (#62) Remove the need for Objects#equals in the posting hot-path Remove null check in ASMEventHandler#invoke The handler is created by the factory, which for ASM and ClassFactory goes through IEventListenerFactory#getUniqueName, which would throw an NPE during handler creation, therefore handler can never be null. Cleanup ASMEventHandler, make filter final --- .../eventbus/ASMEventHandler.java | 19 ++++++++----------- .../net/minecraftforge/eventbus/EventBus.java | 2 +- .../minecraftforge/eventbus/ListenerList.java | 1 + 3 files changed, 10 insertions(+), 12 deletions(-) diff --git a/src/main/java/net/minecraftforge/eventbus/ASMEventHandler.java b/src/main/java/net/minecraftforge/eventbus/ASMEventHandler.java index c45f586..48d75ab 100644 --- a/src/main/java/net/minecraftforge/eventbus/ASMEventHandler.java +++ b/src/main/java/net/minecraftforge/eventbus/ASMEventHandler.java @@ -10,43 +10,40 @@ import static org.objectweb.asm.Type.getMethodDescriptor; public class ASMEventHandler implements IEventListener { - private final IEventListenerFactory factory; private final IEventListener handler; private final SubscribeEvent subInfo; private final String readable; - private Type filter = null; + private final Type filter; public ASMEventHandler(IEventListenerFactory factory, Object target, Method method, boolean isGeneric) throws IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException, ClassNotFoundException { - this.factory = factory; - handler = this.factory.create(method, target); + handler = factory.create(method, target); subInfo = method.getAnnotation(SubscribeEvent.class); readable = "ASM: " + target + " " + method.getName() + getMethodDescriptor(method); + Type filter = null; if (isGeneric) { Type type = method.getGenericParameterTypes()[0]; if (type instanceof ParameterizedType) { filter = ((ParameterizedType)type).getActualTypeArguments()[0]; if (filter instanceof ParameterizedType) // Unlikely that nested generics will ever be relevant for event filtering, so discard them filter = ((ParameterizedType)filter).getRawType(); - else if (filter instanceof WildcardType) { + else if (filter instanceof WildcardType wfilter) { // If there's a wildcard filter of Object.class, then remove the filter. - final WildcardType wfilter = (WildcardType) filter; if (wfilter.getUpperBounds().length == 1 && wfilter.getUpperBounds()[0] == Object.class && wfilter.getLowerBounds().length == 0) { filter = null; } } } } + this.filter = filter; } @SuppressWarnings("rawtypes") @Override public void invoke(Event event) { - if (handler != null) { - if (!event.isCanceled() || subInfo.receiveCanceled()) { - if (filter == null || filter == ((IGenericEvent)event).getGenericType()) - handler.invoke(event); - } + if (!event.isCanceled() || subInfo.receiveCanceled()) { + if (filter == null || filter == ((IGenericEvent)event).getGenericType()) + handler.invoke(event); } } diff --git a/src/main/java/net/minecraftforge/eventbus/EventBus.java b/src/main/java/net/minecraftforge/eventbus/EventBus.java index dcb55d7..46ccbf8 100644 --- a/src/main/java/net/minecraftforge/eventbus/EventBus.java +++ b/src/main/java/net/minecraftforge/eventbus/EventBus.java @@ -298,7 +298,7 @@ public boolean post(Event event, IEventBusInvokeDispatcher wrapper) { int index = 0; try { for (; index < listeners.length; index++) { - if (!trackPhases && Objects.equals(listeners[index].getClass(), EventPriority.class)) continue; + if (!trackPhases && listeners[index].getClass() == EventPriority.class) continue; wrapper.invoke(listeners[index], event); } } catch (Throwable throwable) { diff --git a/src/main/java/net/minecraftforge/eventbus/ListenerList.java b/src/main/java/net/minecraftforge/eventbus/ListenerList.java index fd4601b..131539b 100644 --- a/src/main/java/net/minecraftforge/eventbus/ListenerList.java +++ b/src/main/java/net/minecraftforge/eventbus/ListenerList.java @@ -203,6 +203,7 @@ private void buildCache() { } public void register(EventPriority priority, IEventListener listener) { + if (listener == null) return; writeLock.acquireUninterruptibly(); priorities.get(priority.ordinal()).add(listener); writeLock.release();