1.16.5 Register too many listeners

Discussion in 'Spigot Plugin Development' started by xlightx, Sep 23, 2021.

  1. I have lots of objects, which represent a type of item, each object listens to separate events and acts when that event occurs.

    Is there any downside to this? (like the performance?).

    Let's say I have a AnItem:

    Code (Java):
    class AnItem implements Listener {
        @EventHandler
        public void onInteract(PlayerInteractEvent event) {
            // do something...
        }
    }
    then I register 100 of them or even more.

    Rather than just one.

    Code (Java):
    class GlobalListener implements Listener {
        @EventHandler
        public void onInteract(PlayerInteractEvent event) {
            // do something...
            // if this do that if that do this
        }

        @EventHandler
        public void onMove(PlayerMoveEvent event) {
            // do something...
           // if this do that if that do this
        }
    }
     
  2. The performance is depending on what action you're doing with an event. Yes, they can lower your server performance.
     
    • Like Like x 2
  3. Let's ignore what I do inside the events, does it affect performance when I register too many though?
     
  4. I don't know what to say about that. Just try registring how many you need, you don't need to implement all events if you don't use them.
     
  5. In short, yes. As Bukkit runs through each plugin that listens for that event and then executes the method accordingly, more registered listeners/methods does mean an increased load on the server. On the alternative having all your code in one listener block is just bad practice and would just be as bad as having all of them registered separately.

    I can see from your code you’re wanting to register them all in a single class, you can do this safely but if you have over 100 you’d need to organise them.
     
    • Like Like x 1
  6. If I can test how it affects performance. I wouldn't ask.
    And of course, I will only register what I need.

    Thanks for the fast reply though.
     
  7. I think this is about the debate of whether to have one listener and pass the event internally or have each object registered as a listener.
    Personally, I would go for 1 listener for the plugin and pass it to the objects that require it. This way you can easily add and remove them without doing too many interactions with the plugin manager.
     
  8. I've decided to have one listener only :)

    I'm still interested in other people's opinions, feels free to leave a reply in this thread.
     
  9. I'd say it depends on what you are trying to do. Having 100 of listeners shouldn't really impact performance (at least now the fact that it's 100 listeners), more important is what each listener is doing.

    In some of my plugins, where the order of execution is important, I do stuff like this:

    Code (Java):
    @EventHandler
    public void onWhatEverEvent(WhatEverEvent event) {
      doThis(event);
      doThat(event);
      if(shouldWeCancelThisEvent(event)) {
        event.setCancelled(true);
        return;
      }
      doAnotherThing(event);
      // and so on
    }
    while in other plugins, where I don't care about the order of execution, I also have about 10-20 listeners for the same event. It always depends on what you are actually trying to do.
     
  10. Try grouping up event based on what they do. If you need player events, put all that event inside of a class, and so on.
     
  11. Each time an event gets called bukkit reflectively calls the methods that have the same class as parameter in the methods with the annotation EventListener from all the classes that have been registered as listeners. I suppose that internally it has something like Map<Event, List<Method>> listeners to cache the methods to call for each event, but a reflective method call has indeed a performance impact. So yes, if you are concerned of the performance, then the fewer listeners, the better
     
  12. Well it's a yes and a no,usually you shouldnt have a problem having multiple listeners(as example for most minigames i have instanced listeners for the game wich i later unregister on finish) and so far i cant see any performance loss,unless you go with extreme amounts you should be good both ways

    Also its better to separate them if they affect a certain part of the project for readability
     
  13. Strahan

    Benefactor

    In that case, 100 is nothing. Even a old 80386 running at 33mhz can do over 4 MIPS :)
     
    • Agree Agree x 1