2016-03-31 00:50:23 +00:00
|
|
|
From 90e134ecf1742e7882e0b1d9a0fea51236fd0317 Mon Sep 17 00:00:00 2001
|
2016-03-22 05:46:32 +00:00
|
|
|
From: Aikar <aikar@aikar.co>
|
|
|
|
Date: Thu, 27 Aug 2015 01:15:02 -0400
|
|
|
|
Subject: [PATCH] Optimize Chunk Access
|
|
|
|
|
|
|
|
getting a loaded chunk is one of the most hottest pieces of code in the game.
|
2016-03-23 03:44:35 +00:00
|
|
|
getChunkAt is called for the same chunk multiple times in a row, often from getType();
|
2016-03-22 05:46:32 +00:00
|
|
|
|
|
|
|
Optimize this look up by using a Last Access cache.
|
|
|
|
|
|
|
|
diff --git a/src/main/java/net/minecraft/server/Chunk.java b/src/main/java/net/minecraft/server/Chunk.java
|
2016-03-29 01:01:42 +00:00
|
|
|
index ac35cf4..6e3d17b 100644
|
2016-03-22 05:46:32 +00:00
|
|
|
--- a/src/main/java/net/minecraft/server/Chunk.java
|
|
|
|
+++ b/src/main/java/net/minecraft/server/Chunk.java
|
2016-03-25 06:38:38 +00:00
|
|
|
@@ -30,6 +30,7 @@ public class Chunk {
|
2016-03-22 05:46:32 +00:00
|
|
|
private boolean i;
|
|
|
|
public final World world;
|
|
|
|
public final int[] heightMap;
|
|
|
|
+ public final long chunkKey; // Paper
|
|
|
|
public final int locX;
|
|
|
|
public final int locZ;
|
|
|
|
private boolean l;
|
2016-03-25 06:38:38 +00:00
|
|
|
@@ -97,6 +98,7 @@ public class Chunk {
|
2016-03-22 05:46:32 +00:00
|
|
|
this.world = world;
|
|
|
|
this.locX = i;
|
|
|
|
this.locZ = j;
|
|
|
|
+ this.chunkKey = org.bukkit.craftbukkit.util.LongHash.toLong(this.locX, this.locZ); // Paper
|
|
|
|
this.heightMap = new int[256];
|
|
|
|
|
|
|
|
for (int k = 0; k < this.entitySlices.length; ++k) {
|
|
|
|
diff --git a/src/main/java/net/minecraft/server/ChunkProviderServer.java b/src/main/java/net/minecraft/server/ChunkProviderServer.java
|
2016-03-25 06:38:38 +00:00
|
|
|
index f5a2580..f31ffe2 100644
|
2016-03-22 05:46:32 +00:00
|
|
|
--- a/src/main/java/net/minecraft/server/ChunkProviderServer.java
|
|
|
|
+++ b/src/main/java/net/minecraft/server/ChunkProviderServer.java
|
|
|
|
@@ -24,7 +24,18 @@ public class ChunkProviderServer implements IChunkProvider {
|
|
|
|
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
|
|
|
|
+ // Paper start
|
|
|
|
+ protected Chunk lastChunkByPos = null;
|
|
|
|
+ public LongObjectHashMap<Chunk> chunks = new LongObjectHashMap<Chunk>() {
|
|
|
|
+ @Override
|
|
|
|
+ public Chunk get(long key) {
|
|
|
|
+ if (lastChunkByPos != null && key == lastChunkByPos.chunkKey) {
|
|
|
|
+ return lastChunkByPos;
|
|
|
|
+ }
|
2016-03-23 03:44:35 +00:00
|
|
|
+ return lastChunkByPos = super.get(key);
|
2016-03-22 05:46:32 +00:00
|
|
|
+ }
|
|
|
|
+ }; // CraftBukkit
|
|
|
|
+ // Paper end
|
|
|
|
// private final LongHashMap<Chunk> chunks = new LongHashMap();
|
|
|
|
// private final List<Chunk> chunkList = Lists.newArrayList();
|
|
|
|
public final WorldServer world;
|
2016-03-25 06:38:38 +00:00
|
|
|
@@ -53,6 +64,7 @@ public class ChunkProviderServer implements IChunkProvider {
|
2016-03-22 05:46:32 +00:00
|
|
|
|
|
|
|
Chunk c = chunks.get(LongHash.toLong(i, j));
|
|
|
|
if (c != null) {
|
|
|
|
+ world.testResetChunkCache(c); // Paper
|
|
|
|
c.mustSave = true;
|
|
|
|
}
|
|
|
|
// CraftBukkit end
|
2016-03-25 06:38:38 +00:00
|
|
|
@@ -328,6 +340,7 @@ public class ChunkProviderServer implements IChunkProvider {
|
2016-03-22 05:46:32 +00:00
|
|
|
chunk.removeEntities();
|
|
|
|
this.saveChunk(chunk);
|
|
|
|
this.saveChunkNOP(chunk);
|
|
|
|
+ world.testResetChunkCache(chunk); // Paper
|
|
|
|
this.chunks.remove(chunkcoordinates); // CraftBukkit
|
|
|
|
}
|
|
|
|
|
|
|
|
diff --git a/src/main/java/net/minecraft/server/World.java b/src/main/java/net/minecraft/server/World.java
|
2016-03-29 01:01:42 +00:00
|
|
|
index 0a3cfbc..825e931 100644
|
2016-03-22 05:46:32 +00:00
|
|
|
--- a/src/main/java/net/minecraft/server/World.java
|
|
|
|
+++ b/src/main/java/net/minecraft/server/World.java
|
2016-03-25 06:38:38 +00:00
|
|
|
@@ -156,6 +156,12 @@ public abstract class World implements IBlockAccess {
|
2016-03-22 05:46:32 +00:00
|
|
|
public Chunk getChunkIfLoaded(BlockPosition blockposition) {
|
|
|
|
return this.chunkProvider.getLoadedChunkAt(blockposition.getX() >> 4, blockposition.getZ() >> 4);
|
|
|
|
}
|
|
|
|
+
|
|
|
|
+ public void testResetChunkCache(Chunk chunk) {
|
|
|
|
+ if (chunk == ((ChunkProviderServer) chunkProvider).lastChunkByPos) {
|
|
|
|
+ ((ChunkProviderServer) chunkProvider).lastChunkByPos = null;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
// Paper end
|
|
|
|
|
|
|
|
public Chunk getChunkIfLoaded(int x, int z) {
|
|
|
|
diff --git a/src/main/java/net/minecraft/server/WorldServer.java b/src/main/java/net/minecraft/server/WorldServer.java
|
2016-03-31 00:50:23 +00:00
|
|
|
index 47dbea8..b859c51 100644
|
2016-03-22 05:46:32 +00:00
|
|
|
--- a/src/main/java/net/minecraft/server/WorldServer.java
|
|
|
|
+++ b/src/main/java/net/minecraft/server/WorldServer.java
|
2016-03-31 00:50:23 +00:00
|
|
|
@@ -203,6 +203,7 @@ public class WorldServer extends World implements IAsyncTaskHandler {
|
2016-03-22 05:46:32 +00:00
|
|
|
// CraftBukkit end
|
|
|
|
|
|
|
|
public void doTick() {
|
|
|
|
+ ((ChunkProviderServer) chunkProvider).lastChunkByPos = null; // Paper
|
|
|
|
super.doTick();
|
|
|
|
if (this.getWorldData().isHardcore() && this.getDifficulty() != EnumDifficulty.HARD) {
|
|
|
|
this.getWorldData().setDifficulty(EnumDifficulty.HARD);
|
|
|
|
diff --git a/src/main/java/org/bukkit/craftbukkit/CraftWorld.java b/src/main/java/org/bukkit/craftbukkit/CraftWorld.java
|
2016-03-29 01:01:42 +00:00
|
|
|
index c81dfee..ec62b1f 100644
|
2016-03-22 05:46:32 +00:00
|
|
|
--- a/src/main/java/org/bukkit/craftbukkit/CraftWorld.java
|
|
|
|
+++ b/src/main/java/org/bukkit/craftbukkit/CraftWorld.java
|
|
|
|
@@ -224,6 +224,7 @@ public class CraftWorld implements World {
|
|
|
|
|
|
|
|
world.getChunkProviderServer().unloadQueue.remove(x, z);
|
|
|
|
world.getChunkProviderServer().chunks.remove(LongHash.toLong(x, z));
|
|
|
|
+ world.testResetChunkCache(chunk); // Paper
|
|
|
|
|
|
|
|
// Update neighbor counts
|
|
|
|
for (int xx = -2; xx < 3; xx++) {
|
|
|
|
--
|
2016-03-31 00:50:23 +00:00
|
|
|
2.8.0
|
2016-03-22 05:46:32 +00:00
|
|
|
|