Uploaded image for project: 'Minecraft: Java Edition'
  1. Minecraft: Java Edition
  2. MC-156374

High CPU spawning component with a player placed / flying > 128 blocks above the oceans

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Awaiting Response
    • None
    • Minecraft 1.14.4 Pre-Release 3
    • None
    • Windows
    • Unconfirmed
    • (Unassigned)

      Cause:
      With high cost of spawn attempts due to expensive block collision spawn check calculations and high memory and CPU costs for Entity constructor, it is imperative to avoid continuous attempts to spawn mobs in places where they wouldn't not be able to spawn.

      In certain scenarios, like when a player is located high above the oceans (more than 128 blocks), either via building a skybase, or simply flying with an elytra, this causes all water mobs to despawn and causing spawning for hostile, ambient and water mobs to trigger every tick. While hostile and ambient mobs would likely hit a solid or water block and failed to spawn before their instance are created, water mobs will still attempt to spawn and fail. This causes considerably high portion of the tick to fall onto mob spawning (more than 70% of the tick active time). The same can be observed in a flat world consisting of single layer of solid blocks, at night, and with player more than 128 blocks above the ground.

      Suggestion:
      This can be mitigated by moving the high distance check from after the mob is created and checked for collisions, to before, right where the distance to the closest player is established. One could notice that this check is accompanied with a check for mob immediate despawning condition, but despite various special cases of this method overriden in various mob classes, for mobs that spawn naturally the only extra check it bares is whether that mob is a passive mob or not. All naturally spawned mobs, that are hostile, ambient or water have that check TRUE.
      To conclude, the 'distance too far from the player' check can be moved up, with the 'ability to automatically despawn replaced with a check of the mob category, which is provided in the argument (see attached screenshot). Adding that extra check where the distance to the player is established:

      || (distanceTo > 16384.0D && entityCategory_1 != EntityCategory.CREATURE)
      

      reduces CPU spawning cost in those scenarios by about 50%

      Steps to reproduce / observe
      1. Create a random world (creative is fine)
      2. move away from spawn (not via teleport)
      3. find large ocean area
      4. go to Y > 192
      5. start profiler
      6. stop profiler

            Unassigned Unassigned
            gnembon [Mojang] Gnembon
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: