diff --git a/src/main/java/org/dimdev/jeid/mixin/core/misc/MixinStatList.java b/src/main/java/org/dimdev/jeid/mixin/core/misc/MixinStatList.java index 4700e41..7be72bc 100644 --- a/src/main/java/org/dimdev/jeid/mixin/core/misc/MixinStatList.java +++ b/src/main/java/org/dimdev/jeid/mixin/core/misc/MixinStatList.java @@ -1,6 +1,8 @@ package org.dimdev.jeid.mixin.core.misc; import com.llamalad7.mixinextras.sugar.Local; +import it.unimi.dsi.fastutil.objects.ObjectOpenHashSet; +import it.unimi.dsi.fastutil.objects.Reference2ObjectOpenHashMap; import net.minecraft.block.Block; import net.minecraft.init.Blocks; import net.minecraft.item.Item; @@ -23,10 +25,9 @@ import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; import java.util.ArrayList; -import java.util.HashMap; -import java.util.HashSet; import java.util.List; import java.util.Map; +import java.util.Set; /** * Rewrite most of the class to support an unlimited number of IDs (map rather than array). @@ -63,17 +64,17 @@ public final class MixinStatList { @Shadow private static StatBase[] OBJECTS_DROPPED_STATS; @Unique - private static final Map BLOCKS_STATS_MAP = new HashMap<>(); + private static final Map BLOCKS_STATS_MAP = new Reference2ObjectOpenHashMap<>(); @Unique - private static final Map CRAFTS_STATS_MAP = new HashMap<>(); + private static final Map CRAFTS_STATS_MAP = new Reference2ObjectOpenHashMap<>(); @Unique - private static final Map OBJECT_USE_STATS_MAP = new HashMap<>(); + private static final Map OBJECT_USE_STATS_MAP = new Reference2ObjectOpenHashMap<>(); @Unique - private static final Map OBJECT_BREAK_STATS_MAP = new HashMap<>(); + private static final Map OBJECT_BREAK_STATS_MAP = new Reference2ObjectOpenHashMap<>(); @Unique - private static final Map OBJECTS_PICKED_UP_STATS_MAP = new HashMap<>(); + private static final Map OBJECTS_PICKED_UP_STATS_MAP = new Reference2ObjectOpenHashMap<>(); @Unique - private static final Map OBJECTS_DROPPED_STATS_MAP = new HashMap<>(); + private static final Map OBJECTS_DROPPED_STATS_MAP = new Reference2ObjectOpenHashMap<>(); /** * @reason Reduce memory footprint of stat arrays @@ -364,7 +365,7 @@ public final class MixinStatList { int requiredCapacity = BLOCKS_STATS_MAP.size() + CRAFTS_STATS_MAP.size() + OBJECT_USE_STATS_MAP.size() + OBJECT_BREAK_STATS_MAP.size() + OBJECTS_PICKED_UP_STATS_MAP.size() + OBJECTS_DROPPED_STATS_MAP.size(); - HashSet knownStats = new HashSet<>((int) Math.ceil(requiredCapacity / 0.75f)); + Set knownStats = new ObjectOpenHashSet<>(requiredCapacity + 1); knownStats.addAll(BLOCKS_STATS_MAP.values()); knownStats.addAll(CRAFTS_STATS_MAP.values()); knownStats.addAll(OBJECT_USE_STATS_MAP.values()); diff --git a/src/main/java/org/dimdev/jeid/mixin/core/world/MixinBlockStateContainer.java b/src/main/java/org/dimdev/jeid/mixin/core/world/MixinBlockStateContainer.java index d7a225a..6fefc8e 100644 --- a/src/main/java/org/dimdev/jeid/mixin/core/world/MixinBlockStateContainer.java +++ b/src/main/java/org/dimdev/jeid/mixin/core/world/MixinBlockStateContainer.java @@ -1,5 +1,7 @@ package org.dimdev.jeid.mixin.core.world; +import it.unimi.dsi.fastutil.objects.Reference2IntMap; +import it.unimi.dsi.fastutil.objects.Reference2IntOpenHashMap; import net.minecraft.block.Block; import net.minecraft.block.state.IBlockState; import net.minecraft.init.Blocks; @@ -14,9 +16,6 @@ import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; -import java.util.HashMap; -import java.util.Map; - @Mixin(BlockStateContainer.class) public abstract class MixinBlockStateContainer implements INewBlockStateContainer { @Unique @@ -53,13 +52,14 @@ public void setLegacyAdd2(NibbleArray add2) { @SuppressWarnings("deprecation") @Inject(method = "getDataForNBT", at = @At("HEAD"), cancellable = true) private void reid$newGetDataForNBT(byte[] blockIds, NibbleArray data, CallbackInfoReturnable cir) { - HashMap stateIDMap = new HashMap<>(); + Reference2IntMap stateIDMap = new Reference2IntOpenHashMap<>(); int nextID = 0; for (int index = 0; index < 4096; ++index) { IBlockState state = get(index); - Integer paletteID = stateIDMap.get(state); - if (paletteID == null) { - paletteID = nextID++; + int paletteID = stateIDMap.getOrDefault(state, -1); + if (paletteID == -1) { + paletteID = nextID; + ++nextID; stateIDMap.put(state, paletteID); } @@ -72,8 +72,8 @@ public void setLegacyAdd2(NibbleArray add2) { } temporaryPalette = new int[nextID]; - for (Map.Entry entry : stateIDMap.entrySet()) { - temporaryPalette[entry.getValue()] = Block.BLOCK_STATE_IDS.get(entry.getKey()); + for (final Reference2IntMap.Entry entry : stateIDMap.reference2IntEntrySet()) { + temporaryPalette[entry.getIntValue()] = Block.BLOCK_STATE_IDS.get(entry.getKey()); } cir.setReturnValue(null);