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

Performance issue with unloading large number of block entities

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Cannot Reproduce
    • None
    • Minecraft 1.11.2, Minecraft 1.13, Minecraft 1.13.1-pre2, Minecraft 1.13.1, 1.15, 1.15.2, 1.16 Release Candidate 1, 1.16, 20w27a, 1.16.2 Pre-release 1, 1.16.2 Release Candidate 2, 1.16.2, 1.16.3, 20w48a, 20w49a, 1.16.5, 1.17 Pre-release 1, 1.17, 1.17.1, 21w39a, 21w44a
    • Confirmed
    • Entities, Performance
    • Normal

      The bug

      When there are extreme numbers of block entities (even non-ticking) a lot of time can be spent unloading them from a list in the world. This happens when you have a huge number of block entities and leave the area.

      How to reproduce

      1. Create a new superflat world and /tp 0 ~ 0
      2. Create a lot of block entities spread over a few chunks: /fill 0 ~ 0 100 ~2 100 minecraft:furnace
      3. Fly away, witness huge lag spikes when block entities begin to be unloaded

      Sampling results looks like this: list.png

      Code analysis (using MCP names)

      Several fields in World.java use List methods with very bad time complexity.

      These fields are using add, remove, removeAll, and iterators:

      net.minecraft.world.World#loadedTileEntityList
      net.minecraft.world.World#tickableTileEntities
      

      After replacing those two Lists with Set, there is no lag spike and the sampling results look much better: set.png

      After discussing this with others, we think the best solution is to break up these lists so they are indexed by chunk.
      That way, an unloaded chunk can just drop its ticking list and there is no slow remove on a giant list.
      It could also make finding nearby entities more efficient.

        1. list.png
          list.png
          62 kB
        2. set.png
          set.png
          58 kB
        3. test1_after.png
          test1_after.png
          492 kB
        4. test2_after.png
          test2_after.png
          556 kB
        5. test1_before.png
          test1_before.png
          599 kB
        6. test2_before.png
          test2_before.png
          517 kB
        7. 2021-08-05_20_49_30-New World-1_17_1.zip
          142 kB

            Unassigned Unassigned
            mezz mezz
            Votes:
            43 Vote for this issue
            Watchers:
            22 Start watching this issue

              Created:
              Updated:
              Resolved:
              CHK: