MinecraftPortCentral/Cauldron — pull request #1664 by wildex999 (2014-08-30 05:39:57+00:00)
Title: v164 Chunk Unload performance improvement

**[Adding this to 1.6.4 due to that being the modpack I'm using. Pushing this to 1.7/1.8 shouldn't be too hard]**
This is a simple addition to improve Chunk Unloading performance with Tile Entity heavy Chunks.
It's more targeted towards heavily modded servers more than Vanilla servers.

One of the problems I have discovered on modded server that have been on the same world for a while, is that there are a lot of Tile Entities in the world.
With the growth of the world, and as more and more players build in the world, server admins may notice that there are hard to track lag spikes that often end up causing player disconnects.

From my own running of such a server I have found that a loaded server can suddenly struggle a lot with unloading chunks, if the world has a lot of loaded chunks(Think > 100 000 TE's) and it's trying to unload > 1000 TE's, usually due to a player leaving their nice large base.

This can cause upwards to multiple seconds of the server not responding. Often not enough to trigger the Warning or Shutdown of WatchDog, but enough to start disconnecting players. Of course, that can lead to a chain effect of one player causing others to disconnect, which causes the server to just continue hanging as it's struggling to unload all the chunks and their Tile Entities.

This patch will add the ability to stop unloading chunks for this tick after reaching a certain number of Tile Entities. This limit can be changed in the config, and there are also some debugging to show the amount of chunk in unload queue, and the time spent unloading Tile Entities.

I have left the debugging settings to enabled by default, as I believe this problem is not well known among server admins(The usual fix is a world wipe), and they might benefit from knowing what the server is doing when it suddenly stops responding for half a second every now and then.

Note: This is really just a partial fix to a deeper problem. It will stop unloading chunks and leave them for the next tick when it reaches a TE limit, but if a chunk has a lot of Tile Entities it will still go over the limit, and can cause a noticeable spike.

The deeper problem that might need to be fixed is the Tile Entity unloading part:
```java
this.loadedTileEntityList.removeAll(this.entityRemoval);
```
at World.java : updateEntities().
This is essentially a O(n*m) complexity operation on TOP of the TE's unload cost.
n = Number of TE's to unload
m = Number of current TE's in world.
Not to mention loadedTileEntityList is a ArrayList which needs to rearrange it's entries after this operation.