Skip to content

Commit

Permalink
New: Cache some objects and arrays in a static final reference to avo…
Browse files Browse the repository at this point in the history
…id the memory overhead in every call.
  • Loading branch information
AbdElAziz333 committed Dec 14, 2023
1 parent 8f867d7 commit 6e87597
Show file tree
Hide file tree
Showing 26 changed files with 606 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.abdelaziz.saturn.common.util.constants;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.mojang.datafixers.util.Pair;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.world.entity.*;
import net.minecraft.world.entity.ai.behavior.*;
import net.minecraft.world.entity.ai.behavior.declarative.BehaviorBuilder;
import net.minecraft.world.entity.ai.memory.MemoryModuleType;
import net.minecraft.world.entity.ai.memory.MemoryStatus;
import net.minecraft.world.entity.animal.camel.Camel;
import net.minecraft.world.entity.animal.camel.CamelAi;
import net.minecraft.world.entity.animal.frog.Frog;
import net.minecraft.world.entity.animal.goat.Goat;
import net.minecraft.world.entity.animal.sniffer.Sniffer;
import net.minecraft.world.entity.animal.sniffer.SnifferAi;
import net.minecraft.world.entity.monster.Zoglin;
import net.minecraft.world.entity.monster.hoglin.Hoglin;
import net.minecraft.world.entity.monster.piglin.Piglin;
import net.minecraft.world.entity.monster.piglin.PiglinAi;
import net.minecraft.world.entity.monster.piglin.PiglinBrute;
import net.minecraft.world.entity.monster.warden.Warden;
import net.minecraft.world.item.ItemCooldowns;

import java.util.function.Predicate;

