Paper/patches/server/0095-Optimize-UserCache-Thr...

112 lines
4.9 KiB
Diff
Raw Normal View History

2021-06-11 12:02:28 +00:00
From 0000000000000000000000000000000000000000 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.
2021-06-12 05:20:08 +00:00
1.17: TODO does this need the synchronized blocks anymore?
2021-06-11 12:02:28 +00:00
diff --git a/src/main/java/net/minecraft/server/MinecraftServer.java b/src/main/java/net/minecraft/server/MinecraftServer.java
2022-03-01 05:43:03 +00:00
index 0eaa13cf0f452fe6604767e40d8c9b6664df31bb..3e858ae243eaf9e145cbc5288ac2e3467082e369 100644
2021-06-11 12:02:28 +00:00
--- a/src/main/java/net/minecraft/server/MinecraftServer.java
+++ b/src/main/java/net/minecraft/server/MinecraftServer.java
2022-03-01 05:43:03 +00:00
@@ -991,7 +991,7 @@ public abstract class MinecraftServer extends ReentrantBlockableEventLoop<TickTa
2021-06-11 12:02:28 +00:00
} catch (java.lang.InterruptedException ignored) {} // Paper
if (org.spigotmc.SpigotConfig.saveUserCacheOnStopOnly) {
2021-06-12 05:20:08 +00:00
MinecraftServer.LOGGER.info("Saving usercache.json");
2021-06-11 12:02:28 +00:00
- this.getProfileCache().save();
2021-06-12 05:20:08 +00:00
+ this.getProfileCache().save(false); // Paper
2021-06-11 12:02:28 +00:00
}
// Spigot end
diff --git a/src/main/java/net/minecraft/server/dedicated/DedicatedServer.java b/src/main/java/net/minecraft/server/dedicated/DedicatedServer.java
2022-03-01 05:43:03 +00:00
index 20ff08f1a802d16d2306b570e9fb529946d90f6b..b99c1b62ce01700d5c374a1801e8323d5906255c 100644
2021-06-11 12:02:28 +00:00
--- a/src/main/java/net/minecraft/server/dedicated/DedicatedServer.java
+++ b/src/main/java/net/minecraft/server/dedicated/DedicatedServer.java
2022-03-01 05:43:03 +00:00
@@ -258,7 +258,7 @@ public class DedicatedServer extends MinecraftServer implements ServerInterface
2021-06-11 12:02:28 +00:00
}
if (this.convertOldUsers()) {
- this.getProfileCache().save();
2021-06-12 05:20:08 +00:00
+ this.getProfileCache().save(false); // Paper
2021-06-11 12:02:28 +00:00
}
if (!OldUsersConverter.serverReadyAfterUserconversion(this)) {
diff --git a/src/main/java/net/minecraft/server/players/GameProfileCache.java b/src/main/java/net/minecraft/server/players/GameProfileCache.java
2022-03-01 05:43:03 +00:00
index 975ab6061f9d70eb61885960fdfdb24e71b2239e..981845707f47a674a912bf84509397a42b973e74 100644
2021-06-11 12:02:28 +00:00
--- a/src/main/java/net/minecraft/server/players/GameProfileCache.java
+++ b/src/main/java/net/minecraft/server/players/GameProfileCache.java
2021-07-07 06:52:40 +00:00
@@ -118,7 +118,7 @@ public class GameProfileCache {
2021-06-11 12:02:28 +00:00
return GameProfileCache.usesAuthentication;
}
2021-06-12 05:20:08 +00:00
- public void add(GameProfile profile) {
+ public synchronized void add(GameProfile profile) { // Paper - synchronize
2021-06-11 12:02:28 +00:00
Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date());
2021-07-07 06:52:40 +00:00
@@ -127,14 +127,14 @@ public class GameProfileCache {
2021-06-12 05:20:08 +00:00
GameProfileCache.GameProfileInfo usercache_usercacheentry = new GameProfileCache.GameProfileInfo(profile, date);
2021-06-11 12:02:28 +00:00
this.safeAdd(usercache_usercacheentry);
- if( !org.spigotmc.SpigotConfig.saveUserCacheOnStopOnly ) this.save(); // Spigot - skip saving if disabled
2021-06-12 05:20:08 +00:00
+ if( !org.spigotmc.SpigotConfig.saveUserCacheOnStopOnly ) this.save(true); // Spigot - skip saving if disabled // Paper - async
2021-06-11 12:02:28 +00:00
}
private long getNextOperation() {
2021-07-07 06:52:40 +00:00
return this.operationCount.incrementAndGet();
2021-06-11 12:02:28 +00:00
}
2021-07-07 06:52:40 +00:00
- public Optional<GameProfile> get(String name) {
+ public synchronized Optional<GameProfile> get(String name) { // Paper - synchronize
String s1 = name.toLowerCase(Locale.ROOT);
2021-06-11 12:02:28 +00:00
GameProfileCache.GameProfileInfo usercache_usercacheentry = (GameProfileCache.GameProfileInfo) this.profilesByName.get(s1);
boolean flag = false;
2021-06-12 05:20:08 +00:00
@@ -160,7 +160,7 @@ public class GameProfileCache {
2021-06-11 12:02:28 +00:00
}
if (flag && !org.spigotmc.SpigotConfig.saveUserCacheOnStopOnly) { // Spigot - skip saving if disabled
- this.save();
2021-06-12 05:20:08 +00:00
+ this.save(true); // Paper
2021-06-11 12:02:28 +00:00
}
2021-07-07 06:52:40 +00:00
return optional;
2021-11-23 13:22:49 +00:00
@@ -274,7 +274,7 @@ public class GameProfileCache {
2021-06-11 12:02:28 +00:00
return arraylist;
}
- public void save() {
2021-06-12 05:20:08 +00:00
+ public void save(boolean asyncSave) { // Paper
2021-06-11 12:02:28 +00:00
JsonArray jsonarray = new JsonArray();
2021-06-12 05:20:08 +00:00
DateFormat dateformat = GameProfileCache.createDateFormat();
2021-06-11 12:02:28 +00:00
2021-11-23 13:22:49 +00:00
@@ -282,6 +282,7 @@ public class GameProfileCache {
2021-06-12 05:20:08 +00:00
jsonarray.add(GameProfileCache.writeGameProfile(usercache_usercacheentry, dateformat));
2021-06-11 12:02:28 +00:00
});
String s = this.gson.toJson(jsonarray);
+ Runnable save = () -> { // Paper
try {
BufferedWriter bufferedwriter = Files.newWriter(this.file, StandardCharsets.UTF_8);
2021-11-23 13:22:49 +00:00
@@ -306,7 +307,14 @@ public class GameProfileCache {
2021-06-11 12:02:28 +00:00
} catch (IOException ioexception) {
;
}
2021-07-07 06:52:40 +00:00
-
2021-06-11 12:02:28 +00:00
+ // Paper start
+ };
+ if (asyncSave) {
2021-06-12 05:20:08 +00:00
+ net.minecraft.server.MCUtil.scheduleAsyncTask(save);
2021-06-11 12:02:28 +00:00
+ } else {
+ save.run();
+ }
+ // Paper end
}
2021-07-07 06:52:40 +00:00
private Stream<GameProfileCache.GameProfileInfo> getTopMRUProfiles(int limit) {