9
0
mirror of https://github.com/Winds-Studio/Leaf.git synced 2025-12-23 08:59:23 +00:00
Files
Leaf/Leaf-Server/minecraft-patches/features/0101-Use-faster-and-thread-safe-ban-list-date-format-pars.patch
2025-01-18 10:28:38 -05:00

125 lines
6.6 KiB
Diff

From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Dreeam <61569423+Dreeam-qwq@users.noreply.github.com>
Date: Mon, 11 Nov 2024 02:46:39 -0500
Subject: [PATCH] Use faster and thread-safe ban list date format parsing
Dreeam TODO: check is there need to use more accurate benchmark using jmh?
Use DateTimeFormatter since the original java SimpleDateFormat is not thread-safe
If calls DateTimeFormatter asynchronously, one data format may pollute another
This can fix the server crash on loading according to the user report.
The server crashed during initing the IP ban list, probably caused by calls in off-main.
Some performance test **only for reference**
Single thread, 10,000,000 times loop, java 21 (graalvm / zulu)
SimpleDateFormat: ~29,446ms
DateTimeFormatter: ~13,128ms
apache commons-lang's FastDateFormat: ~23,514ms
In the end, DateTimeFormatter is also fastest in three implementations in any ways,
Wether there is a high frequnently calls or not. And also thread-safe. So there is
a better solution, why not using it?
diff --git a/net/minecraft/server/players/BanListEntry.java b/net/minecraft/server/players/BanListEntry.java
index e111adec2116f922fe67ee434635e50c60dad15c..378ea667007844a08b4f215ca5f0730668e46699 100644
--- a/net/minecraft/server/players/BanListEntry.java
+++ b/net/minecraft/server/players/BanListEntry.java
@@ -9,7 +9,9 @@ import javax.annotation.Nullable;
import net.minecraft.network.chat.Component;
public abstract class BanListEntry<T> extends StoredUserEntry<T> {
- public static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss Z", Locale.ROOT);
+ //public static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss Z", Locale.ROOT); // Leaf - I assume no one will use this, if yes, why?
+ private static final java.time.ZoneId ZONE_ID = java.time.ZoneId.systemDefault();
+ public static final java.time.format.DateTimeFormatter DATE_TIME_FORMATTER = java.time.format.DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss Z"); // Leaf - Use faster and thread-safe ban list date format parsing
public static final String EXPIRES_NEVER = "forever";
protected final Date created;
protected final String source;
@@ -30,8 +32,10 @@ public abstract class BanListEntry<T> extends StoredUserEntry<T> {
Date date;
try {
- date = entryData.has("created") ? DATE_FORMAT.parse(entryData.get("created").getAsString()) : new Date();
- } catch (ParseException var7) {
+ // Leaf start - Use faster and thread-safe ban list date format parsing
+ date = entryData.has("created") ? parseToDate(entryData.get("created").getAsString()) : new Date();
+ } catch (java.time.format.DateTimeParseException var7) {
+ // Leaf end - Use faster and thread-safe ban list date format parsing
date = new Date();
}
@@ -40,8 +44,10 @@ public abstract class BanListEntry<T> extends StoredUserEntry<T> {
Date date1;
try {
- date1 = entryData.has("expires") ? DATE_FORMAT.parse(entryData.get("expires").getAsString()) : null;
- } catch (ParseException var6) {
+ // Leaf start - Use faster and thread-safe ban list date format parsing
+ date1 = entryData.has("expires") ? parseToDate(entryData.get("expires").getAsString()) : null;
+ } catch (java.time.format.DateTimeParseException var6) {
+ // Leaf end - Use faster and thread-safe ban list date format parsing
date1 = null;
}
@@ -75,9 +81,9 @@ public abstract class BanListEntry<T> extends StoredUserEntry<T> {
@Override
protected void serialize(JsonObject data) {
- data.addProperty("created", DATE_FORMAT.format(this.created));
+ data.addProperty("created", formateToString(this.created)); // Leaf - Use faster and thread-safe ban list date format parsing
data.addProperty("source", this.source);
- data.addProperty("expires", this.expires == null ? "forever" : DATE_FORMAT.format(this.expires));
+ data.addProperty("expires", this.expires == null ? "forever" : formateToString(this.expires)); // Leaf - Use faster and thread-safe ban list date format parsing
data.addProperty("reason", this.reason);
}
@@ -86,9 +92,11 @@ public abstract class BanListEntry<T> extends StoredUserEntry<T> {
Date expires = null;
try {
- expires = jsonobject.has("expires") ? BanListEntry.DATE_FORMAT.parse(jsonobject.get("expires").getAsString()) : null;
- } catch (ParseException ex) {
+ // Leaf start - Use faster and thread-safe ban list date format parsing
+ expires = jsonobject.has("expires") ? parseToDate(jsonobject.get("expires").getAsString()) : null;
+ } catch (java.time.format.DateTimeParseException ignored) {
// Guess we don't have a date
+ // Leaf end - Use faster and thread-safe ban list date format parsing
}
if (expires == null || expires.after(new Date())) {
@@ -98,4 +106,15 @@ public abstract class BanListEntry<T> extends StoredUserEntry<T> {
}
}
// CraftBukkit end
+
+ // Leaf start - Use faster and thread-safe ban list date format parsing
+ public static Date parseToDate(String string) {
+ java.time.ZonedDateTime parsedDateTime = java.time.ZonedDateTime.parse(string, DATE_TIME_FORMATTER);
+ return Date.from(parsedDateTime.toInstant());
+ }
+
+ public static String formateToString(Date date) {
+ return DATE_TIME_FORMATTER.format(date.toInstant().atZone(ZONE_ID));
+ }
+ // Leaf end - Use faster and thread-safe ban list date format parsing
}
diff --git a/net/minecraft/server/players/OldUsersConverter.java b/net/minecraft/server/players/OldUsersConverter.java
index 7dbcd9d96f052bb10127ad2b061154c23cc9ffd4..0a3b159a8629fad1a240b9be3e6025bfa1183a00 100644
--- a/net/minecraft/server/players/OldUsersConverter.java
+++ b/net/minecraft/server/players/OldUsersConverter.java
@@ -469,8 +469,10 @@ public class OldUsersConverter {
static Date parseDate(String input, Date defaultValue) {
Date date;
try {
- date = BanListEntry.DATE_FORMAT.parse(input);
- } catch (ParseException var4) {
+ // Leaf start - Use faster and thread-safe ban list date format parsing
+ date = BanListEntry.parseToDate(input);
+ } catch (java.time.format.DateTimeParseException var4) {
+ // Leaf end - Use faster and thread-safe ban list date format parsing
date = defaultValue;
}