Foxikle / CustomNPCs

Foxikle's NPC plugin.
https://foxikle.dev/plugins
MIT License
7 stars 3 forks source link

Significantly optimized every listener in Listeners.java #46

Closed FlameyosSnowy closed 7 months ago

FlameyosSnowy commented 7 months ago

I made some amazing changes for the sake of performance of the library :>

[!CAUTION] Might need a little bit of testing.

The most significant performance improvements include but are not limited to:

[!WARNING]
Listeners#stop method should definitely be called at JavaPlugin#onDisable so that the cached threads in the ScheduledExecutorService would shut down and unallocate allocated memory

Now, There was just two issues when I looked at this code:

  1. There was some really unnecessary nesting in that code, by being a natural never-nester, I made this code good myself (because I coded optimizations for this class so I'm gonna be just a little bit narcissist) so unless it was necessary, I removed any nesting and replaced it with a "return" or "continue" that would later be removed by the compiler anyways and nest the code for us.

  2. There is a possibility that a player CAN leave the server while the plugin waits for them to do something; like name an NPC, or wait for a message, or add args, etc. There would be dead Player objects that just don't do anything and take up memory, and Player objects take LOTS of memory.

    to fix this issue; there were 3 ways to fix this issue:

    • Convert Player into UUIDs since UUID take only 16 bits of ram (excluding the class initialization memory consumption itself) since it only contains 2 longs per instance.
    • Properly remove Players from Sets and Lists
    • Sleep eight hours in college without faili- I mean give the player loads of problems to deal with when rejoining. Now the last option can be a config (just fail college) by just not removing the stuff on leave.

Fixing these issue can improve code quality and code maintainability + readability between others.

These were not made by an IDE (D:) but these were compile-time tested.