2020-05-06 09:48:49 +00:00
|
|
|
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
|
2016-05-17 01:14:28 +00:00
|
|
|
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
|
2020-06-25 14:09:55 +00:00
|
|
|
index dcc138b48e6208f774586e09eb43c3446494542f..56c9e7e2fd3b454f20fe6f440915bbb770d9a0c9 100644
|
2016-05-17 01:14:28 +00:00
|
|
|
--- a/src/main/java/net/minecraft/server/MinecraftServer.java
|
|
|
|
+++ b/src/main/java/net/minecraft/server/MinecraftServer.java
|
2020-06-25 14:09:55 +00:00
|
|
|
@@ -789,7 +789,7 @@ public abstract class MinecraftServer extends IAsyncTaskHandlerReentrant<TickTas
|
2020-05-17 01:38:19 +00:00
|
|
|
} catch (java.lang.InterruptedException ignored) {} // Paper
|
2016-05-17 01:14:28 +00:00
|
|
|
if (org.spigotmc.SpigotConfig.saveUserCacheOnStopOnly) {
|
|
|
|
LOGGER.info("Saving usercache.json");
|
2018-07-16 15:34:55 +00:00
|
|
|
- this.getUserCache().c();
|
|
|
|
+ this.getUserCache().c(false); // Paper
|
2016-05-17 01:14:28 +00:00
|
|
|
}
|
|
|
|
// Spigot end
|
2020-06-25 11:00:35 +00:00
|
|
|
|
2016-05-17 01:14:28 +00:00
|
|
|
diff --git a/src/main/java/net/minecraft/server/UserCache.java b/src/main/java/net/minecraft/server/UserCache.java
|
2020-06-25 11:00:35 +00:00
|
|
|
index b211609995b95c26317995f4d776d21ab58d6c0e..4d3cbe3e517a75269d5c9cdd7f3751b81daee1c2 100644
|
2016-05-17 01:14:28 +00:00
|
|
|
--- a/src/main/java/net/minecraft/server/UserCache.java
|
|
|
|
+++ b/src/main/java/net/minecraft/server/UserCache.java
|
2019-05-27 06:17:50 +00:00
|
|
|
@@ -43,8 +43,8 @@ public class UserCache {
|
|
|
|
|
|
|
|
public static final SimpleDateFormat a = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss Z");
|
|
|
|
private static boolean c;
|
|
|
|
- private final Map<String, UserCache.UserCacheEntry> d = Maps.newHashMap();
|
|
|
|
- private final Map<UUID, UserCache.UserCacheEntry> e = Maps.newHashMap();
|
|
|
|
+ private final Map<String, UserCache.UserCacheEntry> d = new java.util.concurrent.ConcurrentHashMap<>(); // Paper
|
|
|
|
+ private final Map<UUID, UserCache.UserCacheEntry> e = new java.util.concurrent.ConcurrentHashMap<>(); // Paper
|
|
|
|
private final Deque<GameProfile> f = new java.util.concurrent.LinkedBlockingDeque<GameProfile>(); // CraftBukkit
|
|
|
|
private final GameProfileRepository g;
|
|
|
|
protected final Gson b;
|
2020-06-25 11:00:35 +00:00
|
|
|
@@ -97,7 +97,7 @@ public class UserCache {
|
2016-05-23 02:14:17 +00:00
|
|
|
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) {
|
2020-06-25 11:00:35 +00:00
|
|
|
@@ -110,8 +110,9 @@ public class UserCache {
|
2018-07-16 15:34:55 +00:00
|
|
|
|
2019-01-01 03:15:55 +00:00
|
|
|
UserCache.UserCacheEntry usercache_usercacheentry = new UserCache.UserCacheEntry(gameprofile, date);
|
2016-05-17 01:14:28 +00:00
|
|
|
|
|
|
|
- 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);
|
2020-06-25 11:00:35 +00:00
|
|
|
@@ -124,7 +125,7 @@ public class UserCache {
|
2016-05-23 02:14:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@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);
|
|
|
|
|
2020-06-25 11:00:35 +00:00
|
|
|
@@ -209,8 +210,15 @@ public class UserCache {
|
2016-05-17 01:14:28 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
+ // 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));
|
2016-05-23 02:20:15 +00:00
|
|
|
+ Runnable save = () -> {
|
2016-05-17 01:14:28 +00:00
|
|
|
+
|
|
|
|
BufferedWriter bufferedwriter = null;
|
|
|
|
|
|
|
|
try {
|
2020-06-25 11:00:35 +00:00
|
|
|
@@ -224,6 +232,14 @@ public class UserCache {
|
2016-05-17 01:14:28 +00:00
|
|
|
} finally {
|
|
|
|
IOUtils.closeQuietly(bufferedwriter);
|
|
|
|
}
|
|
|
|
+ // Paper start
|
2016-05-23 02:20:15 +00:00
|
|
|
+ };
|
2016-05-17 01:14:28 +00:00
|
|
|
+ if (asyncSave) {
|
|
|
|
+ MCUtil.scheduleAsyncTask(save);
|
|
|
|
+ } else {
|
|
|
|
+ save.run();
|
|
|
|
+ }
|
|
|
|
+ // Paper end
|
|
|
|
|
|
|
|
}
|
|
|
|
|