Skip to content

Commit

Permalink
change: optimize EntityTrackingSection.isEmpty() a tiny bit
Browse files Browse the repository at this point in the history
Due to the addition of mixin-extras, this can now be merged with a slight modification, gaining performance without losing mod compatibility
  • Loading branch information
ishland authored and 2No2Name committed Mar 18, 2024
1 parent d91920f commit 84f3999
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package me.jellysquid.mods.lithium.mixin.ai.nearby_entity_tracking;

import com.llamalad7.mixinextras.injector.ModifyReturnValue;
import it.unimi.dsi.fastutil.objects.ReferenceOpenHashSet;
import me.jellysquid.mods.lithium.common.entity.PositionedEntityTrackingSection;
import me.jellysquid.mods.lithium.common.entity.nearby_tracker.NearbyEntityListener;
Expand All @@ -13,6 +14,7 @@
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.ModifyVariable;
Expand All @@ -30,6 +32,7 @@ public abstract class EntityTrackingSectionMixin<T extends EntityLike> implement
@Shadow
public abstract boolean isEmpty();

@Unique
private final ReferenceOpenHashSet<NearbyEntityListener> nearbyEntityListeners = new ReferenceOpenHashSet<>(0);

@Override
Expand All @@ -51,11 +54,12 @@ public void removeListener(SectionedEntityCache<?> sectionedEntityCache, NearbyE
}
}

@Inject(method = "isEmpty()Z", at = @At(value = "HEAD"), cancellable = true)
public void isEmpty(CallbackInfoReturnable<Boolean> cir) {
if (!this.nearbyEntityListeners.isEmpty()) {
cir.setReturnValue(false);
}
/**
* @author ishland, 2No2Name
*/
@ModifyReturnValue(method = "isEmpty()Z", at = @At(value = "RETURN"))
public boolean modifyIsEmpty(boolean previousIsEmpty) {
return previousIsEmpty && this.nearbyEntityListeners.isEmpty();
}

@Inject(method = "add(Lnet/minecraft/world/entity/EntityLike;)V", at = @At("RETURN"))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package me.jellysquid.mods.lithium.mixin.util.entity_movement_tracking;

import com.llamalad7.mixinextras.injector.ModifyReturnValue;
import com.llamalad7.mixinextras.injector.WrapWithCondition;
import it.unimi.dsi.fastutil.objects.ReferenceOpenHashSet;
import me.jellysquid.mods.lithium.common.entity.PositionedEntityTrackingSection;
import me.jellysquid.mods.lithium.common.entity.movement_tracker.EntityMovementTrackerSection;
Expand All @@ -11,6 +13,7 @@
import net.minecraft.world.entity.SectionedEntityCache;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.ModifyVariable;
Expand All @@ -19,16 +22,19 @@
import java.util.ArrayList;

@Mixin(EntityTrackingSection.class)
public abstract class EntityTrackingSectionMixin<T extends EntityLike> implements EntityMovementTrackerSection, PositionedEntityTrackingSection {
public abstract class EntityTrackingSectionMixin implements EntityMovementTrackerSection, PositionedEntityTrackingSection {
@Shadow
private EntityTrackingStatus status;

@Shadow
public abstract boolean isEmpty();

@Unique
private final ReferenceOpenHashSet<SectionedEntityMovementTracker<?, ?>> sectionVisibilityListeners = new ReferenceOpenHashSet<>(0);
@Unique
@SuppressWarnings("unchecked")
private final ArrayList<SectionedEntityMovementTracker<?, ?>>[] entityMovementListenersByType = new ArrayList[MovementTrackerHelper.NUM_MOVEMENT_NOTIFYING_CLASSES];
@Unique
private final long[] lastEntityMovementByType = new long[MovementTrackerHelper.NUM_MOVEMENT_NOTIFYING_CLASSES];

@Override
Expand Down Expand Up @@ -76,11 +82,9 @@ public long getChangeTime(int trackedClass) {
return this.lastEntityMovementByType[trackedClass];
}

@Inject(method = "isEmpty()Z", at = @At(value = "HEAD"), cancellable = true)
public void isEmpty(CallbackInfoReturnable<Boolean> cir) {
if (!this.sectionVisibilityListeners.isEmpty()) {
cir.setReturnValue(false);
}
@ModifyReturnValue(method = "isEmpty()Z", at = @At(value = "RETURN"))
public boolean modifyIsEmpty(boolean previousIsEmpty) {
return previousIsEmpty && this.sectionVisibilityListeners.isEmpty();
}


Expand Down

0 comments on commit 84f3999

Please sign in to comment.