9
0
mirror of https://github.com/Winds-Studio/Leaf.git synced 2025-12-19 15:09:25 +00:00
Files
Leaf/leaf-server/minecraft-patches/features/0181-Use-faster-and-thread-safe-ban-list-date-format-pars.patch
hayanesuru 6300dc3cfe Revert AI goal selector to vanilla behavior (#458)
* Revert AI goal selector to vanilla behavior

* remove config

* Remove config & Update patch comments

* rename

* re apply
2025-08-15 02:50:55 +08:00

127 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..72f64d5812411be0f0bc5456caff87d63a4cbbb9 100644
--- a/net/minecraft/server/players/BanListEntry.java
+++ b/net/minecraft/server/players/BanListEntry.java
@@ -9,7 +9,11 @@ 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);
+ // Leaf start - Use faster and thread-safe ban list date format parsing
+ //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 end - 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 +34,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 +46,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 +83,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 +94,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 ex) {
// 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 +108,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());
+ }
+
+ private 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 194082763f41a58dc8ee0241a2eeb6dd39dff88d..b39eeaf1515792ccd36d090045f958e2616f65c2 100644
--- a/net/minecraft/server/players/OldUsersConverter.java
+++ b/net/minecraft/server/players/OldUsersConverter.java
@@ -466,8 +466,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;
}