From 8095ee0c36e412b7b0ad5d425d123a618df5268b Mon Sep 17 00:00:00 2001 From: nea Date: Sun, 15 Jan 2023 19:21:00 +0100 Subject: Performance improvements in uuid from item getting --- .../moulberry/notenoughupdates/NEUManager.java | 4 +- .../miscfeatures/ItemCustomizeManager.java | 21 +------- src/test/kotlin/nontests/PerformanceTest.kt | 61 ++++++++++++++++++++++ 3 files changed, 65 insertions(+), 21 deletions(-) create mode 100644 src/test/kotlin/nontests/PerformanceTest.kt (limited to 'src') diff --git a/src/main/java/io/github/moulberry/notenoughupdates/NEUManager.java b/src/main/java/io/github/moulberry/notenoughupdates/NEUManager.java index ce15b09a..5c3083a3 100644 --- a/src/main/java/io/github/moulberry/notenoughupdates/NEUManager.java +++ b/src/main/java/io/github/moulberry/notenoughupdates/NEUManager.java @@ -748,7 +748,7 @@ public class NEUManager { } } - public String getUUIDFromNBT(NBTTagCompound tag) { + public static String getUUIDFromNBT(NBTTagCompound tag) { String uuid = null; if (tag != null && tag.hasKey("ExtraAttributes", 10)) { NBTTagCompound ea = tag.getCompoundTag("ExtraAttributes"); @@ -965,7 +965,7 @@ public class NEUManager { .resolveInternalName(); } - public String getUUIDForItem(ItemStack stack) { + public static String getUUIDForItem(ItemStack stack) { if (stack == null) return null; NBTTagCompound tag = stack.getTagCompound(); return getUUIDFromNBT(tag); diff --git a/src/main/java/io/github/moulberry/notenoughupdates/miscfeatures/ItemCustomizeManager.java b/src/main/java/io/github/moulberry/notenoughupdates/miscfeatures/ItemCustomizeManager.java index 15fc8ef8..dad801ec 100644 --- a/src/main/java/io/github/moulberry/notenoughupdates/miscfeatures/ItemCustomizeManager.java +++ b/src/main/java/io/github/moulberry/notenoughupdates/miscfeatures/ItemCustomizeManager.java @@ -21,6 +21,7 @@ package io.github.moulberry.notenoughupdates.miscfeatures; import com.google.gson.Gson; import com.google.gson.GsonBuilder; +import io.github.moulberry.notenoughupdates.NEUManager; import io.github.moulberry.notenoughupdates.NotEnoughUpdates; import io.github.moulberry.notenoughupdates.core.ChromaColour; import net.minecraft.client.Minecraft; @@ -67,8 +68,6 @@ public class ItemCustomizeManager { private static final Gson GSON = new GsonBuilder().setPrettyPrinting().create(); private static ItemDataMap itemDataMap = new ItemDataMap(); - private static final HashMap itemUuidCache = new HashMap<>(); - public static class ItemDataMap { public HashMap itemData = new HashMap<>(); } @@ -269,25 +268,10 @@ public class ItemCustomizeManager { return CUSTOM_GLINT_TEXTURE; } - public static String getUuidForItem(ItemStack stack) { - if (!stack.hasTagCompound()) return null; - - int nbtHash = stack.getTagCompound().hashCode(); - - if (itemUuidCache.containsKey(nbtHash)) { - return itemUuidCache.get(nbtHash); - } - - String uuid = NotEnoughUpdates.INSTANCE.manager.getUUIDForItem(stack); - - itemUuidCache.put(nbtHash, uuid); - return uuid; - } - public static ItemData getDataForItem(ItemStack stack) { if (stack == null) return null; - String uuid = getUuidForItem(stack); + String uuid = NEUManager.getUUIDForItem(stack); if (uuid == null) { return null; @@ -297,7 +281,6 @@ public class ItemCustomizeManager { } public static void tick() { - itemUuidCache.clear(); disableTextureBinding = false; } diff --git a/src/test/kotlin/nontests/PerformanceTest.kt b/src/test/kotlin/nontests/PerformanceTest.kt new file mode 100644 index 00000000..14dbc121 --- /dev/null +++ b/src/test/kotlin/nontests/PerformanceTest.kt @@ -0,0 +1,61 @@ +/* + * Copyright (C) 2023 NotEnoughUpdates contributors + * + * This file is part of NotEnoughUpdates. + * + * NotEnoughUpdates is free software: you can redistribute it + * and/or modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation, either + * version 3 of the License, or (at your option) any later version. + * + * NotEnoughUpdates 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with NotEnoughUpdates. If not, see . + */ + +package nontests + +import net.minecraft.nbt.NBTTagCompound +import kotlin.random.Random +import kotlin.time.ExperimentalTime +import kotlin.time.measureTime + + +fun main() { + val simple = NBTTagCompound().apply { + setTag("ExtraAttributes", NBTTagCompound().apply { + setString("uuid", "08248f07-41c1-4353-8e37-5225939cb559") + }) + } + val complex = simple.copy() as NBTTagCompound + for (i in 0..100) { + complex.setInteger("tag$i", Random.nextInt()) + } + testPerf(simple, "Simple") + testPerf(complex, "Complex") + +} + +@OptIn(ExperimentalTime::class) +fun testPerf(tag: NBTTagCompound, name: String) { + val hashCodeTime = measureTime { + for (i in 0..100000) { + tag.hashCode() + } + } + val accessTime = measureTime { + for (i in 0..100000) { + tag.getCompoundTag("ExtraAttributes").getString("uuid") + } + } + println("$name - hashCode: $hashCodeTime") + println("$name - accesTime: $accessTime") +} + + + + -- cgit