From 5883bc1153fa14ca797ebb9655e504cdd3e2e72c Mon Sep 17 00:00:00 2001 From: embeddedt <42941056+embeddedt@users.noreply.github.com> Date: Sun, 12 May 2024 16:03:50 -0400 Subject: [PATCH] Reduce memory usage of GTDynamicDataPack (#1242) --- .../gtceu/data/pack/GTDynamicDataPack.java | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/gregtechceu/gtceu/data/pack/GTDynamicDataPack.java b/src/main/java/com/gregtechceu/gtceu/data/pack/GTDynamicDataPack.java index 543bf20cb5..8f24ef2f28 100644 --- a/src/main/java/com/gregtechceu/gtceu/data/pack/GTDynamicDataPack.java +++ b/src/main/java/com/gregtechceu/gtceu/data/pack/GTDynamicDataPack.java @@ -39,7 +39,7 @@ public class GTDynamicDataPack implements PackResources { protected static final ObjectSet SERVER_DOMAINS = new ObjectOpenHashSet<>(); - protected static final Map DATA = new HashMap<>(); + protected static final Map DATA = new HashMap<>(); private final String name; @@ -70,13 +70,13 @@ public static void addRecipe(FinishedRecipe recipe) { if (DATA.containsKey(recipeId)) { GTCEu.LOGGER.error("duplicated recipe: {}", recipeId); } - DATA.put(getRecipeLocation(recipeId), recipeJson); + DATA.put(getRecipeLocation(recipeId), recipeJson.toString().getBytes(StandardCharsets.UTF_8)); if (recipe.serializeAdvancement() != null) { JsonObject advancement = recipe.serializeAdvancement(); if (ConfigHolder.INSTANCE.dev.dumpRecipes) { writeJson(recipe.getAdvancementId(), "advancements", parent, advancement); } - DATA.put(getAdvancementLocation(Objects.requireNonNull(recipe.getAdvancementId())), advancement); + DATA.put(getAdvancementLocation(Objects.requireNonNull(recipe.getAdvancementId())), advancement.toString().getBytes(StandardCharsets.UTF_8)); } } @@ -108,7 +108,7 @@ public static void writeJson(ResourceLocation id, @Nullable String subdir, Path public static void addAdvancement(ResourceLocation loc, JsonObject obj) { ResourceLocation l = getAdvancementLocation(loc); synchronized (DATA) { - DATA.put(l, obj); + DATA.put(l, obj.toString().getBytes(StandardCharsets.UTF_8)); } } @@ -121,8 +121,9 @@ public IoSupplier getRootResource(String... elements) { @Override public IoSupplier getResource(PackType type, ResourceLocation location) { if (type == PackType.SERVER_DATA) { - if (DATA.containsKey(location)) - return () -> new ByteArrayInputStream(DATA.get(location).toString().getBytes(StandardCharsets.UTF_8)); + var byteArray = DATA.get(location); + if (byteArray != null) + return () -> new ByteArrayInputStream(byteArray); else return null; } else { return null;