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. diff --git a/src/main/java/net/minecraft/server/MinecraftServer.java b/src/main/java/net/minecraft/server/MinecraftServer.java index d6bbb462931fd1eb97d39fbbbf4b843aa0f17c2e..28ade7187d6fd61adbfe65a1944426168b131c4c 100644 --- a/src/main/java/net/minecraft/server/MinecraftServer.java +++ b/src/main/java/net/minecraft/server/MinecraftServer.java @@ -788,7 +788,7 @@ public abstract class MinecraftServer extends IAsyncTaskHandlerReentrant d = Maps.newHashMap(); - private final Map e = Maps.newHashMap(); + private final Map d = new java.util.concurrent.ConcurrentHashMap<>(); // Paper + private final Map e = new java.util.concurrent.ConcurrentHashMap<>(); // Paper private final Deque f = new java.util.concurrent.LinkedBlockingDeque(); // CraftBukkit private final GameProfileRepository g; protected final Gson b; @@ -97,7 +97,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) { @@ -110,8 +110,9 @@ public class UserCache { UserCache.UserCacheEntry usercache_usercacheentry = new UserCache.UserCacheEntry(gameprofile, date); - 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); @@ -124,7 +125,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); @@ -209,8 +210,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 { @@ -224,6 +232,14 @@ public class UserCache { } finally { IOUtils.closeQuietly(bufferedwriter); } + // Paper start + }; + if (asyncSave) { + MCUtil.scheduleAsyncTask(save); + } else { + save.run(); + } + // Paper end }