优化本次活动系统
Optimizing this event system
所以我正在重新设计我的事件系统,我想尽可能地优化它。我把这个减少到大约 82 毫秒来触发 100 万个事件。我想问一下你是否有什么不同的做法以及如何进一步优化这一点?目前,当一个事件被触发到该方法时,只有一些基本逻辑正在完成,当我计划将它用于即将到来的项目时,它会更高级一些,这可能会使执行时间加倍。
package me.xerces.eventbus;
import me.xerces.eventbus.annotation.EventHandler;
import me.xerces.eventbus.event.Event;
import java.lang.invoke.MethodHandle;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.AbstractMap;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Map;
public class EventManager {
private ArrayList<Map.Entry<Class, EventHandle>> eventHandlerList = new ArrayList<Map.Entry<Class, EventHandle>>();
public void addEventListener(Object object)
{
for(Method method : object.getClass().getDeclaredMethods())
{
if(method.isAnnotationPresent(EventHandler.class))
{
Map.Entry<Class, EventHandle> eventHandleEntry = new AbstractMap.SimpleEntry<Class, EventHandle>(method.getParameterTypes()[0], new EventHandle(method, object));
method.setAccessible(true);
eventHandlerList.add(eventHandleEntry);
}
}
}
public void addSpecificEventListener(Object object, Class eventClass)
{
for(Method method : object.getClass().getDeclaredMethods())
{
if(method.isAnnotationPresent(EventHandler.class) && method.getParameterTypes().length > 0 && method.getParameterTypes()[0].equals(eventClass))
{
Map.Entry<Class, EventHandle> eventHandleEntry = new AbstractMap.SimpleEntry<Class, EventHandle>(method.getParameterTypes()[0], new EventHandle(method, object));
method.setAccessible(true);
eventHandlerList.add(eventHandleEntry);
}
}
}
public void removeEventListener(Object object)
{
Iterator<Map.Entry<Class, EventHandle>> iterator = eventHandlerList.iterator();
while(iterator.hasNext())
{
Map.Entry<Class, EventHandle> entry = iterator.next();
if(entry.getValue().getMethodClass().equals(object))
iterator.remove();
}
}
public void fireEvent(Event event)
{
Iterator<Map.Entry<Class, EventHandle>> iterator = eventHandlerList.iterator();
while(iterator.hasNext())
{
Map.Entry<Class, EventHandle> entry = iterator.next();
if(entry.getKey().equals(event.getClass())) {
try {
EventHandle eventHandle = entry.getValue();
eventHandle.getMethod().invoke(eventHandle.getMethodClass(), event);
} catch (IllegalAccessException | InvocationTargetException e)
{
e.printStackTrace();
}
}
}
}
}
Github link: https://github.com/X3rces/EventBus
编辑:当 JVM 将调用转换为直接方法调用而不是使用 JNI 时,有没有办法加快速度?
你可以避免
if(entry.getKey().equals(event.getClass())) {
通过使用
private final Map<Class<?>, List<EventHandle>> eventHandlerMap = new HashMap<>();
查看我有问题的 EventSystem 代码
所以我正在重新设计我的事件系统,我想尽可能地优化它。我把这个减少到大约 82 毫秒来触发 100 万个事件。我想问一下你是否有什么不同的做法以及如何进一步优化这一点?目前,当一个事件被触发到该方法时,只有一些基本逻辑正在完成,当我计划将它用于即将到来的项目时,它会更高级一些,这可能会使执行时间加倍。
package me.xerces.eventbus;
import me.xerces.eventbus.annotation.EventHandler;
import me.xerces.eventbus.event.Event;
import java.lang.invoke.MethodHandle;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.AbstractMap;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Map;
public class EventManager {
private ArrayList<Map.Entry<Class, EventHandle>> eventHandlerList = new ArrayList<Map.Entry<Class, EventHandle>>();
public void addEventListener(Object object)
{
for(Method method : object.getClass().getDeclaredMethods())
{
if(method.isAnnotationPresent(EventHandler.class))
{
Map.Entry<Class, EventHandle> eventHandleEntry = new AbstractMap.SimpleEntry<Class, EventHandle>(method.getParameterTypes()[0], new EventHandle(method, object));
method.setAccessible(true);
eventHandlerList.add(eventHandleEntry);
}
}
}
public void addSpecificEventListener(Object object, Class eventClass)
{
for(Method method : object.getClass().getDeclaredMethods())
{
if(method.isAnnotationPresent(EventHandler.class) && method.getParameterTypes().length > 0 && method.getParameterTypes()[0].equals(eventClass))
{
Map.Entry<Class, EventHandle> eventHandleEntry = new AbstractMap.SimpleEntry<Class, EventHandle>(method.getParameterTypes()[0], new EventHandle(method, object));
method.setAccessible(true);
eventHandlerList.add(eventHandleEntry);
}
}
}
public void removeEventListener(Object object)
{
Iterator<Map.Entry<Class, EventHandle>> iterator = eventHandlerList.iterator();
while(iterator.hasNext())
{
Map.Entry<Class, EventHandle> entry = iterator.next();
if(entry.getValue().getMethodClass().equals(object))
iterator.remove();
}
}
public void fireEvent(Event event)
{
Iterator<Map.Entry<Class, EventHandle>> iterator = eventHandlerList.iterator();
while(iterator.hasNext())
{
Map.Entry<Class, EventHandle> entry = iterator.next();
if(entry.getKey().equals(event.getClass())) {
try {
EventHandle eventHandle = entry.getValue();
eventHandle.getMethod().invoke(eventHandle.getMethodClass(), event);
} catch (IllegalAccessException | InvocationTargetException e)
{
e.printStackTrace();
}
}
}
}
}
Github link: https://github.com/X3rces/EventBus 编辑:当 JVM 将调用转换为直接方法调用而不是使用 JNI 时,有没有办法加快速度?
你可以避免
if(entry.getKey().equals(event.getClass())) {
通过使用
private final Map<Class<?>, List<EventHandle>> eventHandlerMap = new HashMap<>();
查看我有问题的 EventSystem 代码