public interface EntityConstants {
ItemCooldowns ITEM_COOLDOWNS = new ItemCooldowns();
float[] WHITE_SHEEP_COLOR = new float[]{0.9019608F, 0.9019608F, 0.9019608F};

//Fall Sounds
LivingEntity.Fallsounds LIVING_ENTITY = new LivingEntity.Fallsounds(SoundEvents.GENERIC_SMALL_FALL, SoundEvents.GENERIC_BIG_FALL);
LivingEntity.Fallsounds PLAYER = new LivingEntity.Fallsounds(SoundEvents.PLAYER_SMALL_FALL, SoundEvents.PLAYER_BIG_FALL);
LivingEntity.Fallsounds MONSTER = new LivingEntity.Fallsounds(SoundEvents.HOSTILE_SMALL_FALL, SoundEvents.HOSTILE_BIG_FALL);
LivingEntity.Fallsounds ARMOR_STAND = new LivingEntity.Fallsounds(SoundEvents.ARMOR_STAND_FALL, SoundEvents.ARMOR_STAND_FALL);

//RunOne
DoNothing DO_NOTHING = new DoNothing(30, 60);
DoNothing SNIFFER_DO_NOTHING = new DoNothing(5, 20);

RunOne<PathfinderMob> ALLAY = new RunOne<>(ImmutableList.of(Pair.of(RandomStroll.fly(1.0F), 2), Pair.of(SetWalkTargetFromLookTarget.create(1.0F, 3), 2), Pair.of(DO_NOTHING, 1)));
RunOne<Camel> CAMEL = new RunOne<>(ImmutableMap.of(MemoryModuleType.WALK_TARGET, MemoryStatus.VALUE_ABSENT), ImmutableList.of(Pair.of(BehaviorBuilder.triggerIf(Predicate.not(Camel::refuseToMove), RandomStroll.stroll(2.0F)), 1), Pair.of(BehaviorBuilder.triggerIf(Predicate.not(Camel::refuseToMove), SetWalkTargetFromLookTarget.create(2.0F, 3)), 1), Pair.of(new CamelAi.RandomSitting(20), 1), Pair.of(DO_NOTHING, 1)));
RunOne<Frog> FROG = new RunOne<>(ImmutableMap.of(MemoryModuleType.WALK_TARGET, MemoryStatus.VALUE_ABSENT), ImmutableList.of(Pair.of(RandomStroll.stroll(1.0F), 1), Pair.of(SetWalkTargetFromLookTarget.create(1.0F, 3), 1), Pair.of(new Croak(), 3), Pair.of(BehaviorBuilder.triggerIf(Entity::onGround), 2)));
RunOne<Goat> GOAT = new RunOne<>(ImmutableList.of(Pair.of(RandomStroll.stroll(1.0F), 2), Pair.of(SetWalkTargetFromLookTarget.create(1.0F, 3), 2), Pair.of(DO_NOTHING, 1)));
RunOne<Hoglin> HOGLIN = new RunOne<>(ImmutableList.of(Pair.of(RandomStroll.stroll(0.4F), 2), Pair.of(SetWalkTargetFromLookTarget.create(0.4F, 3), 2), Pair.of(DO_NOTHING, 1)));
RunOne<LivingEntity> PIGLIN1 = new RunOne<>(ImmutableList.<Pair<? extends BehaviorControl<? super LivingEntity>, Integer>>builder().addAll(PiglinAi.createLookBehaviors()).add(Pair.of(DO_NOTHING, 1)).build());
RunOne<Piglin> PIGLIN2 = new RunOne<>(ImmutableList.of(Pair.of(RandomStroll.stroll(0.6F), 2), Pair.of(InteractWith.of(EntityType.PIGLIN, 8, MemoryModuleType.INTERACTION_TARGET, 0.6F, 2), 2), Pair.of(BehaviorBuilder.triggerIf(PiglinAi::doesntSeeAnyPlayerHoldingLovedItem, SetWalkTargetFromLookTarget.create(0.6F, 3)), 2), Pair.of(DO_NOTHING, 1)));
RunOne<PiglinBrute> PIGLIN_BRUTE1 = new RunOne<>(ImmutableList.of(Pair.of(SetEntityLookTarget.create(EntityType.PLAYER, 8.0F), 1), Pair.of(SetEntityLookTarget.create(EntityType.PIGLIN, 8.0F), 1), Pair.of(SetEntityLookTarget.create(EntityType.PIGLIN_BRUTE, 8.0F), 1), Pair.of(SetEntityLookTarget.create(8.0F), 1), Pair.of(DO_NOTHING, 1)));
RunOne<PiglinBrute> PIGLIN_BRUTE2 = new RunOne<>(ImmutableList.of(Pair.of(RandomStroll.stroll(0.6F), 2), Pair.of(InteractWith.of(EntityType.PIGLIN, 8, MemoryModuleType.INTERACTION_TARGET, 0.6F, 2), 2), Pair.of(InteractWith.of(EntityType.PIGLIN_BRUTE, 8, MemoryModuleType.INTERACTION_TARGET, 0.6F, 2), 2), Pair.of(StrollToPoi.create(MemoryModuleType.HOME, 0.6F, 2, 100), 2), Pair.of(StrollAroundPoi.create(MemoryModuleType.HOME, 0.6F, 5), 2), Pair.of(DO_NOTHING, 1)));
RunOne<Sniffer> SNIFFER = new RunOne<>(ImmutableList.of(Pair.of(SetWalkTargetFromLookTarget.create(1.0F, 3), 2), Pair.of(new SnifferAi.Scenting(40, 80), 1), Pair.of(new SnifferAi.Sniffing(40, 80), 1), Pair.of(SetEntityLookTarget.create(EntityType.PLAYER, 6.0F), 1), Pair.of(RandomStroll.stroll(1.0F), 1), Pair.of(SNIFFER_DO_NOTHING, 2)));
RunOne<LivingEntity> VILLAGER1 = new RunOne<>(ImmutableList.of(Pair.of(SetEntityLookTarget.create(EntityType.VILLAGER, 8.0F), 2), Pair.of(SetEntityLookTarget.create(EntityType.PLAYER, 8.0F), 2), Pair.of(DO_NOTHING, 8)));
RunOne<LivingEntity> VILLAGER2 = new RunOne<>(ImmutableList.of(Pair.of(SetEntityLookTarget.create(EntityType.CAT, 8.0F), 8), Pair.of(SetEntityLookTarget.create(EntityType.VILLAGER, 8.0F), 2), Pair.of(SetEntityLookTarget.create(EntityType.PLAYER, 8.0F), 2), Pair.of(SetEntityLookTarget.create(MobCategory.CREATURE, 8.0F), 1), Pair.of(SetEntityLookTarget.create(MobCategory.WATER_CREATURE, 8.0F), 1), Pair.of(SetEntityLookTarget.create(MobCategory.AXOLOTLS, 8.0F), 1), Pair.of(SetEntityLookTarget.create(MobCategory.UNDERGROUND_WATER_CREATURE, 8.0F), 1), Pair.of(SetEntityLookTarget.create(MobCategory.WATER_AMBIENT, 8.0F), 1), Pair.of(SetEntityLookTarget.create(MobCategory.MONSTER, 8.0F), 1), Pair.of(DO_NOTHING, 2)));
RunOne<Warden> WARDEN = new RunOne<>(ImmutableMap.of(MemoryModuleType.IS_SNIFFING, MemoryStatus.VALUE_ABSENT), ImmutableList.of(Pair.of(RandomStroll.stroll(0.5F), 2), Pair.of(DO_NOTHING, 1)));
RunOne<Zoglin> ZOGLIN = new RunOne<>(ImmutableList.of(Pair.of(RandomStroll.stroll(0.4F), 2), Pair.of(SetWalkTargetFromLookTarget.create(0.4F, 3), 2), Pair.of(DO_NOTHING, 1)));
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.abdelaziz.saturn.common.util.constants;

import net.minecraft.world.phys.Vec3;

import java.io.File;

public interface SaturnConstants {
File SERVER_DIRECTORY = new File(".");
int[] ZERO = new int[0];
int[] MULTI_ZERO = new int[]{0};
Vec3 EMPTY_VECTOR3 = new Vec3(0.0D, 0.0D, 1.0D);
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public boolean shouldApplyMixin(String targetClassName, String mixinClassName) {
return false;
}

if ((mixinClassName.startsWith(MIXIN_PATH + "miscellaneous.threading_detector") || mixinClassName.startsWith(MIXIN_PATH + "leaks.biome_temperature_cache")) &&
if ((mixinClassName.startsWith(MIXIN_PATH + "miscellaneous.threading_detector") || mixinClassName.startsWith(MIXIN_PATH + "leaks.biome_temperature_cache") || mixinClassName.startsWith(MIXIN_PATH + "allocations.composter")) &&
(FMLLoader.getLoadingModList().getModFileById("canary") != null || FMLLoader.getLoadingModList().getModFileById("radium") != null)) {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.abdelaziz.saturn.mixin.allocations.composter;

import com.abdelaziz.saturn.common.util.constants.SaturnConstants;
import net.minecraft.core.Direction;
import net.minecraft.world.level.block.ComposterBlock;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Overwrite;

public class ComposterBlockMixin {
@Mixin(ComposterBlock.EmptyContainer.class)
static class EmptyContainerMixin {
/**
* @reason Cache to a static final reference.
* @author AbdElAziz
* */
@Overwrite
public int[] getSlotsForFace(Direction direction) {
return SaturnConstants.ZERO;
}
}

@Mixin(ComposterBlock.InputContainer.class)
static class InputContainerMixin {
/**
* @reason Cache to a static final reference.
* @author AbdElAziz
* */
@Overwrite
public int[] getSlotsForFace(Direction direction) {
return direction == Direction.UP ? SaturnConstants.MULTI_ZERO : SaturnConstants.ZERO;
}
}

@Mixin(ComposterBlock.OutputContainer.class)
static class OutputContainerMixin {
/**
* @reason Cache to a static final reference.
* @author AbdElAziz
* */
@Overwrite
public int[] getSlotsForFace(Direction direction) {
return direction == Direction.DOWN ? SaturnConstants.MULTI_ZERO : SaturnConstants.ZERO;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.abdelaziz.saturn.mixin.allocations.entity.animal;

import com.abdelaziz.saturn.common.util.constants.SaturnConstants;
import net.minecraft.world.entity.animal.Pig;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.phys.Vec3;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Overwrite;

@Mixin(Pig.class)
public class PigMixin {
/**
* @reason Cache to a static final reference.
* @author AbdElAziz
* */
@Overwrite
protected Vec3 getRiddenInput(Player player, Vec3 vec3) {
return SaturnConstants.EMPTY_VECTOR3;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.abdelaziz.saturn.mixin.allocations.entity.animal;

import com.abdelaziz.saturn.common.util.constants.EntityConstants;
import net.minecraft.world.entity.animal.Sheep;
import net.minecraft.world.item.DyeColor;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Overwrite;

@Mixin(Sheep.class)
public class SheepMixin {
/**
* @reason Cache to a static final reference.
* @author AbdElAziz
* */
@Overwrite
private static float[] createSheepColor(DyeColor color) {
if (color == DyeColor.WHITE) {
return EntityConstants.WHITE_SHEEP_COLOR;
} else {
float[] afloat = color.getTextureDiffuseColors();
return new float[]{afloat[0] * 0.75F, afloat[1] * 0.75F, afloat[2] * 0.75F};
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.abdelaziz.saturn.mixin.allocations.entity.animal;

import com.abdelaziz.saturn.common.util.constants.SaturnConstants;
import net.minecraft.world.entity.monster.Strider;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.phys.Vec3;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Overwrite;

@Mixin(Strider.class)
public class StriderMixin {
/**
* @reason Cache to a static final reference.
* @author AbdElAziz
* */
@Overwrite
protected Vec3 getRiddenInput(Player player, Vec3 vec3) {
return SaturnConstants.EMPTY_VECTOR3;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.abdelaziz.saturn.mixin.allocations.entity.fall_sounds;

import com.abdelaziz.saturn.common.util.constants.EntityConstants;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.decoration.ArmorStand;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Overwrite;

@Mixin(ArmorStand.class)
public class ArmorStandMixin {
/**
* @reason Cache to a static final reference.
* @author AbdElAziz
* */
@Overwrite
public LivingEntity.Fallsounds getFallSounds() {
return EntityConstants.ARMOR_STAND;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.abdelaziz.saturn.mixin.allocations.entity.fall_sounds;

import com.abdelaziz.saturn.common.util.constants.EntityConstants;
import net.minecraft.world.entity.LivingEntity;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Overwrite;

@Mixin(LivingEntity.class)
public class LivingEntityMixin {
/**
* @reason Cache to a static final reference.
* @author AbdElAziz
* */
@Overwrite
public LivingEntity.Fallsounds getFallSounds() {
return EntityConstants.LIVING_ENTITY;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.abdelaziz.saturn.mixin.allocations.entity.fall_sounds;

import com.abdelaziz.saturn.common.util.constants.EntityConstants;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.monster.Monster;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Overwrite;

@Mixin(Monster.class)
public class MonsterMixin {
/**
* @reason Cache to a static final reference.
* @author AbdElAziz
* */
@Overwrite
public LivingEntity.Fallsounds getFallSounds() {
return EntityConstants.MONSTER;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.abdelaziz.saturn.mixin.allocations.entity.fall_sounds;

import com.abdelaziz.saturn.common.util.constants.EntityConstants;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.player.Player;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Overwrite;

@Mixin(Player.class)
public class PlayerMixin {
/**
* @reason Cache to a static final reference.
* @author AbdElAziz
* */
@Overwrite
public LivingEntity.Fallsounds getFallSounds() {
return EntityConstants.PLAYER;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.abdelaziz.saturn.mixin.allocations.entity.item_cooldowns;

import com.abdelaziz.saturn.common.util.constants.EntityConstants;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemCooldowns;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Overwrite;

@Mixin(Player.class)
public class PlayerMixin {
/**
* @reason Cache to a static final reference.
* @author AbdElAziz
* */
@Overwrite
protected ItemCooldowns createItemCooldowns() {
return EntityConstants.ITEM_COOLDOWNS;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.abdelaziz.saturn.mixin.allocations.entity.run_one;

import com.abdelaziz.saturn.common.util.constants.EntityConstants;
import net.minecraft.world.entity.PathfinderMob;
import net.minecraft.world.entity.ai.behavior.RunOne;
import net.minecraft.world.entity.animal.allay.AllayAi;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Redirect;

import java.util.List;

@Mixin(AllayAi.class)
public class AllayAiMixin {
@Redirect(
method = "initIdleActivity",
at = @At(
value = "NEW",
target = "(Ljava/util/List;)Lnet/minecraft/world/entity/ai/behavior/RunOne;"
)
)
private static RunOne<PathfinderMob> useStaticFinalReference(List<?> list) {
return EntityConstants.ALLAY;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.abdelaziz.saturn.mixin.allocations.entity.run_one;

import com.abdelaziz.saturn.common.util.constants.EntityConstants;
import net.minecraft.world.entity.ai.behavior.RunOne;
import net.minecraft.world.entity.animal.camel.CamelAi;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Redirect;

import java.util.List;
import java.util.Map;

@Mixin(CamelAi.class)
public class CamelAiMixin {
@Redirect(
method = "initIdleActivity",
at = @At(
value = "NEW",
target = "(Ljava/util/Map;Ljava/util/List;)Lnet/minecraft/world/entity/ai/behavior/RunOne;"
)
)
private static RunOne<?> useStaticFinalReference(Map<?, ?> map, List<?> list) {
return EntityConstants.CAMEL;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.abdelaziz.saturn.mixin.allocations.entity.run_one;

import com.abdelaziz.saturn.common.util.constants.EntityConstants;
import net.minecraft.world.entity.ai.behavior.RunOne;
import net.minecraft.world.entity.animal.frog.FrogAi;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Redirect;

import java.util.List;
import java.util.Map;

@Mixin(FrogAi.class)
public class FrogAiMixin {
@Redirect(
method = "initIdleActivity",
at = @At(
value = "NEW",
target = "(Ljava/util/Map;Ljava/util/List;)Lnet/minecraft/world/entity/ai/behavior/RunOne;"
)
)
private static RunOne<?> useStaticFinalReference(Map<?, ?> map, List<?> list) {
return EntityConstants.FROG;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.abdelaziz.saturn.mixin.allocations.entity.run_one;

import com.abdelaziz.saturn.common.util.constants.EntityConstants;
import net.minecraft.world.entity.ai.behavior.RunOne;
import net.minecraft.world.entity.animal.goat.GoatAi;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Redirect;

import java.util.List;

@Mixin(GoatAi.class)
public class GoatAiMixin {
@Redirect(
method = "initIdleActivity",
at = @At(
value = "NEW",
target = "(Ljava/util/List;)Lnet/minecraft/world/entity/ai/behavior/RunOne;"
)
)
private static RunOne<?> useStaticFinalReference(List<?> list) {
return EntityConstants.GOAT;
}
}

0 comments on commit 6e87597

Please sign in to comment.