Paper/Spigot-Server-Patches/0105-Optimize-UserCache-Thread-Safe.patch
Spottedleaf 5c7081fecc Update upstream & fix some chunk related issues (#2177)
* Updated Upstream (Bukkit/CraftBukkit)

Upstream has released updates that appears to apply and compile correctly.
This update has not been tested by PaperMC and as with ANY update, please do your own testing

Bukkit Changes:
45690fe9 SPIGOT-5047: Correct slot types for 1.14 inventories

CraftBukkit Changes:
4090d01f SPIGOT-5047: Correct slot types for 1.14 inventories
e8c08362 SPIGOT-5046: World#getLoadedChunks returning inaccessible cached chunks.
d445af3b SPIGOT-5067: Add item meta for 1.14 spawn eggs

* Bring Chunk load checks in-line with spigot

As of the last upstream merge spigot now checks ticket level status
when returning loaded chunks for a world from api. Now our checks
will respect that decision.

* Fix spawn ticket levels

Vanilla would keep the inner chunks of spawn available for ticking,
however my changes made all chunks non-ticking. Resolve by changing
ticket levels for spawn chunks inside the border to respect this
behavior.


* Make World#getChunkIfLoadedImmediately return only entity ticking chunks

Mojang appears to be using chunks with level > 33 (non-ticking chunks)
as cached chunks and not actually loaded chunks.

* Bring all loaded checks in line with spigot

Loaded chunks must be at least border  chunks, or level <= 33
2019-06-14 03:27:40 +01:00

111 lines
4.4 KiB
Diff

From 06b292dd14d20731596c660752b2ac0d64971546 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.
diff --git a/src/main/java/net/minecraft/server/MinecraftServer.java b/src/main/java/net/minecraft/server/MinecraftServer.java
index d3d8fffeb0..f142ed9a3b 100644
--- a/src/main/java/net/minecraft/server/MinecraftServer.java
+++ b/src/main/java/net/minecraft/server/MinecraftServer.java
@@ -739,7 +739,7 @@ public abstract class MinecraftServer extends IAsyncTaskHandlerReentrant<TickTas
// Spigot start
if (org.spigotmc.SpigotConfig.saveUserCacheOnStopOnly) {
LOGGER.info("Saving usercache.json");
- this.getUserCache().c();
+ this.getUserCache().c(false); // Paper
}
// Spigot end
}
diff --git a/src/main/java/net/minecraft/server/UserCache.java b/src/main/java/net/minecraft/server/UserCache.java
index 57d053e9ff..ea02567554 100644
--- a/src/main/java/net/minecraft/server/UserCache.java
+++ b/src/main/java/net/minecraft/server/UserCache.java
@@ -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;
@@ -108,7 +108,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) {
@@ -121,8 +121,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);
@@ -135,7 +136,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);
@@ -164,6 +165,7 @@ public class UserCache {
return usercache_usercacheentry == null ? null : usercache_usercacheentry.a();
}
+ @Nullable public GameProfile getProfile(UUID uuid) { return a(uuid); } // Paper - OBFHELPER
@Nullable
public GameProfile a(UUID uuid) {
UserCache.UserCacheEntry usercache_usercacheentry = (UserCache.UserCacheEntry) this.e.get(uuid);
@@ -220,8 +222,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 {
@@ -235,6 +244,14 @@ public class UserCache {
} finally {
IOUtils.closeQuietly(bufferedwriter);
}
+ // Paper start
+ };
+ if (asyncSave) {
+ MCUtil.scheduleAsyncTask(save);
+ } else {
+ save.run();
+ }
+ // Paper end
}
--
2.21.0