Solved How to optimise code

Discussion in 'Spigot Plugin Development' started by FatalPacket, May 30, 2017.

  1. My plugin, I believe is lagging my server.
    I want to know how to optimise it since that reason, I don't know how to.

    Can you give me some tips?

    Code: https://github.com/FinalPacket/Llama
     
    #1 FatalPacket, May 30, 2017
    Last edited: May 30, 2017
  2. Well, to start out, you might want to post your code. I can't help you much without it.
     
    • Agree Agree x 1
  3. And some timings, too, while you're at it.
     
  4. A common problem I've seen is people trying to put all of their code in the onEnable method, which usually involves a loop of some sort. The server can't start up fully until your onEnable method has finished running. Instead of that, use the bukkit scheduler, which allows you to run code every tick, and if I recall correctly, there are 20 ticks in one second. This may be your problem, but I have no way of knowing, seeing as you haven't posted any of your code yet.
     
  5. A more in-depth explanation about the game clock and ticks can be found here. I'm still waiting on your code so that I can give you more specific help.
     
  6. Oh sorry, I just say your reply. I'm looking into it now.
     
  7. The code is on the github..
     
  8. One thing I noticed is that every time a player moves, it iterates through all the checks. Maybe make it so that it only iterates through checks relevant to that. For instance, the FastBow check is probably not relevant each time a player moves. I'll keep looking for more things that could be slowing it down
     
  9. Any large chunk of code that is executed many times within a short time frame on the main server thread is going to cause your server to halt until it has finished processing all necessary functions, or possibly crash if it is too much. I havent looked at your code, but I suggest making some of the processing take place asynchronously.
     
    • Agree Agree x 1
  10. A couple of quick notes,
    Make your main class "Llama" final.
    Put "Bukkit.getServer().getClass().getPackage().getName().split("\\.")[3]" in a variable instead of calling it twice.
    PLEASE use @Override
    Make your Utils class final and non instantiable.
    Put "ChatColor.translateAlternateColorCodes" in a method, (probably in the utils class)

    Use DI, You should almost never need to use the static method for getting the instance of your main class.

    These are all things to improve your code as a whole and aren't really tied to the lagging, but still are important.

    (But also you are definitely lagging it with that move listener, keep in mind it is called whenever there is any movement at all, including just standing still and looking around)
     
  11. Like mentioned before, post timings of your plugin or profile it, it'll help narrow down what code should be examined.
     
  12. I know this isn't relevant to performance, but I doubt that the com.llama namespace belongs to you. That's a freelance writer website. Namespaces matter people.
     
  13. Haven't looked at your code too much, but some small measures I take to help optimize my code that it looks like you could probably use are:

    - Cache your configuration values when applicable - no point to iterate through a ConfigurationSection map every time you need a message or some other value stored there
    - Cache constant strings that are created using concatenation- so if you have player.sendMessage(ChatColor.BLUE+"Hi player"), you should really be assigning that string to a field so the concatenation only occurs once.
    - Finalize every single field that is not mutated
    - Structure your logic so that the least expensive operations are performed first

    However not following those points shouldn't cause any game breaking lag in most cases. So you should definitely listen to everyone above and post a timings of your server if something is creating severe lag.
     
  14. Its definitely the move listener, he has a bunch of calls to Location#distance, Location cloning, etc.
     
  15. Have You looked into concurrency, and/or multi-threading? Also just a tip static code is not used to make coding easier, it's used in situations where it's performance can help - Try and look into using Singleton instances of a class. Also Your 'Check' class should be abstrct.
     
  16. Clones should not be that costly, it's just allocating a new object (although he shouldn't use clone in the first place, add/subtract would be much more efficient)

    Having some form of profiling would be a rock solid way of verifying which part of code causes noticeable performance drop.
    Multithreading is not always the go to solution for performance, it has its costs. Also, static is not a performance tool either, but a memory tool.
     
  17. I was just listing what I saw happening every time PlayerMoveEvent was called, those two are just the ones I see a lot of.