aboutsummaryrefslogtreecommitdiffhomepage
path: root/Spigot-Server-Patches/0099-Sanitise-RegionFileCache-and-make-configurable.patch
blob: 05510d35ebc7cd1cf38fc53d38a7933aa30c136d (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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
From e9bec1384df97f7d694ebfa1dad07e0ae0e3faab Mon Sep 17 00:00:00 2001
From: Antony Riley <antony@cyberiantiger.org>
Date: Tue, 29 Mar 2016 08:22:55 +0300
Subject: [PATCH] Sanitise RegionFileCache and make configurable.

RegionFileCache prior to this patch would close every single open region
file upon reaching a size of 256.
This patch modifies that behaviour so it closes the the least recently
used RegionFile.
The implementation uses a LinkedHashMap as an LRU cache (modified from HashMap).
The maximum size of the RegionFileCache is also made configurable.

diff --git a/src/main/java/com/destroystokyo/paper/PaperConfig.java b/src/main/java/com/destroystokyo/paper/PaperConfig.java
index 6907d4680..c3ff47e00 100644
--- a/src/main/java/com/destroystokyo/paper/PaperConfig.java
+++ b/src/main/java/com/destroystokyo/paper/PaperConfig.java
@@ -220,4 +220,9 @@ public class PaperConfig {
     private static void healthDeath() {
         setHealthBeforeDeathEvent = getBoolean("settings.set-health-before-death-event", false);
     }
+
+    public static int regionFileCacheSize = 256;
+    private static void regionFileCacheSize() {
+        regionFileCacheSize = getInt("settings.region-file-cache-size", 256);
+    }
 }
diff --git a/src/main/java/net/minecraft/server/RegionFileCache.java b/src/main/java/net/minecraft/server/RegionFileCache.java
index 02b9bc8df..91b55074e 100644
--- a/src/main/java/net/minecraft/server/RegionFileCache.java
+++ b/src/main/java/net/minecraft/server/RegionFileCache.java
@@ -8,10 +8,12 @@ import java.io.File;
 import java.io.IOException;
 import java.util.Iterator;
 import java.util.Map;
+import com.destroystokyo.paper.PaperConfig; // Paper
+import java.util.LinkedHashMap; // Paper
 
 public class RegionFileCache {
 
-    public static final Map<File, RegionFile> a = Maps.newHashMap(); // Spigot - private -> public
+    public static final Map<File, RegionFile> a = new LinkedHashMap(PaperConfig.regionFileCacheSize, 0.75f, true); // Spigot - private -> public, Paper - HashMap -> LinkedHashMap
 
     public static synchronized RegionFile a(File file, int i, int j) {
         File file1 = new File(file, "region");
@@ -25,8 +27,8 @@ public class RegionFileCache {
                 file1.mkdirs();
             }
 
-            if (RegionFileCache.a.size() >= 256) {
-                a();
+            if (RegionFileCache.a.size() >= PaperConfig.regionFileCacheSize) { // Paper
+                trimCache(); // Paper
             }
 
             RegionFile regionfile1 = new RegionFile(file2);
@@ -57,6 +59,22 @@ public class RegionFileCache {
         }
     }
 
+    // Paper Start
+    private static synchronized void trimCache() {
+        Iterator<Map.Entry<File, RegionFile>> itr = RegionFileCache.a.entrySet().iterator();
+        int count = RegionFileCache.a.size() - PaperConfig.regionFileCacheSize;
+        while (count-- >= 0 && itr.hasNext()) {
+            try {
+                itr.next().getValue().c();
+            } catch (IOException ioexception) {
+                ioexception.printStackTrace();
+                ServerInternalException.reportInternalException(ioexception);
+            }
+            itr.remove();
+        }
+    }
+    // Paper End
+
     public static synchronized void a() {
         Iterator iterator = RegionFileCache.a.values().iterator();
 
-- 
2.12.2.windows.2