From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: Aikar 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. 1.17: TODO does this need the synchronized blocks anymore? diff --git a/src/main/java/net/minecraft/server/MinecraftServer.java b/src/main/java/net/minecraft/server/MinecraftServer.java index 4d5f3509d7be914658014d07c72ec12762a76da3..feab424cbfcb4c8f7ce38cdeeb42dce3fe1ce7aa 100644 --- a/src/main/java/net/minecraft/server/MinecraftServer.java +++ b/src/main/java/net/minecraft/server/MinecraftServer.java @@ -972,7 +972,7 @@ public abstract class MinecraftServer extends ReentrantBlockableEventLoop get(String name) { + public synchronized Optional get(String name) { // Paper - synchronize String s1 = name.toLowerCase(Locale.ROOT); GameProfileCache.GameProfileInfo usercache_usercacheentry = (GameProfileCache.GameProfileInfo) this.profilesByName.get(s1); boolean flag = false; @@ -160,7 +160,7 @@ public class GameProfileCache { } if (flag && !org.spigotmc.SpigotConfig.saveUserCacheOnStopOnly) { // Spigot - skip saving if disabled - this.save(); + this.save(true); // Paper } return optional; @@ -270,7 +270,7 @@ public class GameProfileCache { return arraylist; } - public void save() { + public void save(boolean asyncSave) { // Paper JsonArray jsonarray = new JsonArray(); DateFormat dateformat = GameProfileCache.createDateFormat(); @@ -278,6 +278,7 @@ public class GameProfileCache { jsonarray.add(GameProfileCache.writeGameProfile(usercache_usercacheentry, dateformat)); }); String s = this.gson.toJson(jsonarray); + Runnable save = () -> { // Paper try { BufferedWriter bufferedwriter = Files.newWriter(this.file, StandardCharsets.UTF_8); @@ -302,7 +303,14 @@ public class GameProfileCache { } catch (IOException ioexception) { ; } - + // Paper start + }; + if (asyncSave) { + net.minecraft.server.MCUtil.scheduleAsyncTask(save); + } else { + save.run(); + } + // Paper end } private Stream getTopMRUProfiles(int limit) {