From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: Spottedleaf Date: Sat, 11 Jul 2020 05:09:28 -0700 Subject: [PATCH] Separate lookup locking from state access in UserCache Prevent lookups from stalling simple state access/write calls diff --git a/src/main/java/net/minecraft/server/players/GameProfileCache.java b/src/main/java/net/minecraft/server/players/GameProfileCache.java index d7eba4190110b92641664c827c6bc50f62d2ae15..534373a1e305942d63ffb0fc97ea3f165eca76b5 100644 --- a/src/main/java/net/minecraft/server/players/GameProfileCache.java +++ b/src/main/java/net/minecraft/server/players/GameProfileCache.java @@ -62,6 +62,11 @@ public class GameProfileCache { @Nullable private Executor executor; + // Paper start + protected final java.util.concurrent.locks.ReentrantLock stateLock = new java.util.concurrent.locks.ReentrantLock(); + protected final java.util.concurrent.locks.ReentrantLock lookupLock = new java.util.concurrent.locks.ReentrantLock(); + // Paper end + public GameProfileCache(GameProfileRepository profileRepository, File cacheFile) { this.profileRepository = profileRepository; this.file = cacheFile; @@ -69,6 +74,7 @@ public class GameProfileCache { } private void safeAdd(GameProfileCache.GameProfileInfo entry) { + try { this.stateLock.lock(); // Paper - allow better concurrency GameProfile gameprofile = entry.getProfile(); entry.setLastAccess(this.getNextOperation()); @@ -83,6 +89,7 @@ public class GameProfileCache { if (uuid != null) { this.profilesByUUID.put(uuid, entry); } + } finally { this.stateLock.unlock(); } // Paper - allow better concurrency } @@ -119,7 +126,7 @@ public class GameProfileCache { return com.destroystokyo.paper.PaperConfig.isProxyOnlineMode(); // Paper } - public synchronized void add(GameProfile profile) { // Paper - synchronize + public void add(GameProfile profile) { // Paper - synchronize // Paper - allow better concurrency Calendar calendar = Calendar.getInstance(); calendar.setTime(new Date()); @@ -137,17 +144,20 @@ public class GameProfileCache { // Paper start @Nullable public GameProfile getProfileIfCached(String name) { + try { this.stateLock.lock(); // Paper - allow better concurrency GameProfileCache.GameProfileInfo entry = this.profilesByName.get(name.toLowerCase(Locale.ROOT)); if (entry == null) { return null; } entry.setLastAccess(this.getNextOperation()); return entry.getProfile(); + } finally { this.stateLock.unlock(); } // Paper - allow better concurrency } // Paper end - public synchronized Optional get(String name) { // Paper - synchronize + public Optional get(String name) { // Paper - synchronize // Paper start - allow better concurrency String s1 = name.toLowerCase(Locale.ROOT); + boolean stateLocked = true; try { this.stateLock.lock(); // Paper - allow better concurrency GameProfileCache.GameProfileInfo usercache_usercacheentry = (GameProfileCache.GameProfileInfo) this.profilesByName.get(s1); boolean flag = false; @@ -163,8 +173,12 @@ public class GameProfileCache { if (usercache_usercacheentry != null) { usercache_usercacheentry.setLastAccess(this.getNextOperation()); optional = Optional.of(usercache_usercacheentry.getProfile()); + stateLocked = false; this.stateLock.unlock(); // Paper - allow better concurrency } else { + stateLocked = false; this.stateLock.unlock(); // Paper - allow better concurrency + try { this.lookupLock.lock(); // Paper - allow better concurrency optional = GameProfileCache.lookupGameProfile(this.profileRepository, name); // Spigot - use correct case for offline players + } finally { this.lookupLock.unlock(); } // Paper - allow better concurrency if (optional.isPresent()) { this.add((GameProfile) optional.get()); flag = false; @@ -176,6 +190,7 @@ public class GameProfileCache { } return optional; + } finally { if (stateLocked) { this.stateLock.unlock(); } } // Paper - allow better concurrency } public void getAsync(String username, Consumer> consumer) { @@ -202,6 +217,7 @@ public class GameProfileCache { } public Optional get(UUID uuid) { + try { this.stateLock.lock(); // Paper - allow better concurrency GameProfileCache.GameProfileInfo usercache_usercacheentry = (GameProfileCache.GameProfileInfo) this.profilesByUUID.get(uuid); if (usercache_usercacheentry == null) { @@ -210,6 +226,7 @@ public class GameProfileCache { usercache_usercacheentry.setLastAccess(this.getNextOperation()); return Optional.of(usercache_usercacheentry.getProfile()); } + } finally { this.stateLock.unlock(); } // Paper - allow better concurrency } public void setExecutor(Executor executor) { @@ -290,7 +307,7 @@ public class GameProfileCache { JsonArray jsonarray = new JsonArray(); DateFormat dateformat = GameProfileCache.createDateFormat(); - this.getTopMRUProfiles(org.spigotmc.SpigotConfig.userCacheCap).forEach((usercache_usercacheentry) -> { // Spigot + this.listTopMRUProfiles(org.spigotmc.SpigotConfig.userCacheCap).forEach((usercache_usercacheentry) -> { // Spigot // Paper - allow better concurrency jsonarray.add(GameProfileCache.writeGameProfile(usercache_usercacheentry, dateformat)); }); String s = this.gson.toJson(jsonarray); @@ -330,8 +347,19 @@ public class GameProfileCache { } private Stream getTopMRUProfiles(int limit) { - return ImmutableList.copyOf(this.profilesByUUID.values()).stream().sorted(Comparator.comparing(GameProfileCache.GameProfileInfo::getLastAccess).reversed()).limit((long) limit); + // Paper start - allow better concurrency + return this.listTopMRUProfiles(limit).stream(); + } + + private List listTopMRUProfiles(int limit) { + try { + this.stateLock.lock(); + return this.profilesByUUID.values().stream().sorted(Comparator.comparing(GameProfileCache.GameProfileInfo::getLastAccess).reversed()).limit(limit).toList(); + } finally { + this.stateLock.unlock(); + } } + // Paper end private static JsonElement writeGameProfile(GameProfileCache.GameProfileInfo entry, DateFormat dateFormat) { JsonObject jsonobject = new JsonObject();