Poor performance of the Entity AI
CovertJaguar opened this issue ยท 8 comments
The way the AI currently works, every animal in the world is given an AI task that scans an absolutely massive area multiple times a tick. This brutal on performance.
Recommended solution, have the blocks scan for entities once every several seconds and if it find the right kind of entity, add the task if it doesn't already exist, with the source block location being a variable in the AI Task object. If it exists, add some kind logic for deciding how to update the target block in the case of multiple valid blocks.
Then only check that single block location in the AI Task's shouldExecute() function.
A config option would be nice too.
Related to #18
If you feel like working on this, I encourage you to open a PR. If not I'll have to look into it. I have known for a long time that the AI part is flawed, but I never really realized how badly.
The data that @sfPlayer1 was able to extract from the server using his Sampler mod indicated that it was using upwards of 80% tick time on the server. So yeah, its significant.
I agree. This is top priority. Are you interested in opening a PR yourself or should I go ahead?
This was fixed as of 78a0196
@Ferdzz I have few suggestions about algorithm to improve the AI. You could simply add an interval for scanning (so instead of every tick, you just scan it once every second).
Or instead of scanning the nearby blocks, you could implement such system which is going to store locations of given blocks (on chunk loads), and update those (by capturing block changes based using IWorldEventListener
), and removing those positions once the chunks are unloaded. This also might require implementing a quad tree or something (for faster fetch).
Thank you @mchorse for those suggestions, I will consider them as potential solutions. For the time being the AI is disabled and I will mark this issue as Help Wanted in case anyone is interested in picking it up, since I'm going to concentrate on more interactions and updating the textures and models for an upcoming update.