Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8313005
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T20:21:06+00:00 2026-06-08T20:21:06+00:00

I have made an event system, however, it is too slow. The issue, is

  • 0

I have made an event system, however, it is too slow.

The issue, is that there are multiple entries in a map that I never actually added. I don’t understand how they get there.

public class OrdinalMap<V> {

    private final Map<Integer, V> map;

    public OrdinalMap(Class<? extends Enum<?>> valueType, V virginValue) {
        map = new HashMap<Integer, V>();
        Enum<?>[] enums = valueType.getEnumConstants();
        for (int i = 0; i < enums.length; i++) {
            put(enums[i].ordinal(), virginValue);
        }
    }

    public OrdinalMap(Class<? extends Enum<?>> valueType) {
        this(valueType, null);
    }

    public V put(Integer key, V value) {
        return map.put(key, value);
    }

    public V get(Object o) {
        return map.get(o);
    }

    public Set<Entry<Integer, V>> entrySet() {
        return map.entrySet();
    }

}

I want to make dispatchEvent faster (less iterations). It has too many iterations because of registerListener

There are event handler methods inside of all of the other priorities, when they shouldn’t be there. I can’t figure out why there are there, but I’m certain it’s in registerListener. Because they are inside all priorities, I have to use this check:
if (mapping.getKey().getAnnotation(EventHandler.class).priority().ordinal() == entry.getKey()) {

Which makes it even slower.

@Override
public void dispatchEvent(Event event) {
    OrdinalMap<Map<Method, EventListener>> priorityMap = getRegistry().get(event.getClass());

    if (priorityMap != null) {
        CancellableEvent cancellableEvent = null;
        boolean cancellable;
        if (cancellable = event instanceof CancellableEvent) {
            cancellableEvent = (CancellableEvent) event;
            if (cancellableEvent.isCancelled()) return;
        }

        try {
            for (Entry<Integer, Map<Method, EventListener>> entry : priorityMap.entrySet()) {
                for (Entry<Method, EventListener> mapping : entry.getValue().entrySet()) {
                    if (mapping.getKey().getAnnotation(EventHandler.class).priority().ordinal() == entry.getKey()) {
                        mapping.getKey().invoke(mapping.getValue(), event);
                        if (cancellable && cancellableEvent.isCancelled()) return;
                    }
                }
            }
        } catch (InvocationTargetException | IllegalAccessException e) {
            e.printStackTrace();
        }
    }
}

@Override
public void registerListener(EventListener listener) {
    for (Method method : listener.getClass().getMethods()) {
        EventHandler handler = method.getAnnotation(EventHandler.class);
        if (handler != null) {
            Class<?>[] parameters = method.getParameterTypes();
            if (parameters.length == 1) {
                @SuppressWarnings("unchecked")
                Class<? extends Event> event = (Class<? extends Event>) parameters[0];
                EventPriority priority = handler.priority();

                OrdinalMap<Map<Method, EventListener>> priorityMap = getRegistry().get(event);
                if (priorityMap == null) {
                    priorityMap = new OrdinalMap<Map<Method, EventListener>>(EventPriority.class, (Map<Method, EventListener>) new HashMap<Method, EventListener>());
                }

                Map<Method, EventListener> methodMap = priorityMap.get(priority.ordinal());

                methodMap.put(method, listener);
                priorityMap.put(priority.ordinal(), methodMap);

                getRegistry().put(event, priorityMap);
            }
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T20:21:08+00:00Added an answer on June 8, 2026 at 8:21 pm

    You are using maps so consider using thiere benefits instead of iterating on all entries

    if (mapping.getKey().getAnnotation(EventHandler.class).priority().ordinal() == entry.getKey()) {
    

    comparing two hahmap keys to find a match is not really a good idea.

    how about the following, i hope i didn’t make any thinking mistake

    Set<Integer> priorityMapKeySet = priorityMap.keySet();
    for (Map<Method, EventListener> mapping : priorityMap.values()) {
        if (priorityMapKeySet.contains(mapping.getKey().getAnnotation(EventHandler.class).priority().ordinal())) {
            mapping.getKey().invoke(mapping.getValue(), event);
            if (cancellable && cancellableEvent.isCancelled()) return;
        }
    }
    

    here you no longer have the outer for loop

    My bad, didn’t pay attention enough …

    But the idea is the same, when using hashmaps / hashsets one should always try to use get / contains instead of iterating, and for that one needs to design the registry the way that makes this prossible

    Would the following work for your needs ? (untested)

    private final static class Registry {
    
        private final static Map<String, Set<Integer>>  prioritySetByEventMap = new HashMap<>();
        private final static Map<String, EventListener> eventListenerByEventAndPriorityMap = new HashMap<>();
        private final static Map<String, Method> methodByEventAndListenerMap = new HashMap<>();
    
        public static Set<Integer> getPrioritySetByEvent(Class<Event> event) {
            return prioritySetByEventMap.get(event.getName());
        }
    
        public static synchronized void registerEventByPriority(Class<Event> event, Integer priority) {
            Set<Integer> ps = prioritySetByEventMap.get(event.getName());
            if(ps == null) {
                ps = new HashSet<>();
                prioritySetByEventMap.put(event.getName(), ps);
            }
            ps.add(priority);
        }
    
        public static EventListener getEventListenerByEventAndPriority(Class<Event> event, Integer priority) {
            String key = event.getName() + "-" + priority;
            return eventListenerByEventAndPriorityMap.get(key);
        }
    
        public static synchronized void registerEventListenerByEventAndPriority(Class<Event> event, Integer priority, EventListener listener) {
            String key = event.getName() + "-" + priority;
            eventListenerByEventAndPriorityMap.put(key, listener);
        }
    
        public static Method getMethodByEventAndListener(Class<Event> event, EventListener listener) {
            String key = listener.toString() + "-" + event.getName();
            return methodByEventAndListenerMap.get(key);
        }
    
        public static synchronized void registerMethodByEventAndListener(Class<Event> event, EventListener listener, Method method) {
            String key = listener.toString() + "-" + event.getName();
            methodByEventAndListenerMap.put(key, method);
        }
    }
    

    and

    public void registerListener(EventListener listener) {
        for (Method method : listener.getClass().getMethods()) {
            EventHandler handler = method.getAnnotation(EventHandler.class);
            if (handler != null) {
                Class<?>[] parameters = method.getParameterTypes();
                if (parameters.length == 1) {
    
                    Class<Event> event = (Class<Event>) parameters[0];
    
                    EventPriority priority = handler.priority();
    
                    Registry.registerEventByPriority(event, priority.ordinal());
    
                    Registry.registerEventListenerByEventAndPriority(event, priority.ordinal(), listener);
    
                    Registry.registerMethodByEventAndListener(event, listener, method);
    
                }
            }
        }
    }
    
    
    public void dispatchEvent(Event event) {
        Set<Integer> prioritySet = Registry.getPrioritySetByEvent((Class<Event>) event.getClass());
    
        if (prioritySet != null) {
            CancellableEvent cancellableEvent = null;
            boolean cancellable;
            if (cancellable = event instanceof CancellableEvent) {
                cancellableEvent = (CancellableEvent) event;
                if (cancellableEvent.isCancelled())
                    return;
            }
    
            try {
    
                for(Integer priority : prioritySet) {
    
                    EventListener listener = Registry.getEventListenerByEventAndPriority((Class<Event>) event.getClass(), priority);
    
                    if(listener != null) {
                        Method m = Registry.getMethodByEventAndListener((Class<Event>) event.getClass(), listener);
                        if(m != null) {
                            m.invoke(listener, event);
                            if (cancellable && cancellableEvent.isCancelled()) {
                                return;
                            }
                        }
                    }
                }
    
            } catch (InvocationTargetException | IllegalAccessException e) {
                e.printStackTrace();
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is this even possible? I have a key-pair that I already made with GPG
I have made a jQuery toggle for a menu that I had in mind.
I have made a code that read data from flash Nand (without filesystem). fd
I have made a map of functions. all these functions are void and receive
I have made a page that allows users to upload files to the server
I have a List(Of MyObject) that I need to sort. So I've made sure
I think that you have heard of message/event buses, it's the single place when
I have made a drag-to-trigger_event page based on this guide . I'm displaying list
I have made an application for IPad in objective C. In this I am
i have made an application having entity framewrok. It is wpf application, now it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.