Paper/patches/server/0240-Use-ConcurrentHashMap-in-JsonList.patch

141 lines
6.9 KiB
Diff
Raw Normal View History

2021-06-11 14:02:28 +02:00
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: egg82 <phantom_zero@ymail.com>
Date: Tue, 7 Aug 2018 01:24:23 -0600
Subject: [PATCH] Use ConcurrentHashMap in JsonList
This is specifically aimed at fixing #471
Using a ConcurrentHashMap because thread safety
The performance benefit of Map over ConcurrentMap is negligabe at best in this scenaio, as most operations will be get and not add or remove
Even without considering the use-case the benefits are still negligable
Original ideas for the system included an expiration policy and/or handler
The simpler solution was to use a computeIfPresent in the get method
This will simultaneously have an O(1) lookup time and automatically expire any values
Since the get method (nor other similar methods) don't seem to have a critical need to flush the map to disk at any of these points further processing is simply wasteful
Meaning the original function expired values unrelated to the current value without actually having any explicit need to
The h method was heavily modified to be much more efficient in its processing
Also instead of being called on every get, it's now called just before a save
This will eliminate stale values being flushed to disk
Modified isEmpty to use the isEmpty() method instead of the slightly confusing size() < 1
The point of this is readability, but does have a side-benefit of a small microptimization
diff --git a/src/main/java/net/minecraft/server/players/PlayerList.java b/src/main/java/net/minecraft/server/players/PlayerList.java
Rewrite chunk system (#8177) Patch documentation to come Issues with the old system that are fixed now: - World generation does not scale with cpu cores effectively. - Relies on the main thread for scheduling and maintaining chunk state, dropping chunk load/generate rates at lower tps. - Unreliable prioritisation of chunk gen/load calls that block the main thread. - Shutdown logic is utterly unreliable, as it has to wait for all chunks to unload - is it guaranteed that the chunk system is in a state on shutdown that it can reliably do this? Watchdog shutdown also typically failed due to thread checks, which is now resolved. - Saving of data is not unified (i.e can save chunk data without saving entity data, poses problems for desync if shutdown is really abnormal. - Entities are not loaded with chunks. This caused quite a bit of headache for Chunk#getEntities API, but now the new chunk system loads entities with chunks so that they are ready whenever the chunk loads in. Effectively brings the behavior back to 1.16 era, but still storing entities in their own separate regionfiles. The above list is not complete. The patch documentation will complete it. New chunk system hard relies on starlight and dataconverter, and most importantly the new concurrent utilities in ConcurrentUtil. Some of the old async chunk i/o interface (i.e the old file io thread reroutes _some_ calls to the new file io thread) is kept for plugin compat reasons. It will be removed in the next major version of minecraft. The old legacy chunk system patches have been moved to the removed folder in case we need them again.
2022-09-26 10:02:51 +02:00
index a99d6938d912a169ac329ba09f5a6becd072a94e..ffa3f9d147a0113d7b8cbf185ca751ed159576c2 100644
2021-06-11 14:02:28 +02:00
--- a/src/main/java/net/minecraft/server/players/PlayerList.java
+++ b/src/main/java/net/minecraft/server/players/PlayerList.java
@@ -624,7 +624,7 @@ public abstract class PlayerList {
2022-06-07 23:45:11 +02:00
} else if (!this.isWhiteListed(gameprofile, event)) { // Paper
//ichatmutablecomponent = Component.translatable("multiplayer.disconnect.not_whitelisted"); // Paper
//event.disallow(PlayerLoginEvent.Result.KICK_WHITELIST, net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer.legacySection().deserialize(org.spigotmc.SpigotConfig.whitelistMessage)); // Spigot // Paper - Adventure - moved to isWhitelisted
2021-06-13 01:45:00 +02:00
- } else if (this.getIpBans().isBanned(socketaddress) && !this.getIpBans().get(socketaddress).hasExpired()) {
+ } else if (this.getIpBans().isBanned(socketaddress) && getIpBans().get(socketaddress) != null && !this.getIpBans().get(socketaddress).hasExpired()) { // Paper - fix NPE with temp ip bans
2021-06-11 14:02:28 +02:00
IpBanListEntry ipbanentry = this.ipBans.get(socketaddress);
2022-06-07 23:45:11 +02:00
ichatmutablecomponent = Component.translatable("multiplayer.disconnect.banned_ip.reason", ipbanentry.getReason());
2021-06-11 14:02:28 +02:00
diff --git a/src/main/java/net/minecraft/server/players/StoredUserList.java b/src/main/java/net/minecraft/server/players/StoredUserList.java
2022-03-01 06:43:03 +01:00
index 097a25fd4ae5438161158a1018c707886cfbb959..91899909abd83611ac63633fef701be542cf64bf 100644
2021-06-11 14:02:28 +02:00
--- a/src/main/java/net/minecraft/server/players/StoredUserList.java
+++ b/src/main/java/net/minecraft/server/players/StoredUserList.java
2022-03-01 06:43:03 +01:00
@@ -13,6 +13,8 @@ import java.io.BufferedReader;
2021-06-11 14:02:28 +02:00
import java.io.BufferedWriter;
import java.io.File;
import java.io.IOException;
+import java.lang.reflect.ParameterizedType; // Paper
+import java.lang.reflect.Type; // Paper
import java.nio.charset.StandardCharsets;
import java.util.Collection;
import java.util.Iterator;
2021-06-13 01:45:00 +02:00
@@ -30,7 +32,22 @@ public abstract class StoredUserList<K, V extends StoredUserEntry<K>> {
2022-03-01 06:43:03 +01:00
private static final Logger LOGGER = LogUtils.getLogger();
2021-06-11 14:02:28 +02:00
private static final Gson GSON = (new GsonBuilder()).setPrettyPrinting().create();
private final File file;
- private final Map<String, V> map = Maps.newHashMap();
+ // Paper - replace HashMap is ConcurrentHashMap
2021-06-17 19:11:00 +02:00
+ private final Map<String, V> map = Maps.newConcurrentMap();
2021-06-11 14:02:28 +02:00
+ private boolean e = true;
+ private static final ParameterizedType f = new ParameterizedType() {
+ public Type[] getActualTypeArguments() {
+ return new Type[]{StoredUserEntry.class};
+ }
+
+ public Type getRawType() {
+ return List.class;
+ }
+
+ public Type getOwnerType() {
+ return null;
+ }
+ };
public StoredUserList(File file) {
this.file = file;
2021-06-13 01:45:00 +02:00
@@ -53,8 +70,13 @@ public abstract class StoredUserList<K, V extends StoredUserEntry<K>> {
2021-06-11 14:02:28 +02:00
@Nullable
public V get(K key) {
- this.removeExpired();
- return (V) this.map.get(this.getKeyForUser(key)); // CraftBukkit - fix decompile error
+ // Paper start
+ // this.g();
+ // return (V) this.d.get(this.a(k0)); // CraftBukkit - fix decompile error
2021-06-17 19:11:00 +02:00
+ return (V) this.map.computeIfPresent(this.getKeyForUser(key), (k, v) -> {
2021-06-11 14:02:28 +02:00
+ return v.hasExpired() ? null : v;
+ });
+ // Paper end
}
public void remove(K key) {
2021-06-16 19:48:25 +02:00
@@ -83,7 +105,8 @@ public abstract class StoredUserList<K, V extends StoredUserEntry<K>> {
2021-06-11 14:02:28 +02:00
// CraftBukkit end
public boolean isEmpty() {
- return this.map.size() < 1;
+ // return this.d.size() < 1; // Paper
2021-06-17 19:11:00 +02:00
+ return this.map.isEmpty(); // Paper - readability is the goal. As an aside, isEmpty() uses only sumCount() and a comparison. size() uses sumCount(), casts, and boolean logic
2021-06-11 14:02:28 +02:00
}
protected String getKeyForUser(K profile) {
2021-06-16 19:48:25 +02:00
@@ -95,14 +118,14 @@ public abstract class StoredUserList<K, V extends StoredUserEntry<K>> {
2021-06-11 14:02:28 +02:00
}
private void removeExpired() {
- List<K> list = Lists.newArrayList();
- Iterator iterator = this.map.values().iterator();
+ /*List<K> list = Lists.newArrayList();
+ Iterator iterator = this.d.values().iterator();
while (iterator.hasNext()) {
V v0 = (V) iterator.next(); // CraftBukkit - decompile error
if (v0.hasExpired()) {
- list.add(v0.getUser());
+ list.add(v0.getKey());
}
}
2021-06-16 19:48:25 +02:00
@@ -111,9 +134,11 @@ public abstract class StoredUserList<K, V extends StoredUserEntry<K>> {
2021-06-11 14:02:28 +02:00
while (iterator.hasNext()) {
K k0 = (K) iterator.next(); // CraftBukkit - decompile error
- this.map.remove(this.getKeyForUser(k0));
- }
+ this.d.remove(this.a(k0));
+ }*/
2021-06-17 19:11:00 +02:00
+ this.map.values().removeIf(StoredUserEntry::hasExpired);
2021-06-11 14:02:28 +02:00
+ // Paper end
}
protected abstract StoredUserEntry<K> createEntry(JsonObject json);
2021-06-16 19:48:25 +02:00
@@ -123,6 +148,7 @@ public abstract class StoredUserList<K, V extends StoredUserEntry<K>> {
2021-06-11 14:02:28 +02:00
}
public void save() throws IOException {
2021-06-16 19:48:25 +02:00
+ this.removeExpired(); // Paper - remove expired values before saving
2021-06-11 14:02:28 +02:00
JsonArray jsonarray = new JsonArray();
2021-06-13 01:45:00 +02:00
Stream<JsonObject> stream = this.map.values().stream().map((jsonlistentry) -> { // CraftBukkit - decompile error
JsonObject jsonobject = new JsonObject();