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

112 lines
4.9 KiB
Diff

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.
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 375045935b7b9a3a8fd8f8f897763caf75e59813..370e9a840a9c8abf8804755c0395d51fd576ee49 100644
--- a/src/main/java/net/minecraft/server/MinecraftServer.java
+++ b/src/main/java/net/minecraft/server/MinecraftServer.java
@@ -973,7 +973,7 @@ public abstract class MinecraftServer extends ReentrantBlockableEventLoop<TickTa
} catch (java.lang.InterruptedException ignored) {} // Paper
if (org.spigotmc.SpigotConfig.saveUserCacheOnStopOnly) {
MinecraftServer.LOGGER.info("Saving usercache.json");
- this.getProfileCache().save();
+ this.getProfileCache().save(false); // Paper
}
// Spigot end
diff --git a/src/main/java/net/minecraft/server/dedicated/DedicatedServer.java b/src/main/java/net/minecraft/server/dedicated/DedicatedServer.java
index 66f74553fdddf1a6304919e4a9cde1dc2b935cf5..2201aeecd9936402825200dd696dc5607fe0f880 100644
--- a/src/main/java/net/minecraft/server/dedicated/DedicatedServer.java
+++ b/src/main/java/net/minecraft/server/dedicated/DedicatedServer.java
@@ -255,7 +255,7 @@ public class DedicatedServer extends MinecraftServer implements ServerInterface
}
if (this.convertOldUsers()) {
- this.getProfileCache().save();
+ this.getProfileCache().save(false); // Paper
}
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
index 2ead61f86571b9ae67300a6c286d363dd7d64a8a..e8515f4ed1f29ba926bc4ab6d918722aee450ac2 100644
--- a/src/main/java/net/minecraft/server/players/GameProfileCache.java
+++ b/src/main/java/net/minecraft/server/players/GameProfileCache.java
@@ -118,7 +118,7 @@ public class GameProfileCache {
return GameProfileCache.usesAuthentication;
}
- public void add(GameProfile profile) {
+ public synchronized void add(GameProfile profile) { // Paper - synchronize
Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date());
@@ -127,14 +127,14 @@ public class GameProfileCache {
GameProfileCache.GameProfileInfo usercache_usercacheentry = new GameProfileCache.GameProfileInfo(profile, date);
this.safeAdd(usercache_usercacheentry);
- if( !org.spigotmc.SpigotConfig.saveUserCacheOnStopOnly ) this.save(); // Spigot - skip saving if disabled
+ if( !org.spigotmc.SpigotConfig.saveUserCacheOnStopOnly ) this.save(true); // Spigot - skip saving if disabled // Paper - async
}
private long getNextOperation() {
return this.operationCount.incrementAndGet();
}
- public Optional<GameProfile> get(String name) {
+ public synchronized Optional<GameProfile> 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;
@@ -274,7 +274,7 @@ public class GameProfileCache {
return arraylist;
}
- public void save() {
+ public void save(boolean asyncSave) { // Paper
JsonArray jsonarray = new JsonArray();
DateFormat dateformat = GameProfileCache.createDateFormat();
@@ -282,6 +282,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);
@@ -306,7 +307,14 @@ public class GameProfileCache {
} catch (IOException ioexception) {
;
}
-
+ // Paper start
+ };
+ if (asyncSave) {
+ net.minecraft.server.MCUtil.scheduleAsyncTask(save);
+ } else {
+ save.run();
+ }
+ // Paper end
}
private Stream<GameProfileCache.GameProfileInfo> getTopMRUProfiles(int limit) {