aboutsummaryrefslogtreecommitdiffhomepage
path: root/Spigot-Server-Patches/0148-Optimize-UserCache-Thread-Safe.patch
blob: eb3602f5d6b8ad2895f4690bbbb07ad23d9e4290 (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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
From f69529e8ae915db7197f5cbc39f551cbbfe4c936 Mon Sep 17 00:00:00 2001
From: Aikar <aikar@aikar.co>
Date: Mon, 16 May 2016 20:47:41 -0400
Subject: [PATCH] Optimize UserCache / Thread Safe

Because Techable keeps complaining about how this isn't thread safe,
easier to do this than replace the entire thing.

Additionally, move Saving of the User cache to be done async, incase
the user never changed the default setting for Spigot's save on stop only.

diff --git a/src/main/java/net/minecraft/server/MinecraftServer.java b/src/main/java/net/minecraft/server/MinecraftServer.java
index 25c4f9f..005a27b 100644
--- a/src/main/java/net/minecraft/server/MinecraftServer.java
+++ b/src/main/java/net/minecraft/server/MinecraftServer.java
@@ -507,7 +507,7 @@ public abstract class MinecraftServer implements Runnable, ICommandListener, IAs
         // Spigot start
         if (org.spigotmc.SpigotConfig.saveUserCacheOnStopOnly) {
             LOGGER.info("Saving usercache.json");
-            this.X.c();
+            this.X.c(false); // Paper
         }
         // Spigot end
     }
diff --git a/src/main/java/net/minecraft/server/UserCache.java b/src/main/java/net/minecraft/server/UserCache.java
index ccfc755..989758c 100644
--- a/src/main/java/net/minecraft/server/UserCache.java
+++ b/src/main/java/net/minecraft/server/UserCache.java
@@ -108,7 +108,7 @@ public class UserCache {
         this.a(gameprofile, (Date) null);
     }
 
-    private void a(GameProfile gameprofile, Date date) {
+    private synchronized void a(GameProfile gameprofile, Date date) { // Paper - synchronize
         UUID uuid = gameprofile.getId();
 
         if (date == null) {
@@ -122,8 +122,9 @@ public class UserCache {
         String s = gameprofile.getName().toLowerCase(Locale.ROOT);
         UserCache.UserCacheEntry usercache_usercacheentry = new UserCache.UserCacheEntry(gameprofile, date, null);
 
-        if (this.e.containsKey(uuid)) {
+        //if (this.e.containsKey(uuid)) { // Paper
             UserCache.UserCacheEntry usercache_usercacheentry1 = (UserCache.UserCacheEntry) this.e.get(uuid);
+        if (usercache_usercacheentry1 != null) { // Paper
 
             this.d.remove(usercache_usercacheentry1.a().getName().toLowerCase(Locale.ROOT));
             this.f.remove(gameprofile);
@@ -136,7 +137,7 @@ public class UserCache {
     }
 
     @Nullable
-    public GameProfile getProfile(String s) {
+    public synchronized GameProfile getProfile(String s) { // Paper - synchronize
         String s1 = s.toLowerCase(Locale.ROOT);
         UserCache.UserCacheEntry usercache_usercacheentry = (UserCache.UserCacheEntry) this.d.get(s1);
 
@@ -165,7 +166,7 @@ public class UserCache {
         return usercache_usercacheentry == null ? null : usercache_usercacheentry.a();
     }
 
-    public String[] a() {
+    public synchronized String[] a() { // Paper - synchronize
         ArrayList arraylist = Lists.newArrayList(this.d.keySet());
 
         return (String[]) arraylist.toArray(new String[arraylist.size()]);
@@ -227,8 +228,15 @@ public class UserCache {
 
     }
 
+    // Paper start
     public void c() {
+        c(true);
+    }
+    public void c(boolean asyncSave) {
+        // Paper end
         String s = this.b.toJson(this.a(org.spigotmc.SpigotConfig.userCacheCap));
+        Runnable save = () -> {
+
         BufferedWriter bufferedwriter = null;
 
         try {
@@ -242,6 +250,14 @@ public class UserCache {
         } finally {
             IOUtils.closeQuietly(bufferedwriter);
         }
+        // Paper start
+        };
+        if (asyncSave) {
+            MCUtil.scheduleAsyncTask(save);
+        } else {
+            save.run();
+        }
+        // Paper end
 
     }
 
-- 
2.9.3