From 503296a9cfb50b51ade3b53449209627e1c380e5 Mon Sep 17 00:00:00 2001 From: Camotoy <20743703+Camotoy@users.noreply.github.com> Date: Fri, 7 Apr 2023 14:08:22 -0400 Subject: [PATCH] Decrease final startup memory usage --- .../geyser/registry/loader/NbtRegistryLoader.java | 2 +- .../registry/populator/BlockRegistryPopulator.java | 9 ++++++++- .../translator/protocol/java/JavaCommandsTranslator.java | 2 +- .../java/org/geysermc/geyser/util/CooldownUtils.java | 5 +++-- 4 files changed, 13 insertions(+), 5 deletions(-) diff --git a/core/src/main/java/org/geysermc/geyser/registry/loader/NbtRegistryLoader.java b/core/src/main/java/org/geysermc/geyser/registry/loader/NbtRegistryLoader.java index 8b3ed1be9..fcdbfcab3 100644 --- a/core/src/main/java/org/geysermc/geyser/registry/loader/NbtRegistryLoader.java +++ b/core/src/main/java/org/geysermc/geyser/registry/loader/NbtRegistryLoader.java @@ -37,7 +37,7 @@ public class NbtRegistryLoader implements RegistryLoader { @Override public NbtMap load(String input) { - try (NBTInputStream nbtInputStream = NbtUtils.createNetworkReader(GeyserImpl.getInstance().getBootstrap().getResource(input))) { + try (NBTInputStream nbtInputStream = NbtUtils.createNetworkReader(GeyserImpl.getInstance().getBootstrap().getResource(input), true, true)) { return (NbtMap) nbtInputStream.readTag(); } catch (Exception e) { throw new AssertionError("Failed to load registrations for " + input, e); diff --git a/core/src/main/java/org/geysermc/geyser/registry/populator/BlockRegistryPopulator.java b/core/src/main/java/org/geysermc/geyser/registry/populator/BlockRegistryPopulator.java index 99815709a..6f9267c2a 100644 --- a/core/src/main/java/org/geysermc/geyser/registry/populator/BlockRegistryPopulator.java +++ b/core/src/main/java/org/geysermc/geyser/registry/populator/BlockRegistryPopulator.java @@ -28,6 +28,8 @@ package org.geysermc.geyser.registry.populator; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.node.ArrayNode; import com.google.common.collect.ImmutableMap; +import com.google.common.collect.Interner; +import com.google.common.collect.Interners; import it.unimi.dsi.fastutil.objects.*; import org.cloudburstmc.nbt.*; import org.cloudburstmc.protocol.bedrock.codec.v544.Bedrock_v544; @@ -84,10 +86,14 @@ public final class BlockRegistryPopulator { }) .build(); + // We can keep this strong as nothing should be garbage collected + // Safe to intern since Cloudburst NBT is immutable + Interner statesInterner = Interners.newStrongInterner(); + for (Map.Entry, BiFunction> palette : blockMappers.entrySet()) { NbtList blocksTag; try (InputStream stream = GeyserImpl.getInstance().getBootstrap().getResource(String.format("bedrock/block_palette.%s.nbt", palette.getKey().key())); - NBTInputStream nbtInputStream = new NBTInputStream(new DataInputStream(new GZIPInputStream(stream)))) { + NBTInputStream nbtInputStream = new NBTInputStream(new DataInputStream(new GZIPInputStream(stream)), true, true)) { NbtMap blockPalette = (NbtMap) nbtInputStream.readTag(); blocksTag = (NbtList) blockPalette.getList("blocks", NbtType.COMPOUND); } catch (Exception e) { @@ -102,6 +108,7 @@ public final class BlockRegistryPopulator { for (int i = 0; i < blocksTag.size(); i++) { NbtMapBuilder builder = blocksTag.get(i).toBuilder(); builder.remove("name_hash"); // Quick workaround - was added in 1.19.20 + builder.putCompound("states", statesInterner.intern((NbtMap) builder.remove("states"))); NbtMap tag = builder.build(); if (blockStateOrderedMap.containsKey(tag)) { throw new AssertionError("Duplicate block states in Bedrock palette: " + tag); diff --git a/core/src/main/java/org/geysermc/geyser/translator/protocol/java/JavaCommandsTranslator.java b/core/src/main/java/org/geysermc/geyser/translator/protocol/java/JavaCommandsTranslator.java index 679d458d4..e5dc9c579 100644 --- a/core/src/main/java/org/geysermc/geyser/translator/protocol/java/JavaCommandsTranslator.java +++ b/core/src/main/java/org/geysermc/geyser/translator/protocol/java/JavaCommandsTranslator.java @@ -172,7 +172,7 @@ public class JavaCommandsTranslator extends PacketTranslator> values = new LinkedHashMap<>(); // Is this right? for (String s : entry.getValue()) { - values.put(s, Set.of(CommandEnumConstraint.ALLOW_ALIASES)); + values.put(s, EnumSet.of(CommandEnumConstraint.ALLOW_ALIASES)); } // Create a basic alias diff --git a/core/src/main/java/org/geysermc/geyser/util/CooldownUtils.java b/core/src/main/java/org/geysermc/geyser/util/CooldownUtils.java index f2ab4d424..c00e389fd 100644 --- a/core/src/main/java/org/geysermc/geyser/util/CooldownUtils.java +++ b/core/src/main/java/org/geysermc/geyser/util/CooldownUtils.java @@ -29,6 +29,7 @@ import org.cloudburstmc.protocol.bedrock.packet.SetTitlePacket; import lombok.Getter; import org.geysermc.geyser.session.GeyserSession; import org.geysermc.geyser.session.cache.PreferencesCache; +import org.geysermc.geyser.text.ChatColor; import java.util.concurrent.TimeUnit; @@ -131,12 +132,12 @@ public class CooldownUtils { int darkGrey = (int) Math.floor(10d * cooldown); int grey = 10 - darkGrey; - StringBuilder builder = new StringBuilder("§8"); + StringBuilder builder = new StringBuilder(ChatColor.DARK_GRAY); while (darkGrey > 0) { builder.append("˙"); darkGrey--; } - builder.append("§7"); + builder.append(ChatColor.GRAY); while (grey > 0) { builder.append("˙"); grey--;