From 2ff73b53e6d0394344247aa487ea399ab90f1367 Mon Sep 17 00:00:00 2001 From: Beanes Date: Fri, 24 Apr 2026 13:43:22 +0200 Subject: [PATCH 1/5] Small optimization to prevent blocking netty threads on UUID.randomUUID() --- .../VelocityBossBarImplementation.java | 3 +- .../proxy/tablist/VelocityTabListLegacy.java | 3 +- .../proxy/util/FastRandomUuid.java | 32 +++++++++++++++++++ 3 files changed, 36 insertions(+), 2 deletions(-) create mode 100644 proxy/src/main/java/com/velocitypowered/proxy/util/FastRandomUuid.java diff --git a/proxy/src/main/java/com/velocitypowered/proxy/adventure/VelocityBossBarImplementation.java b/proxy/src/main/java/com/velocitypowered/proxy/adventure/VelocityBossBarImplementation.java index e2451188ad..77963b54d7 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/adventure/VelocityBossBarImplementation.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/adventure/VelocityBossBarImplementation.java @@ -21,6 +21,7 @@ import com.velocitypowered.proxy.connection.client.ConnectedPlayer; import com.velocitypowered.proxy.protocol.packet.BossBarPacket; import com.velocitypowered.proxy.protocol.packet.chat.ComponentHolder; +import com.velocitypowered.proxy.util.FastRandomUuid; import java.util.Collections; import java.util.Set; import java.util.UUID; @@ -36,7 +37,7 @@ public final class VelocityBossBarImplementation implements BossBar.Listener, BossBarImplementation { private final Set viewers = Collections.newSetFromMap( new MapMaker().weakKeys().makeMap()); - private final UUID id = UUID.randomUUID(); + private final UUID id = FastRandomUuid.generate(); private final BossBar bar; public static VelocityBossBarImplementation get(final BossBar bar) { diff --git a/proxy/src/main/java/com/velocitypowered/proxy/tablist/VelocityTabListLegacy.java b/proxy/src/main/java/com/velocitypowered/proxy/tablist/VelocityTabListLegacy.java index 037ab26286..0a729014e3 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/tablist/VelocityTabListLegacy.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/tablist/VelocityTabListLegacy.java @@ -26,6 +26,7 @@ import com.velocitypowered.proxy.connection.client.ConnectedPlayer; import com.velocitypowered.proxy.protocol.packet.LegacyPlayerListItemPacket; import com.velocitypowered.proxy.protocol.packet.LegacyPlayerListItemPacket.Item; +import com.velocitypowered.proxy.util.FastRandomUuid; import java.util.Collections; import java.util.Map; import java.util.Optional; @@ -95,7 +96,7 @@ public void processLegacy(LegacyPlayerListItemPacket packet) { entry.setLatencyInternal(item.getLatency()); } } else { - UUID uuid = UUID.randomUUID(); // Use a fake uuid to preserve function of custom entries + UUID uuid = FastRandomUuid.generate(); // Use a fake uuid to preserve function of custom entries nameMapping.put(item.getName(), uuid); entries.put(uuid, (KeyedVelocityTabListEntry) TabListEntry.builder() .tabList(this) diff --git a/proxy/src/main/java/com/velocitypowered/proxy/util/FastRandomUuid.java b/proxy/src/main/java/com/velocitypowered/proxy/util/FastRandomUuid.java new file mode 100644 index 0000000000..e58ac5e0c4 --- /dev/null +++ b/proxy/src/main/java/com/velocitypowered/proxy/util/FastRandomUuid.java @@ -0,0 +1,32 @@ +/* + * Copyright (C) 2026 Velocity Contributors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package com.velocitypowered.proxy.util; + +import java.util.UUID; +import java.util.concurrent.ThreadLocalRandom; + +/** + * A utility class for generating random UUIDs without the overhead of secure random generation. + * Not official UUID v4 version, but should be sufficient for most use cases. + */ +public class FastRandomUuid { + public static UUID generate() { + ThreadLocalRandom random = ThreadLocalRandom.current(); + return new UUID(random.nextLong(), random.nextLong()); + } +} From c94104bf25d837492b65e9e425ba1341ae431b57 Mon Sep 17 00:00:00 2001 From: Beanes Date: Fri, 24 Apr 2026 14:02:55 +0200 Subject: [PATCH 2/5] Change FastRandomUuid to be a valid uuid v4 --- .../com/velocitypowered/proxy/util/FastRandomUuid.java | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/proxy/src/main/java/com/velocitypowered/proxy/util/FastRandomUuid.java b/proxy/src/main/java/com/velocitypowered/proxy/util/FastRandomUuid.java index e58ac5e0c4..4390abf609 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/util/FastRandomUuid.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/util/FastRandomUuid.java @@ -21,12 +21,14 @@ import java.util.concurrent.ThreadLocalRandom; /** - * A utility class for generating random UUIDs without the overhead of secure random generation. - * Not official UUID v4 version, but should be sufficient for most use cases. + * Generates valid random UUID v4 values using {@link ThreadLocalRandom} instead of + * {@link java.security.SecureRandom}, avoiding contention on the shared secure random instance. */ public class FastRandomUuid { public static UUID generate() { ThreadLocalRandom random = ThreadLocalRandom.current(); - return new UUID(random.nextLong(), random.nextLong()); + long msb = (random.nextLong() & 0xffffffffffff0fffL) | 0x0000000000004000L; // version 4 + long lsb = (random.nextLong() & 0x3fffffffffffffffL) | 0x8000000000000000L; // IETF variant + return new UUID(msb, lsb); } } From 45420433b9d293175d2afbfd89dda92136225650 Mon Sep 17 00:00:00 2001 From: Beanes Date: Fri, 24 Apr 2026 20:18:21 +0200 Subject: [PATCH 3/5] Update javadoc for spotless --- .../com/velocitypowered/proxy/util/FastRandomUuid.java | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/proxy/src/main/java/com/velocitypowered/proxy/util/FastRandomUuid.java b/proxy/src/main/java/com/velocitypowered/proxy/util/FastRandomUuid.java index 4390abf609..25c7c3d31c 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/util/FastRandomUuid.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/util/FastRandomUuid.java @@ -21,10 +21,16 @@ import java.util.concurrent.ThreadLocalRandom; /** - * Generates valid random UUID v4 values using {@link ThreadLocalRandom} instead of - * {@link java.security.SecureRandom}, avoiding contention on the shared secure random instance. + * Utility for generating random UUID v4 values using {@link ThreadLocalRandom}. */ public class FastRandomUuid { + + /** + * Generates a random UUID v4 using {@link ThreadLocalRandom} instead of + * {@link java.security.SecureRandom}, avoiding contention on the shared secure random instance. + * + * @return a new random {@link UUID} + */ public static UUID generate() { ThreadLocalRandom random = ThreadLocalRandom.current(); long msb = (random.nextLong() & 0xffffffffffff0fffL) | 0x0000000000004000L; // version 4 From 794b52e622e1c5951e59664ca2cb1ca3f58b2b02 Mon Sep 17 00:00:00 2001 From: Beanes Date: Fri, 24 Apr 2026 20:26:03 +0200 Subject: [PATCH 4/5] Migrate method to VelocityTabListLegacy and add notice that it is insecure --- .../VelocityBossBarImplementation.java | 3 +- .../proxy/tablist/VelocityTabListLegacy.java | 19 ++++++--- .../proxy/util/FastRandomUuid.java | 40 ------------------- 3 files changed, 14 insertions(+), 48 deletions(-) delete mode 100644 proxy/src/main/java/com/velocitypowered/proxy/util/FastRandomUuid.java diff --git a/proxy/src/main/java/com/velocitypowered/proxy/adventure/VelocityBossBarImplementation.java b/proxy/src/main/java/com/velocitypowered/proxy/adventure/VelocityBossBarImplementation.java index 77963b54d7..e2451188ad 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/adventure/VelocityBossBarImplementation.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/adventure/VelocityBossBarImplementation.java @@ -21,7 +21,6 @@ import com.velocitypowered.proxy.connection.client.ConnectedPlayer; import com.velocitypowered.proxy.protocol.packet.BossBarPacket; import com.velocitypowered.proxy.protocol.packet.chat.ComponentHolder; -import com.velocitypowered.proxy.util.FastRandomUuid; import java.util.Collections; import java.util.Set; import java.util.UUID; @@ -37,7 +36,7 @@ public final class VelocityBossBarImplementation implements BossBar.Listener, BossBarImplementation { private final Set viewers = Collections.newSetFromMap( new MapMaker().weakKeys().makeMap()); - private final UUID id = FastRandomUuid.generate(); + private final UUID id = UUID.randomUUID(); private final BossBar bar; public static VelocityBossBarImplementation get(final BossBar bar) { diff --git a/proxy/src/main/java/com/velocitypowered/proxy/tablist/VelocityTabListLegacy.java b/proxy/src/main/java/com/velocitypowered/proxy/tablist/VelocityTabListLegacy.java index 0a729014e3..d1d8a8f51c 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/tablist/VelocityTabListLegacy.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/tablist/VelocityTabListLegacy.java @@ -26,12 +26,12 @@ import com.velocitypowered.proxy.connection.client.ConnectedPlayer; import com.velocitypowered.proxy.protocol.packet.LegacyPlayerListItemPacket; import com.velocitypowered.proxy.protocol.packet.LegacyPlayerListItemPacket.Item; -import com.velocitypowered.proxy.util.FastRandomUuid; import java.util.Collections; import java.util.Map; import java.util.Optional; import java.util.UUID; import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ThreadLocalRandom; import net.kyori.adventure.text.Component; import org.checkerframework.checker.nullness.qual.Nullable; @@ -96,7 +96,7 @@ public void processLegacy(LegacyPlayerListItemPacket packet) { entry.setLatencyInternal(item.getLatency()); } } else { - UUID uuid = FastRandomUuid.generate(); // Use a fake uuid to preserve function of custom entries + UUID uuid = generateInsecureRandomUuid(); // Use a fake uuid to preserve function of custom entries nameMapping.put(item.getName(), uuid); entries.put(uuid, (KeyedVelocityTabListEntry) TabListEntry.builder() .tabList(this) @@ -149,9 +149,16 @@ public TabListEntry buildEntry(GameProfile profile, @Nullable Component displayN return new VelocityTabListEntryLegacy(this, profile, displayName, latency, gameMode); } - @Override - public TabListEntry buildEntry(GameProfile profile, @Nullable Component displayName, int latency, - int gameMode, @Nullable ChatSession chatSession, boolean listed, int listOrder, boolean showHat) { - return new VelocityTabListEntryLegacy(this, profile, displayName, latency, gameMode); + /** + * Generates a random UUID v4 using {@link ThreadLocalRandom}. The result is a structurally valid + * UUID v4 but is not cryptographically secure + * + * @return a new random {@link UUID} + */ + private static UUID generateInsecureRandomUuid() { + ThreadLocalRandom random = ThreadLocalRandom.current(); + long msb = (random.nextLong() & 0xffffffffffff0fffL) | 0x0000000000004000L; // version 4 + long lsb = (random.nextLong() & 0x3fffffffffffffffL) | 0x8000000000000000L; // IETF variant + return new UUID(msb, lsb); } } diff --git a/proxy/src/main/java/com/velocitypowered/proxy/util/FastRandomUuid.java b/proxy/src/main/java/com/velocitypowered/proxy/util/FastRandomUuid.java deleted file mode 100644 index 25c7c3d31c..0000000000 --- a/proxy/src/main/java/com/velocitypowered/proxy/util/FastRandomUuid.java +++ /dev/null @@ -1,40 +0,0 @@ -/* - * Copyright (C) 2026 Velocity Contributors - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -package com.velocitypowered.proxy.util; - -import java.util.UUID; -import java.util.concurrent.ThreadLocalRandom; - -/** - * Utility for generating random UUID v4 values using {@link ThreadLocalRandom}. - */ -public class FastRandomUuid { - - /** - * Generates a random UUID v4 using {@link ThreadLocalRandom} instead of - * {@link java.security.SecureRandom}, avoiding contention on the shared secure random instance. - * - * @return a new random {@link UUID} - */ - public static UUID generate() { - ThreadLocalRandom random = ThreadLocalRandom.current(); - long msb = (random.nextLong() & 0xffffffffffff0fffL) | 0x0000000000004000L; // version 4 - long lsb = (random.nextLong() & 0x3fffffffffffffffL) | 0x8000000000000000L; // IETF variant - return new UUID(msb, lsb); - } -} From c04928ccce2ddd9782918e456f85a9a5118167f1 Mon Sep 17 00:00:00 2001 From: Beanes Date: Sun, 24 May 2026 17:21:36 +0200 Subject: [PATCH 5/5] Bring back deleted override --- .../proxy/tablist/VelocityTabListLegacy.java | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/proxy/src/main/java/com/velocitypowered/proxy/tablist/VelocityTabListLegacy.java b/proxy/src/main/java/com/velocitypowered/proxy/tablist/VelocityTabListLegacy.java index d1d8a8f51c..76cea8415e 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/tablist/VelocityTabListLegacy.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/tablist/VelocityTabListLegacy.java @@ -149,6 +149,12 @@ public TabListEntry buildEntry(GameProfile profile, @Nullable Component displayN return new VelocityTabListEntryLegacy(this, profile, displayName, latency, gameMode); } + @Override + public TabListEntry buildEntry(GameProfile profile, @Nullable Component displayName, int latency, + int gameMode, @Nullable ChatSession chatSession, boolean listed, int listOrder, boolean showHat) { + return new VelocityTabListEntryLegacy(this, profile, displayName, latency, gameMode); + } + /** * Generates a random UUID v4 using {@link ThreadLocalRandom}. The result is a structurally valid * UUID v4 but is not cryptographically secure