InventivetalentDev / CompactNPCLib

Library to create NPCs
MIT License
22 stars 9 forks source link

Excessive CPU usage after several NPCs are spawned then removed. #29

Open null321-jared opened 7 years ago

null321-jared commented 7 years ago

What steps will reproduce the problem?

  1. Create a PlayerNPC
  2. Run this:
    npc.setShowInList(false);
    Main.registery.removeNpc(npc);
  3. Repeat steps 1 and 2 ~10 times

What were you expecting to happen? What happened instead?

I would expect that now the NPC is gone for it to have no impact on CPU. Instead, randomly, CPU usage jumps for the reflection methods as if there is an infinite loop or something wrong happening. Here is what the sampler says: http://imgur.com/a/hNUwa It's all on the main server thread, dropping my TPS to 5.

What version of the plugin are you using? Type /version <Plugin Name>

2.3.2, however this has happened on all versions of CompactNPCLib

What Spigot version are you using? Type /version

The screenshot is on 1.11, but it happens on all supported versions.

What plugins are you using? Type /plugins

Should not matter.

Do you have an error log? Use pastebin.com. If you're not sure, upload your whole server log

No errors.

Did your client crash? Upload errors in .minecraft/logs/latest.log as well

No crash.

Additional information? (Are you using Bungeecord? Did it work in previous versions? etc.)

No bungeecord. Has happened ever since I started using this plugin.

If you do not want to fix this, just accept my pull request and suggest a few possible things that could be causing this and I will try to fix it myself. Is the problem in ReflectionHelper or NPCLib?

null321-jared commented 7 years ago

This is what it did after a few minutes after spawning a few playerNPCs (Keep in mind it does it for every NPC, not just playerNPCs): https://timings.spigotmc.org/?url=hevogoyanu

Is there some way we can replace the Watcher system and instead use Bukkit events? I cannot use this on a public server if it cuts the TPS in a matter of minutes. I am willing to do this, but I need to know about it before I can do it.

How did you do it in the original NPC lib?

FormallyMyles commented 7 years ago

Would you be able to provide more information in relation to your profiler output? (Are you able to break it down more so more info about the methods with issues)

As an overview, it's possible the reflectionhelper could be optimised a bit more (precompiling patterns) but i'm unsure how much speed increase it could give:

https://github.com/InventivetalentDev/ReflectionHelper/blob/master/src/main/java/org/inventivetalent/reflection/resolver/wrapper/MethodWrapper.java#L246

Could be precompiled as a pattern and saved in the object but i'm unaware how many calls to this happen.

null321-jared commented 7 years ago

I just tried pre-compiling, however it creates the object once, then does the matching so it made no difference. Here is the profiler output: http://i.imgur.com/COYFxF9.png It stays at a steady CPU usage that is significantly higher than it should be, as if it is in some sort of infinite loop or is limited my something.