Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Memory optimization #432

Merged
merged 2 commits into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,10 @@ public class FixesConfig {
@Config.DefaultBoolean(true)
public static boolean removeUpdateChecks;

@Config.Comment("Enable multiple fixes to reduce RAM usage")
@Config.DefaultBoolean(true)
public static boolean enableMemoryFixes;

// BetterHUD

@Config.Comment("Maximum hp for BetterHUD to render as hearts")
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/com/mitchej123/hodgepodge/mixins/Mixins.java
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,14 @@ public enum Mixins {
.setPhase(Phase.EARLY).setSide(Side.CLIENT).addTargetedMod(TargetedMod.VANILLA)
.addMixinClasses("minecraft.MixinGuiChat_OpenLinks").setApplyIf(() -> FixesConfig.fixChatOpenLink)),

MEMORY_FIXES_CLIENT(
new Builder("Memory fixes").setPhase(Phase.EARLY).setSide(Side.CLIENT).addTargetedMod(TargetedMod.VANILLA)
.addMixinClasses("memory.MixinFMLClientHandler").setApplyIf(() -> FixesConfig.enableMemoryFixes)),

MEMORY_FIXES_IC2(new Builder("Removes allocation spam from the Direction.applyTo method").setPhase(Phase.LATE)
.setSide(Side.BOTH).addMixinClasses("ic2.MixinDirection_Memory")
.setApplyIf(() -> FixesConfig.enableMemoryFixes).addTargetedMod(TargetedMod.IC2)),

// Ic2 adjustments
IC2_UNPROTECTED_GET_BLOCK_FIX(new Builder("IC2 Kinetic Fix").setPhase(Phase.EARLY).setSide(Side.BOTH)
.addMixinClasses("ic2.MixinIc2WaterKinetic").setApplyIf(() -> FixesConfig.fixIc2UnprotectedGetBlock)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.mitchej123.hodgepodge.mixins.early.memory;

import java.util.Set;

import net.minecraft.util.ResourceLocation;

import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

import com.google.common.collect.HashBasedTable;
import com.google.common.collect.HashMultimap;
import com.google.common.collect.SetMultimap;
import com.google.common.collect.Sets;
import com.google.common.collect.Table;

import cpw.mods.fml.client.FMLClientHandler;

@Mixin(value = FMLClientHandler.class, remap = false)
public class MixinFMLClientHandler {

@Shadow
private SetMultimap<String, ResourceLocation> missingTextures;
@Shadow
private Set<String> badTextureDomains;
@Shadow
private Table<String, String, Set<ResourceLocation>> brokenTextures;

@Inject(method = "logMissingTextureErrors", at = @At("TAIL"))
private void hodgepodge$freeMemory(CallbackInfo ci) {
missingTextures = HashMultimap.create();
badTextureDomains = Sets.newHashSet();
brokenTextures = HashBasedTable.create();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.mitchej123.hodgepodge.mixins.late.ic2;

import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.World;

import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Overwrite;
import org.spongepowered.asm.mixin.Unique;

import ic2.api.Direction;

@Mixin(value = Direction.class, remap = false)
public abstract class MixinDirection_Memory {

/**
* @author Alexdoru
* @reason Reduce allocation spam
*/
@Overwrite
public TileEntity applyTo(World world, int x, int y, int z) {
if (world == null) return null;
return switch ((Direction) (Object) this) {
case XN -> hodgepodge$getTE(world, x - 1, y, z);
case XP -> hodgepodge$getTE(world, x + 1, y, z);
case YN -> hodgepodge$getTE(world, x, y - 1, z);
case YP -> hodgepodge$getTE(world, x, y + 1, z);
case ZN -> hodgepodge$getTE(world, x, y, z - 1);
case ZP -> hodgepodge$getTE(world, x, y, z + 1);
};
}

@Unique
private TileEntity hodgepodge$getTE(World world, int x, int y, int z) {
if (world.blockExists(x, y, z)) {
return world.getTileEntity(x, y, z);
} else {
return null;
}
}

}