Skip to content

Commit

Permalink
Feat: Use FastUtil to improve performance.
Browse files Browse the repository at this point in the history
  • Loading branch information
KasumiNova committed Sep 30, 2024
1 parent bafa2d7 commit edf9473
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 18 deletions.
19 changes: 10 additions & 9 deletions src/main/java/org/dimdev/jeid/mixin/core/misc/MixinStatList.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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).
Expand Down Expand Up @@ -63,17 +64,17 @@ public final class MixinStatList {
@Shadow
private static StatBase[] OBJECTS_DROPPED_STATS;
@Unique
private static final Map<Block, StatBase> BLOCKS_STATS_MAP = new HashMap<>();
private static final Map<Block, StatBase> BLOCKS_STATS_MAP = new Reference2ObjectOpenHashMap<>();
@Unique
private static final Map<Item, StatBase> CRAFTS_STATS_MAP = new HashMap<>();
private static final Map<Item, StatBase> CRAFTS_STATS_MAP = new Reference2ObjectOpenHashMap<>();
@Unique
private static final Map<Item, StatBase> OBJECT_USE_STATS_MAP = new HashMap<>();
private static final Map<Item, StatBase> OBJECT_USE_STATS_MAP = new Reference2ObjectOpenHashMap<>();
@Unique
private static final Map<Item, StatBase> OBJECT_BREAK_STATS_MAP = new HashMap<>();
private static final Map<Item, StatBase> OBJECT_BREAK_STATS_MAP = new Reference2ObjectOpenHashMap<>();
@Unique
private static final Map<Item, StatBase> OBJECTS_PICKED_UP_STATS_MAP = new HashMap<>();
private static final Map<Item, StatBase> OBJECTS_PICKED_UP_STATS_MAP = new Reference2ObjectOpenHashMap<>();
@Unique
private static final Map<Item, StatBase> OBJECTS_DROPPED_STATS_MAP = new HashMap<>();
private static final Map<Item, StatBase> OBJECTS_DROPPED_STATS_MAP = new Reference2ObjectOpenHashMap<>();

/**
* @reason Reduce memory footprint of stat arrays
Expand Down Expand Up @@ -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<StatBase> knownStats = new HashSet<>((int) Math.ceil(requiredCapacity / 0.75f));
Set<StatBase> knownStats = new ObjectOpenHashSet<>(requiredCapacity + 1);
knownStats.addAll(BLOCKS_STATS_MAP.values());
knownStats.addAll(CRAFTS_STATS_MAP.values());
knownStats.addAll(OBJECT_USE_STATS_MAP.values());
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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<NibbleArray> cir) {
HashMap<IBlockState, Integer> stateIDMap = new HashMap<>();
Reference2IntMap<IBlockState> 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);
}

Expand All @@ -72,8 +72,8 @@ public void setLegacyAdd2(NibbleArray add2) {
}

temporaryPalette = new int[nextID];
for (Map.Entry<IBlockState, Integer> entry : stateIDMap.entrySet()) {
temporaryPalette[entry.getValue()] = Block.BLOCK_STATE_IDS.get(entry.getKey());
for (final Reference2IntMap.Entry<IBlockState> entry : stateIDMap.reference2IntEntrySet()) {
temporaryPalette[entry.getIntValue()] = Block.BLOCK_STATE_IDS.get(entry.getKey());
}

cir.setReturnValue(null);
Expand Down

0 comments on commit edf9473

Please sign in to comment.