2016-04-13 22:57:34 +02:00
From ee06b0730cdcaa25235e3fec5594288200403a26 Mon Sep 17 00:00:00 2001
2016-03-18 23:03:44 +01:00
From: Aikar <aikar@aikar.co>
Date: Fri, 18 Mar 2016 17:57:25 -0400
Subject: [PATCH] Optimize Chunk Unload Queue
Removing chunks from the unload queue when performing chunk lookups is a costly activity.
It drastically slows down server performance as many methods call getChunkAt, resulting in a bandaid
to skip removing chunks from the unload queue.
This patch optimizes the unload queue to instead use a boolean on the Chunk object itself to mark
if the chunk is active, and then insert into a LinkedList queue.
The benefits here is that all chunk unload queue actions are now O(1) constant time.
A LinkedList will never need to resize, and can be removed from in constant time when
used in a queue like method.
We mark the chunk as active in many places that notify it is still being used, so that
when the chunk unload queue reaches that chunk, and sees the chunk became active again,
it will skip it and move to next.
2016-03-22 03:51:14 +01:00
Also optimize EAR to use these methods.
2016-03-18 23:03:44 +01:00
diff --git a/src/main/java/net/minecraft/server/Chunk.java b/src/main/java/net/minecraft/server/Chunk.java
2016-03-29 03:01:42 +02:00
index 8b11266..ac35cf4 100644
2016-03-18 23:03:44 +01:00
--- a/src/main/java/net/minecraft/server/Chunk.java
+++ b/src/main/java/net/minecraft/server/Chunk.java
2016-03-25 07:38:38 +01:00
@@ -53,6 +53,8 @@ public class Chunk {
2016-03-18 23:03:44 +01:00
// Keep this synced with entitySlices.add() and entitySlices.remove()
private final int[] itemCounts = new int[16];
private final int[] inventoryEntityCounts = new int[16];
+ public boolean isChunkActive = true;
+ public boolean isInUnloadQueue = false;
// Paper end
// CraftBukkit start - Neighbor loaded cache for chunk lighting and entity ticking
diff --git a/src/main/java/net/minecraft/server/ChunkProviderServer.java b/src/main/java/net/minecraft/server/ChunkProviderServer.java
2016-04-03 09:02:00 +02:00
index 450bf9b..5485a66 100644
2016-03-18 23:03:44 +01:00
--- a/src/main/java/net/minecraft/server/ChunkProviderServer.java
+++ b/src/main/java/net/minecraft/server/ChunkProviderServer.java
@@ -21,7 +21,7 @@ import org.bukkit.event.world.ChunkUnloadEvent;
public class ChunkProviderServer implements IChunkProvider {
private static final Logger a = LogManager.getLogger();
- public final LongHashSet unloadQueue = new LongHashSet(); // CraftBukkit - LongHashSet
+ public final ChunkUnloadQueue unloadQueue = new ChunkUnloadQueue(); // CraftBukkit - LongHashSet // Paper -> ChunkUnloadQueue
public final ChunkGenerator chunkGenerator; // CraftBukkit - public
private final IChunkLoader chunkLoader;
public LongObjectHashMap<Chunk> chunks = new LongObjectHashMap<Chunk>(); // CraftBukkit
2016-03-25 07:38:38 +01:00
@@ -77,14 +77,24 @@ public class ChunkProviderServer implements IChunkProvider {
2016-03-22 03:51:14 +01:00
return (chunk == null) ? getChunkAt(x, z) : chunk;
}
2016-03-18 23:03:44 +01:00
2016-03-22 03:51:14 +01:00
- public Chunk getChunkIfLoaded(int x, int z) {
- return chunks.get(LongHash.toLong(x, z));
2016-03-19 16:29:46 +01:00
+ // Paper start
+ public Chunk getLoadedChunkAtWithoutMarkingActive(int i, int j) {
+ return chunks.get(LongHash.toLong(i, j));
2016-03-20 05:13:20 +01:00
}
+ // getChunkIfLoaded -> getChunkIfActive
+ // this is only used by CraftBukkit now, and plugins shouldnt mark things active
+ public Chunk getChunkIfActive(int x, int z) {
+ Chunk chunk = chunks.get(LongHash.toLong(x, z));
+ return (chunk != null && chunk.isChunkActive) ? chunk : null;
2016-03-22 03:51:14 +01:00
+ }
2016-03-20 05:13:20 +01:00
+ // Paper end
2016-03-22 03:51:14 +01:00
+
2016-03-18 23:03:44 +01:00
public Chunk getLoadedChunkAt(int i, int j) {
Chunk chunk = chunks.get(LongHash.toLong(i, j)); // CraftBukkit
2016-03-22 03:51:14 +01:00
- this.unloadQueue.remove(i, j); // CraftBukkit
+ //this.unloadQueue.remove(i, j); // CraftBukkit // Paper
+ markChunkActive(chunk); // Paper
return chunk;
}
2016-03-25 07:38:38 +01:00
@@ -112,6 +122,7 @@ public class ChunkProviderServer implements IChunkProvider {
2016-03-22 03:51:14 +01:00
// CraftBukkit end
}
+ markChunkActive(chunk); // Paper
2016-03-18 23:03:44 +01:00
return chunk;
}
2016-03-25 07:38:38 +01:00
@@ -120,7 +131,7 @@ public class ChunkProviderServer implements IChunkProvider {
2016-03-22 03:51:14 +01:00
}
public Chunk getChunkAt(int i, int j, Runnable runnable) {
- unloadQueue.remove(i, j);
+ //unloadQueue.remove(i, j); // Paper
Chunk chunk = chunks.get(LongHash.toLong(i, j));
ChunkRegionLoader loader = null;
2016-03-25 07:38:38 +01:00
@@ -144,12 +155,13 @@ public class ChunkProviderServer implements IChunkProvider {
2016-03-22 03:51:14 +01:00
if (runnable != null) {
2016-03-18 23:03:44 +01:00
runnable.run();
}
2016-03-22 03:51:14 +01:00
+ markChunkActive(chunk); // Paper
2016-03-18 23:03:44 +01:00
return chunk;
}
2016-03-22 03:51:14 +01:00
public Chunk originalGetChunkAt(int i, int j) {
- this.unloadQueue.remove(i, j);
+ //this.unloadQueue.remove(i, j); // Paper
Chunk chunk = this.chunks.get(LongHash.toLong(i, j));
boolean newChunk = false;
// CraftBukkit end
2016-03-25 07:38:38 +01:00
@@ -195,7 +207,7 @@ public class ChunkProviderServer implements IChunkProvider {
2016-03-19 16:29:46 +01:00
continue;
}
- Chunk neighbor = this.getChunkIfLoaded(chunk.locX + x, chunk.locZ + z);
+ Chunk neighbor = this.getLoadedChunkAtWithoutMarkingActive(chunk.locX + x, chunk.locZ + z); // Paper
if (neighbor != null) {
neighbor.setNeighborLoaded(-x, -z);
chunk.setNeighborLoaded(x, z);
2016-03-25 07:38:38 +01:00
@@ -206,6 +218,7 @@ public class ChunkProviderServer implements IChunkProvider {
2016-03-22 03:51:14 +01:00
chunk.loadNearby(this, this.chunkGenerator);
world.timings.syncChunkLoadTimer.stopTiming(); // Spigot
}
+ markChunkActive(chunk); // Paper
return chunk;
}
2016-03-25 07:38:38 +01:00
@@ -294,10 +307,17 @@ public class ChunkProviderServer implements IChunkProvider {
2016-03-18 23:03:44 +01:00
if (!this.world.savingDisabled) {
// CraftBukkit start
Server server = this.world.getServer();
- for (int i = 0; i < 100 && !this.unloadQueue.isEmpty(); ++i) {
- long chunkcoordinates = this.unloadQueue.popFirst();
- Chunk chunk = this.chunks.get(chunkcoordinates);
- if (chunk == null) continue;
+ // Paper start
+ final Iterator<Chunk> iterator = this.unloadQueue.iterator();
+ for (int i = 0; i < 100 && iterator.hasNext(); ++i) {
+ Chunk chunk = iterator.next();
+ iterator.remove();
+ long chunkcoordinates = LongHash.toLong(chunk.locX, chunk.locZ);
+ chunk.isInUnloadQueue = false;
+ if (chunk.isChunkActive) {
+ continue;
+ }
+ // Paper end
2016-03-25 07:38:38 +01:00
if (chunk.hasLightUpdates()) continue; // Paper - Don't unload chunks with pending light updates.
2016-03-18 23:03:44 +01:00
ChunkUnloadEvent event = new ChunkUnloadEvent(chunk.bukkitChunk);
2016-04-03 09:02:00 +02:00
@@ -320,13 +340,18 @@ public class ChunkProviderServer implements IChunkProvider {
2016-03-19 16:29:46 +01:00
continue;
}
- Chunk neighbor = this.getChunkIfLoaded(chunk.locX + x, chunk.locZ + z);
+ Chunk neighbor = this.getLoadedChunkAtWithoutMarkingActive(chunk.locX + x, chunk.locZ + z); // Paper
if (neighbor != null) {
neighbor.setNeighborUnloaded(-x, -z);
chunk.setNeighborUnloaded(x, z);
2016-04-03 09:02:00 +02:00
}
}
}
+ // Paper start
+ } else {
+ // Unload event is cancelled, make sure this chunk is marked active
+ markChunkActive(chunk);
+ // Paper end
}
}
// CraftBukkit end
@@ -362,4 +387,69 @@ public class ChunkProviderServer implements IChunkProvider {
2016-03-18 23:03:44 +01:00
public boolean e(int i, int j) {
return this.chunks.containsKey(LongHash.toLong(i, j)); // CraftBukkit
}
+
+ // Paper start
2016-03-22 03:51:14 +01:00
+ public static void markChunkActive(Chunk chunk) {
+ if (chunk != null) {
+ chunk.isChunkActive = true;
+ }
+ }
+ public class ChunkUnloadQueue extends LongHashSet { // Provide compat with NMS plugins
+ private java.util.LinkedList<Chunk> unloadQueue = new java.util.LinkedList<Chunk>();
+
+ @Override
+ public boolean isEmpty() {
+ return unloadQueue.isEmpty();
+ }
+
+ @Override
+ public boolean contains(long value) {
+ throw new UnsupportedOperationException("contains on unload queue");
+ }
+
+ @Override
+ public boolean add(long value) {
+ throw new UnsupportedOperationException("add on unload queue");
+ }
+
+ @Override
+ public boolean remove(long value) {
+ throw new UnsupportedOperationException("remove on unload queue");
+ }
+
+ @Override
+ public int size() {
+ return unloadQueue.size();
+ }
+
+ @Override
+ public Iterator iterator() {
+ return unloadQueue.iterator();
+ }
+
+ @Override
+ public boolean contains(int x, int z) {
+ final Chunk chunk = chunks.get(LongHash.toLong(x, z));
+ return (chunk != null && chunk.isInUnloadQueue);
+ }
+
2016-03-18 23:03:44 +01:00
+ public void remove(int x, int z) {
2016-03-22 03:51:14 +01:00
+ final Chunk chunk = chunks.get(LongHash.toLong(x, z));
+ if (chunk != null) {
+ chunk.isChunkActive = true;
+ }
2016-03-18 23:03:44 +01:00
+ }
2016-03-22 03:51:14 +01:00
+ public boolean add(int x, int z) {
2016-03-18 23:03:44 +01:00
+ final Chunk chunk = chunks.get(LongHash.toLong(x, z));
+ if (chunk != null) {
+ chunk.isChunkActive = false;
+ if (!chunk.isInUnloadQueue) {
+ chunk.isInUnloadQueue = true;
2016-03-22 03:51:14 +01:00
+ return unloadQueue.add(chunk);
2016-03-18 23:03:44 +01:00
+ }
+ }
2016-03-22 03:51:14 +01:00
+ return false;
2016-03-18 23:03:44 +01:00
+ }
+ }
+ // Paper end
}
diff --git a/src/main/java/net/minecraft/server/SpawnerCreature.java b/src/main/java/net/minecraft/server/SpawnerCreature.java
2016-04-13 22:57:34 +02:00
index e15cc1a..dc7c17c 100644
2016-03-18 23:03:44 +01:00
--- a/src/main/java/net/minecraft/server/SpawnerCreature.java
+++ b/src/main/java/net/minecraft/server/SpawnerCreature.java
@@ -28,7 +28,7 @@ public final class SpawnerCreature {
Long coord = it.next();
int x = LongHash.msw( coord );
int z = LongHash.lsw( coord );
- if ( !((ChunkProviderServer)server.chunkProvider).unloadQueue.contains( coord ) && server.isChunkLoaded( x, z, true ) )
+ if ( server.isChunkLoaded( x, z, true ) ) // Paper
{
i += server.getChunkAt( x, z ).entityCount.get( oClass );
}
2016-03-20 05:13:20 +01:00
diff --git a/src/main/java/net/minecraft/server/World.java b/src/main/java/net/minecraft/server/World.java
2016-04-13 22:57:34 +02:00
index c6f15b8..b223847 100644
2016-03-20 05:13:20 +01:00
--- a/src/main/java/net/minecraft/server/World.java
+++ b/src/main/java/net/minecraft/server/World.java
2016-04-13 22:57:34 +02:00
@@ -160,9 +160,15 @@ public abstract class World implements IBlockAccess {
2016-03-22 03:51:14 +01:00
// Paper end
2016-03-20 05:13:20 +01:00
public Chunk getChunkIfLoaded(int x, int z) {
- return ((ChunkProviderServer) this.chunkProvider).getChunkIfLoaded(x, z);
+ return ((ChunkProviderServer) this.chunkProvider).getLoadedChunkAtWithoutMarkingActive(x, z); // Paper - This is added by CB, and will not mark as active. Simply an alias
}
+ // Paper start
+ public Chunk getChunkIfActive(int x, int z) {
+ return ((ChunkProviderServer) this.chunkProvider).getChunkIfActive(x, z);
+ }
+ // Paper end
+
protected World(IDataManager idatamanager, WorldData worlddata, WorldProvider worldprovider, MethodProfiler methodprofiler, boolean flag, ChunkGenerator gen, org.bukkit.World.Environment env) {
this.spigotConfig = new org.spigotmc.SpigotWorldConfig( worlddata.getName() ); // Spigot
this.paperConfig = new com.destroystokyo.paper.PaperWorldConfig(worlddata.getName(), this.spigotConfig); // Paper
2016-03-18 23:03:44 +01:00
diff --git a/src/main/java/org/bukkit/craftbukkit/CraftWorld.java b/src/main/java/org/bukkit/craftbukkit/CraftWorld.java
2016-04-04 16:53:03 +02:00
index dfa7e73..922ebaf 100644
2016-03-18 23:03:44 +01:00
--- a/src/main/java/org/bukkit/craftbukkit/CraftWorld.java
+++ b/src/main/java/org/bukkit/craftbukkit/CraftWorld.java
2016-04-04 16:53:03 +02:00
@@ -210,7 +210,7 @@ public class CraftWorld implements World {
2016-03-20 05:13:20 +01:00
2016-04-04 16:53:03 +02:00
private boolean unloadChunk0(int x, int z, boolean save, boolean safe) {
2016-03-20 05:13:20 +01:00
// Paper start - Don't create a chunk just to unload it
- net.minecraft.server.Chunk chunk = world.getChunkProviderServer().getChunkIfLoaded(x, z);
+ net.minecraft.server.Chunk chunk = world.getChunkProviderServer().getChunkIfActive(x, z); // Paper
if (chunk == null) {
return false;
}
2016-04-04 16:53:03 +02:00
@@ -237,7 +237,7 @@ public class CraftWorld implements World {
2016-03-19 16:29:46 +01:00
continue;
}
- net.minecraft.server.Chunk neighbor = world.getChunkProviderServer().getChunkIfLoaded(chunk.locX + x, chunk.locZ + z);
+ net.minecraft.server.Chunk neighbor = world.getChunkProviderServer().getLoadedChunkAtWithoutMarkingActive(chunk.locX + x, chunk.locZ + z); // Paper
if (neighbor != null) {
neighbor.setNeighborUnloaded(-xx, -zz);
chunk.setNeighborUnloaded(xx, zz);
2016-04-04 16:53:03 +02:00
@@ -299,6 +299,7 @@ public class CraftWorld implements World {
2016-03-22 03:51:14 +01:00
// Use the default variant of loadChunk when generate == true.
return world.getChunkProviderServer().getChunkAt(x, z) != null;
}
+ if (true) { return world.getChunkProviderServer().getOrLoadChunkAt(x, z) != null; } // Paper
2016-03-18 23:03:44 +01:00
2016-03-22 03:51:14 +01:00
world.getChunkProviderServer().unloadQueue.remove(x, z);
net.minecraft.server.Chunk chunk = world.getChunkProviderServer().chunks.get(LongHash.toLong(x, z));
2016-04-04 16:53:03 +02:00
@@ -324,7 +325,7 @@ public class CraftWorld implements World {
2016-03-19 16:29:46 +01:00
continue;
}
- net.minecraft.server.Chunk neighbor = world.getChunkProviderServer().getChunkIfLoaded(chunk.locX + x, chunk.locZ + z);
+ net.minecraft.server.Chunk neighbor = world.getChunkProviderServer().getLoadedChunkAtWithoutMarkingActive(chunk.locX + x, chunk.locZ + z); // Paper
if (neighbor != null) {
neighbor.setNeighborLoaded(-x, -z);
chunk.setNeighborLoaded(x, z);
2016-04-04 16:53:03 +02:00
@@ -1541,7 +1542,7 @@ public class CraftWorld implements World {
2016-03-18 23:03:44 +01:00
}
// Already unloading?
- if (cps.unloadQueue.contains(chunk.locX, chunk.locZ)) {
2016-03-22 03:51:14 +01:00
+ if (chunk.isInUnloadQueue) { // Paper
2016-03-18 23:03:44 +01:00
continue;
}
2016-03-19 16:29:46 +01:00
diff --git a/src/main/java/org/bukkit/craftbukkit/chunkio/ChunkIOProvider.java b/src/main/java/org/bukkit/craftbukkit/chunkio/ChunkIOProvider.java
index 482af17..a1a6d5a 100644
--- a/src/main/java/org/bukkit/craftbukkit/chunkio/ChunkIOProvider.java
+++ b/src/main/java/org/bukkit/craftbukkit/chunkio/ChunkIOProvider.java
@@ -62,7 +62,7 @@ class ChunkIOProvider implements AsynchronousExecutor.CallBackProvider<QueuedChu
continue;
}
- Chunk neighbor = queuedChunk.provider.getChunkIfLoaded(chunk.locX + x, chunk.locZ + z);
+ Chunk neighbor = queuedChunk.provider.getLoadedChunkAtWithoutMarkingActive(chunk.locX + x, chunk.locZ + z); // Paper
if (neighbor != null) {
neighbor.setNeighborLoaded(-x, -z);
chunk.setNeighborLoaded(x, z);
2016-03-20 05:13:20 +01:00
diff --git a/src/main/java/org/spigotmc/ActivationRange.java b/src/main/java/org/spigotmc/ActivationRange.java
2016-04-02 04:08:40 +02:00
index f0d3a19..c32b805 100644
2016-03-20 05:13:20 +01:00
--- a/src/main/java/org/spigotmc/ActivationRange.java
+++ b/src/main/java/org/spigotmc/ActivationRange.java
2016-03-25 05:59:37 +01:00
@@ -126,9 +126,9 @@ public class ActivationRange
2016-03-22 03:51:14 +01:00
{
for ( int j1 = k; j1 <= l; ++j1 )
{
- if ( world.getWorld().isChunkLoaded( i1, j1 ) )
- {
- activateChunkEntities( world.getChunkAt( i1, j1 ) );
+ Chunk chunk = world.getChunkIfActive(i1, j1); // Paper
+ if (chunk != null) { // Paper
+ activateChunkEntities( chunk ); // Paper
}
}
}
2016-03-25 05:59:37 +01:00
@@ -276,7 +276,7 @@ public class ActivationRange
2016-03-20 05:13:20 +01:00
int x = MathHelper.floor( entity.locX );
int z = MathHelper.floor( entity.locZ );
// Make sure not on edge of unloaded chunk
- Chunk chunk = entity.world.getChunkIfLoaded( x >> 4, z >> 4 );
2016-03-22 03:51:14 +01:00
+ Chunk chunk = isActive ? entity.world.getChunkIfActive( x >> 4, z >> 4 ) : null; // Paper
2016-03-20 05:13:20 +01:00
if ( isActive && !( chunk != null && chunk.areNeighborsLoaded( 1 ) ) )
{
isActive = false;
2016-03-18 23:03:44 +01:00
--
2016-04-13 22:57:34 +02:00
2.8.0
2016-03-18 23:03:44 +01:00