diff --git a/leaf-server/minecraft-patches/features/0277-Optimise-TextColor.patch b/leaf-server/minecraft-patches/features/0277-Optimise-TextColor.patch new file mode 100644 index 00000000..aa40086d --- /dev/null +++ b/leaf-server/minecraft-patches/features/0277-Optimise-TextColor.patch @@ -0,0 +1,71 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: Overwrite987 +Date: Fri, 25 Jul 2025 01:04:43 +0300 +Subject: [PATCH] Optimise TextColor + +The JMH benchmark of this patch can be found in SunBox's JMH `OptimizeTextColorMisc`, `OptimizeTextColorMapGet` +and nanoTime bench `OptimizeTextColor`. + +diff --git a/net/minecraft/network/chat/TextColor.java b/net/minecraft/network/chat/TextColor.java +index a68e0999c7eb1f038a0da23b3417609443660809..35f1a3b542e5049bf00cd091b9c3c1069c188d6e 100644 +--- a/net/minecraft/network/chat/TextColor.java ++++ b/net/minecraft/network/chat/TextColor.java +@@ -15,9 +15,18 @@ import net.minecraft.ChatFormatting; + public final class TextColor { + private static final String CUSTOM_COLOR_PREFIX = "#"; + public static final Codec CODEC = Codec.STRING.comapFlatMap(TextColor::parseColor, TextColor::serialize); +- private static final Map LEGACY_FORMAT_TO_COLOR = Stream.of(ChatFormatting.values()) +- .filter(ChatFormatting::isColor) +- .collect(ImmutableMap.toImmutableMap(Function.identity(), formatting -> new TextColor(formatting.getColor(), formatting.getName(), formatting))); // CraftBukkit ++ // Leaf start - Optimise TextColor ++ private static final char[] HEX_DIGITS = new char[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'}; ++ private static final Map LEGACY_FORMAT_TO_COLOR = ++ Stream.of(ChatFormatting.values()) ++ .filter(ChatFormatting::isColor) ++ .collect(java.util.stream.Collectors.toMap( ++ Function.identity(), ++ format -> new TextColor(format.getColor(), format.getName(), format), ++ (a, b) -> a, ++ () -> new java.util.EnumMap<>(ChatFormatting.class) ++ )); ++ // Leaf end - Optimise TextColor + private static final Map NAMED_COLORS = LEGACY_FORMAT_TO_COLOR.values() + .stream() + .collect(ImmutableMap.toImmutableMap(textColor -> textColor.name, Function.identity())); +@@ -50,7 +59,17 @@ public final class TextColor { + } + + private String formatValue() { +- return String.format(Locale.ROOT, "#%06X", this.value); ++ // Leaf start - Optimise TextColor ++ return new String(new char[]{ ++ '#', ++ HEX_DIGITS[(value >> 20) & 0xF], ++ HEX_DIGITS[(value >> 16) & 0xF], ++ HEX_DIGITS[(value >> 12) & 0xF], ++ HEX_DIGITS[(value >> 8) & 0xF], ++ HEX_DIGITS[(value >> 4) & 0xF], ++ HEX_DIGITS[value & 0xF] ++ }); ++ // Leaf end - Optimise TextColor + } + + @Override +@@ -77,7 +96,7 @@ public final class TextColor { + + @Nullable + public static TextColor fromLegacyFormat(ChatFormatting formatting) { +- return LEGACY_FORMAT_TO_COLOR.get(formatting); ++ return LEGACY_FORMAT_TO_COLOR.get(formatting); // Leaf - Optimise TextColor - diff on change + } + + public static TextColor fromRgb(int color) { +@@ -85,7 +104,7 @@ public final class TextColor { + } + + public static DataResult parseColor(String color) { +- if (color.startsWith("#")) { ++ if (color.charAt(0) == '#') { // Leaf - Optimise TextColor + try { + int i = Integer.parseInt(color.substring(1), 16); + return i >= 0 && i <= 16777215