9
0
mirror of https://github.com/Winds-Studio/Leaf.git synced 2025-12-26 02:19:19 +00:00

Optimise TextColor (#411)

* Create 0278-Optimise-TextColor.patch

* update patch for the new requirements

* fix patch number and save 0.5 ns

* [ci/skip] cleanup
This commit is contained in:
Overwrite987
2025-07-27 10:44:33 +03:00
committed by GitHub
parent ab1bdc7c38
commit b43bda8fb7

View File

@@ -0,0 +1,71 @@
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Overwrite987 <artem.korolev_2003@mail.ru>
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<TextColor> CODEC = Codec.STRING.comapFlatMap(TextColor::parseColor, TextColor::serialize);
- private static final Map<ChatFormatting, TextColor> 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<ChatFormatting, TextColor> 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<String, TextColor> 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<TextColor> 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