Fossils and Archeology: Revival

Fossils and Archeology: Revival

6M Downloads

Lag in EntityPrehistoric from repeated BlockPos objects creation

LemADEC opened this issue ยท 1 comments

commented

As of version 8.0.4, lag is observed in the EntityPrehistoric.arePlantsNearby().
When a player has no plants near its prehistoric animal, lag spikes are observed from this check.

The related code is https://github.com/FossilsArcheologyRevival/FossilsArcheologyRevival/blob/1.12.2/src/main/java/fossilsarcheology/server/entity/prehistoric/EntityPrehistoric.java#L491-L502
This code is always called with a range of 16, every 3000 to 8000 ticks (~5mn).
The method is scanning up to a 33 x 17 x 33 = 18513 blocks.
Existing code is creating up to 18k BlockPos objects, causing a secondary lag from the ensuing GC.
Consider using BlockPos.getAllInBoxMutable() which an Iterate returning MutableBlockPos that'll remove the repeated allocations.
Consider reducing the range or scanning progressively to remove the initial lag spike. Maybe use a range of 16 + entity size?

Furthermore, from the look of it, this method is reloading chunks, causing lag.
Consider skipping the mood check completely if the corner chunks aren't loaded.

In the same class, we can see similar loops that would cause lag spikes:

  • getNearestFeeder reallocating in a loop (is it dead code?)
  • breakBlock allocating 3 times the same BlockPos for every blocks in range
  • isInWaterMaterial double allocation of BlockPos objects
  • getBlockToEat reallocating BlockPos objects in a loop
    Consider using a MutableBlockPos property to save from constant reallocation in those simple checks.
commented

fixed