mojira.dev
MC-117075

Performance issue with unloading large number of block entities

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:

[media]

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:

[media]

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.

Attachments

Comments 10

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.

Still affects 1.15, both server and client-side

Can confirm for 1.16 pre release 5

Can reproduce in 1.17 Release Candidate 2.

Can confirm in 1.17.1.

Can confirm in 21w39a.

This bug was fixed in 1.18. BlockEntity are now removed by iterator.remove()

Marking as Cannot Reproduce then. I tested and got some lag spikes, but I think they're just from MC-162253. I will reopen this if anyone is still able to reproduce this.

mezz

(Unassigned)

Confirmed

Normal

Entities, Performance

block-entity, chunk, performance

Minecraft 1.11.2, Minecraft 1.13, Minecraft 1.13.1-pre2, Minecraft 1.13.1, 1.15, ..., 1.17 Pre-release 1, 1.17, 1.17.1, 21w39a, 21w44a

Retrieved