aboutsummaryrefslogtreecommitdiffhomepage
path: root/Spigot-Server-Patches/0086-Optimize-Chunk-Access.patch
blob: 75dc5c1e9803fb07f0044dac8bac25df4dcce5a3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
From 8938542fec81aff213567b6bb3c3ffe88958597c Mon Sep 17 00:00:00 2001
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.
getChunkAt is called for the same chunk multiple times in a row, often from getType();

Optimize this look up by using a Last Access cache.

diff --git a/src/main/java/net/minecraft/server/ChunkProviderServer.java b/src/main/java/net/minecraft/server/ChunkProviderServer.java
index 0ad57afba..b49f4e292 100644
--- a/src/main/java/net/minecraft/server/ChunkProviderServer.java
+++ b/src/main/java/net/minecraft/server/ChunkProviderServer.java
@@ -27,7 +27,27 @@ public class ChunkProviderServer implements IChunkProvider {
     public final Set<Long> unloadQueue = Sets.newHashSet();
     public final ChunkGenerator chunkGenerator;
     private final IChunkLoader chunkLoader;
-    public final Long2ObjectMap<Chunk> chunks = new Long2ObjectOpenHashMap(8192);
+    // Paper start
+    protected Chunk lastChunkByPos = null;
+    public Long2ObjectOpenHashMap<Chunk> chunks = new Long2ObjectOpenHashMap<Chunk>(8192) {
+
+        @Override
+        public Chunk get(long key) {
+            if (lastChunkByPos != null && key == lastChunkByPos.chunkKey) {
+                return lastChunkByPos;
+            }
+            return lastChunkByPos = super.get(key);
+        }
+
+        @Override
+        public Chunk remove(long key) {
+            if (lastChunkByPos != null && key == lastChunkByPos.chunkKey) {
+                lastChunkByPos = null;
+            }
+            return super.remove(key);
+        }
+    }; // CraftBukkit
+    // Paper end
     public final WorldServer world;
 
     public ChunkProviderServer(WorldServer worldserver, IChunkLoader ichunkloader, ChunkGenerator chunkgenerator) {
-- 
2.12.2