From cbd2aca344766695e4981d1e9f8484330c493f2a Mon Sep 17 00:00:00 2001 From: "Josiah (Gaming32) Glosson" Date: Thu, 9 May 2024 11:22:45 -0500 Subject: [PATCH] Migrate some stuff to fastutil --- .../gaming32/worldhost/FriendsListUpdate.java | 6 +++--- .../io/github/gaming32/worldhost/WorldHost.java | 17 ++++++++++------- .../gui/screen/OnlineFriendsScreen.java | 4 ++-- .../worldhost/gui/widget/FriendsButton.java | 4 ++-- 4 files changed, 17 insertions(+), 14 deletions(-) diff --git a/src/main/java/io/github/gaming32/worldhost/FriendsListUpdate.java b/src/main/java/io/github/gaming32/worldhost/FriendsListUpdate.java index afba0f5..550d21b 100644 --- a/src/main/java/io/github/gaming32/worldhost/FriendsListUpdate.java +++ b/src/main/java/io/github/gaming32/worldhost/FriendsListUpdate.java @@ -1,11 +1,11 @@ package io.github.gaming32.worldhost; -import java.util.Map; +import it.unimi.dsi.fastutil.objects.Object2LongMap; + import java.util.UUID; public interface FriendsListUpdate { - // TODO: fastutil - void friendsListUpdate(Map friends); + void friendsListUpdate(Object2LongMap friends); default void friendsListUpdate() { friendsListUpdate(WorldHost.ONLINE_FRIENDS); diff --git a/src/main/java/io/github/gaming32/worldhost/WorldHost.java b/src/main/java/io/github/gaming32/worldhost/WorldHost.java index 17572c5..f7c2c2f 100644 --- a/src/main/java/io/github/gaming32/worldhost/WorldHost.java +++ b/src/main/java/io/github/gaming32/worldhost/WorldHost.java @@ -18,6 +18,10 @@ import io.netty.buffer.Unpooled; import it.unimi.dsi.fastutil.longs.Long2ObjectMap; import it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap; +import it.unimi.dsi.fastutil.objects.Object2IntAVLTreeMap; +import it.unimi.dsi.fastutil.objects.Object2IntMap; +import it.unimi.dsi.fastutil.objects.Object2LongLinkedOpenHashMap; +import it.unimi.dsi.fastutil.objects.Object2LongMap; import net.minecraft.ChatFormatting; import net.minecraft.Util; import net.minecraft.client.Minecraft; @@ -58,12 +62,10 @@ import java.security.SecureRandom; import java.util.Collections; import java.util.HashMap; -import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.Optional; import java.util.Set; -import java.util.TreeMap; import java.util.UUID; import java.util.WeakHashMap; import java.util.concurrent.CompletableFuture; @@ -189,11 +191,11 @@ public class WorldHost public static final WorldHostConfig CONFIG = new WorldHostConfig(); private static List wordsForCid; - private static Map wordsForCidInverse; // TODO: fastutil + private static Object2IntMap wordsForCidInverse; public static final long MAX_CONNECTION_IDS = 1L << 42; - public static final Map ONLINE_FRIENDS = new LinkedHashMap<>(); + public static final Object2LongMap ONLINE_FRIENDS = new Object2LongLinkedOpenHashMap<>(); public static final Map ONLINE_FRIEND_PINGS = new HashMap<>(); public static final Set ONLINE_FRIEND_UPDATES = Collections.newSetFromMap(new WeakHashMap<>()); @@ -242,7 +244,8 @@ private static void init() { if (wordsForCid.size() != (1 << 14)) { throw new RuntimeException("Expected WORDS_FOR_CID to have " + (1 << 14) + " elements, but it has " + wordsForCid.size() + " elements."); } - wordsForCidInverse = new TreeMap<>(String.CASE_INSENSITIVE_ORDER); + wordsForCidInverse = new Object2IntAVLTreeMap<>(String.CASE_INSENSITIVE_ORDER); + wordsForCidInverse.defaultReturnValue(-1); for (int i = 0; i < wordsForCid.size(); i++) { wordsForCidInverse.put(wordsForCid.get(i), i); } @@ -643,8 +646,8 @@ public static Long tryParseConnectionId(String str) { long result = 0L; int shift = 0; for (final String word : words) { - final Integer part = wordsForCidInverse.get(word); - if (part == null) { + final int part = wordsForCidInverse.getInt(word); + if (part == -1) { return null; } result |= (long)part << shift; diff --git a/src/main/java/io/github/gaming32/worldhost/gui/screen/OnlineFriendsScreen.java b/src/main/java/io/github/gaming32/worldhost/gui/screen/OnlineFriendsScreen.java index b7d5df7..9d3fbf1 100644 --- a/src/main/java/io/github/gaming32/worldhost/gui/screen/OnlineFriendsScreen.java +++ b/src/main/java/io/github/gaming32/worldhost/gui/screen/OnlineFriendsScreen.java @@ -9,6 +9,7 @@ import io.github.gaming32.worldhost.gui.widget.FriendsButton; import io.github.gaming32.worldhost.mixin.ServerStatusPingerAccessor; import io.github.gaming32.worldhost.versions.Components; +import it.unimi.dsi.fastutil.objects.Object2LongMap; import net.minecraft.ChatFormatting; import net.minecraft.SharedConstants; import net.minecraft.Util; @@ -31,7 +32,6 @@ import java.util.Arrays; import java.util.LinkedHashMap; import java.util.List; -import java.util.Map; import java.util.UUID; //#if MC >= 1.20.0 @@ -212,7 +212,7 @@ private void updateButtonActivationStates() { } @Override - public void friendsListUpdate(Map friends) { + public void friendsListUpdate(Object2LongMap friends) { final var newFriends = new LinkedHashMap<>(friends); for (int i = list.children().size() - 1; i >= 0; i--) { final UUID uuid = list.children().get(i).profile.getId(); diff --git a/src/main/java/io/github/gaming32/worldhost/gui/widget/FriendsButton.java b/src/main/java/io/github/gaming32/worldhost/gui/widget/FriendsButton.java index 9e85017..d158679 100644 --- a/src/main/java/io/github/gaming32/worldhost/gui/widget/FriendsButton.java +++ b/src/main/java/io/github/gaming32/worldhost/gui/widget/FriendsButton.java @@ -4,12 +4,12 @@ import io.github.gaming32.worldhost.WorldHostComponents; import io.github.gaming32.worldhost.gui.screen.WorldHostScreen; import io.github.gaming32.worldhost.versions.Components; +import it.unimi.dsi.fastutil.objects.Object2LongMap; import net.minecraft.client.Minecraft; import net.minecraft.client.gui.Font; import net.minecraft.client.gui.components.Button; import org.jetbrains.annotations.NotNull; -import java.util.Map; import java.util.UUID; //#if MC >= 1.20.0 @@ -32,7 +32,7 @@ public FriendsButton(int x, int y, int width, int height, OnPress onPress) { } @Override - public void friendsListUpdate(Map friends) { + public void friendsListUpdate(Object2LongMap friends) { final int online = friends.size(); final var baseText = WorldHostComponents.FRIENDS.copy().append(" " + online + " "); setMessage(baseText);