Solved Plugin optimization - Hashmaps + events

Discussion in 'Plugin Development' started by zotmer, Feb 22, 2017.

Thread Status:
Not open for further replies.
  1. Offline

    zotmer

    Hey guys! I've got 2 questions regarding the optimization of my plugin.

    1) I'm quite commonly using metadata for players to store different variables. I've found out that it is easier than creating a new Hashmap for each and every variable I want to have. The question is: are there any downsides of using metadata instead of normal variables or is it perfectly correct?

    2) I've got to the point where I have tons of events in my plugin. The thing is that some of those events are repeating (same event in different class with different conditions). Let me give an example:
    class A: projectile launch event, then testing if a projectile is snowball, then executes some code
    class B: projectile launch event, then testing if a projectile is fireball, then executes some code
    class C: projectile launch event, then testing if a projectile is ender pearl, then executes some code
    etc.
    Now: is it resource efficient to have multiple classes with same events? I'm thinking of creating a single class that would detect event, test for the projectile and then pass the event to different classes based on the projectile type. Would this make any difference?

    Thanks in advance for any helpful answer :)
     
  2. Offline

    Tecno_Wizard

    @zotmer there are a couple of things to consider when using metadata.
    1. Metadata doesn't persist. If the player logs out or the server shuts down, the data is gone.
    2. Its less straightforward to access.
    3. Some added complexity when reloading the data.
    4. HashMap access can be O(n) when not overriding the hashcode. I believe (and I may be wrong), that metadata is O(1). In laymans terms, with lots of players, metadata is faster.
    As far as events, since your use is very similar in each case, you can have the event call another method to run the code related to that specific case instead of making 3 events.
     
  3. Offline

    zotmer

    So regarding the events: would it then improve the resource efficiency if I switched to one event calling another methods?
     
  4. Offline

    mythbusterma

    @zotmer

    It would not make a single difference whatsoever. Modern computers and the amount of data you're dealing with mean you're likely worrying about nanoseconds. Do whatever makes your code the cleanest, and makes the most intuitive sense. In this case, it sounds like attaching metadata is the most sensible, since it's going to be an attribute of the pearl itself.


    @Tecno_Wizard

    HashMap access is constant time (in common parlance), assuming the hashcode isn't recalculated. This is because the worst case scenario is almost never seen in practice, and it is almost always relatively evenly distributed amongst the elements.

    I can pretty much guarantee that the metadata is backed by a HashMap, however if you can even measure the difference in time between storing it either way, I will be impressed.
     
  5. Offline

    Tecno_Wizard

    @mythbusterma I always forget to specify that everything i say in that regard is usually highly theoretical and usually not practical whatsoever. Unfortunately, I have to deal with professors who do care which way it is done, and most of them are pretty stubborn.

    Then there's the category when asked why the integral of 1/x^n when n>=1 diverges, the response i get is "because it increases faster :eek:"

    @zotmer mythbuster is a lot more correct than I am here. Take his word for it.
     
    mythbusterma likes this.
  6. Offline

    mythbusterma

    @Tecno_Wizard

    Sometimes professors are like that, I get it.

    Whenever the question is performance (at least in regards to MC servers), n is almost always less than 1000, and so it's not often practical to consider theoretical performance, especially when it comes to one data structure versus another.

    The only time performance is really an issue is when dealing with the Minecraft world (which is a huge amount of data with tiny amounts of process times that must be synchronous), or when dealing with remote queries, for example a database, where the time taken is dependent on network characteristics.
     
  7. Offline

    zotmer

    Ok, thanks both of you for explanation + some opinions regarding the performance. I now see that I am overly concerned about it. :)
     
Thread Status:
Not open for further replies.

Share This Page