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

117 lines
6.2 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
2023-12-06 20:40:37 +01:00
index 5607616c933556de00bfb4218ba75ee477bb2201..27bfddd847175717697fb589b820ec5a81e08578 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
2023-12-05 23:12:48 +01:00
@@ -634,7 +634,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
2023-12-06 19:26:53 +01:00
index 4d6f5e627d386f9ca2d7653b0f485c82a13557f5..665120a62525f56912263a3e1b6f12f6c3e15dec 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
2023-12-06 19:26:53 +01:00
@@ -31,7 +31,7 @@ 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();
2023-12-06 19:26:53 +01:00
+ private final Map<String, V> map = Maps.newConcurrentMap(); // Paper - replace HashMap is ConcurrentHashMap
2021-06-11 14:02:28 +02:00
public StoredUserList(File file) {
this.file = file;
2023-12-06 19:26:53 +01:00
@@ -54,8 +54,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) {
2023-12-06 19:26:53 +01:00
@@ -78,7 +83,8 @@ public abstract class StoredUserList<K, V extends StoredUserEntry<K>> {
Updated Upstream (Bukkit/CraftBukkit/Spigot) (#9440) Upstream has released updates that appear 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: 01aa02eb PR-858: Add LivingEntity#playHurtAnimation() 9421320f PR-884: Refinements to new ban API for improved compatibility and correctness 37a60b45 SPIGOT-6455, SPIGOT-7030, PR-750: Improve ban API 4eeb174b All smithing inventories are now the new smithing inventory f2bb168e PR-880: Add methods to get/set FallingBlock CancelDrop e7a807fa PR-879: Add Player#sendHealthUpdate() 692b8e96 SPIGOT-7370: Remove float value conversion in plugin.yml 2d033390 SPIGOT-7403: Add direct API for waxed signs 16a08373 PR-876: Add missing Raider API and 'no action ticks' CraftBukkit Changes: b60a95c8c PR-1189: Add LivingEntity#playHurtAnimation() 95c335c63 PR-1226: Fix VehicleEnterEvent not being called for certain entities 0a0fc3bee PR-1227: Refinements to new ban API for improved compatibility and correctness 0d0b1e5dc Revert bad change to PathfinderGoalSit causing all cats to sit 648196070 SPIGOT-6455, SPIGOT-7030, PR-1054: Improve ban API 31fe848d6 All smithing inventories are now the new smithing inventory 9a919a143 SPIGOT-7416: SmithItemEvent not firing in Smithing Table 9f64f0d22 PR-1221: Add methods to get/set FallingBlock CancelDrop 3be9ac171 PR-1220: Add Player#sendHealthUpdate() c1279f775 PR-1209: Clean up various patches c432e4397 Fix Raider#setCelebrating() implementation 504d96665 SPIGOT-7403: Add direct API for waxed signs c68c1f1b3 PR-1216: Add missing Raider API and 'no action ticks' 85b89c3dd Increase outdated build delay Spigot Changes: 9ebce8af Rebuild patches 64b565e6 Rebuild patches
2023-07-04 10:22:56 +02:00
}
2021-06-11 14:02:28 +02:00
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) {
2023-12-06 19:26:53 +01:00
@@ -90,14 +96,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());
}
}
2023-12-06 19:26:53 +01:00
@@ -106,9 +112,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);
2023-12-06 19:26:53 +01:00
@@ -118,6 +126,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();