Lithium (Fabric)

Lithium (Fabric)

22M Downloads

Bee pathfinding and possibly other flying mobs causes a performance hit on servers when in chaotic landscape

TelepathicGrunt opened this issue ยท 2 comments

commented

Bear with me as I try my best to describe the issue. So originally, I made a mod called The Bumblezone which is a dimension where the terrain is made to look like the inside of a beehive. However, I have noticed when I started populating the dimension with Bees, the server ticks starts to lag significantly to really bad levels. I have isolated the issue to just bees as when I do /kill @e[type=bee], the performance goes back to normal. Furthermore, when I cut the terrain and entities, put them into the overworld, and run the game in vanilla, the extreme performance hit is still there. Attached is the world and I did my tests at -111, 17, 20.
New World - Copy.zip

Furthermore, I have found some interesting stuff as I tested the performance hits. If I kill all bees (usually just 181 bees killed) and try to spawn the same number of bees using a repeating command block, the performance hit is bad but not as bad as before. But when I exit the world and re-enter the world with these command summoned bees, the performance actually gets worse and back to the level that the naturally spawned bees were at. Super odd.

One thing to note is you cannot reproduce this bee performance hit with vanilla terrain. It seems it has to be very vertical or chaotic terrain before the bee's pathfinding starts really affecting the server. Lithium does help a lot with optimizing the mob AI and reduces this issue but perhaps it could be improved even more? Not sure if it is possible but it might be worth checking out!

Attached are pictures of the various tests I have done.

Original performance hit when using my mod and the performance improvement with Lithium. (70 tps to 30 tps)
2020-09-10_14 05 24

The attached world with 181 natural bees and after they are killed by command. (30 tps to 2 tps)
2020-09-10_22 32 43

The attached world with hundreds of command summoned bees and after exiting/re-entering world. (25 tps to 35 tps)
2020-09-10_22 34 09

Normal vanilla terrain with 594 bees and still remaining at 25 tps and less visible server lag than with previous terrains with only 1/3rd amount of bees.
2020-09-10_22 41 15

Intellij profiler I did a long time ago to try and find what was lagging my mod. This is what tipped me off that vanilla bee's pathfinding AI was the issue
image

commented

Most of the slowness here was being caused by the path-finding algorithm looking for neighboring danger sources all the time, which accounted for nearly 90% of the block reads. I've implemented a cache tonight (commit) which tries to avoid scanning blocks when we know a chunk to be free of dangers, but it won't help any when a chunk section is dangerous.

Outside of that, said commit also introduces some optimizations for those block reads, so even if we can't avoid them, we can at least perform them more quickly.

All that said, I measured a ~25% reduction in tick time on an otherwise silent server with 300 bees present with the optimized block read code, and another ~25% reduction when the cache is hit (which should happen most of the time.)

A build with these improvements can be found here if you're wanting to try it out. I'll close this issue if you're satisfied with the improvements.

commented

Yeah the improvement is amazing! Thank you!
I'll recommend people to use Lithium too as I wanna fill my mod's dimension to the ceiling of BEEESSSS!!!