Max collisions optimization (#1038)

* Lift entity limit up to the `getOtherEntities` method so it bails early

This has the potential to prevent a lot of bounding box computation

* Clean up decompiled code

* Fix raw generic types

This doesn't alter the signature, but it helps the compiler warn about misuse of the type later on

* Actually abort forEach when `limit` entities have been checked

`throw`ing a known exception is the easiest way to `break` out of a forEach call
This commit is contained in:
Mitchell Skaggs authored and GitHub committed 2021-08-03 07:20:09 -03:00
1 parent 56a15fb384
commit 28cb136c6b
6 files changed
+96 -28

No files matched your search

@@ -5,12 +5,18 @@ import net.minecraft.block.entity.BlockEntity;
import net.minecraft.entity.Entity; import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityType; import net.minecraft.entity.EntityType;
import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Box;
import org.jetbrains.annotations.Nullable;
import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.function.Predicate;
public interface WorldInterface public interface WorldInterface
{ {
Map<EntityType, Entity> getPrecookedMobs(); Map<EntityType<?>, Entity> getPrecookedMobs();
boolean setBlockStateWithBlockEntity(BlockPos blockPos, BlockState blockState, BlockEntity newBlockEntity, int int1); boolean setBlockStateWithBlockEntity(BlockPos blockPos, BlockState blockState, BlockEntity newBlockEntity, int int1);
List<Entity> getOtherEntitiesLimited(@Nullable Entity except, Box box, Predicate<? super Entity> predicate, int limit);
} }
@@ -1,6 +1,7 @@
package carpet.mixins; package carpet.mixins;
import carpet.CarpetSettings; import carpet.CarpetSettings;
import carpet.fakes.WorldInterface;
import net.minecraft.entity.Entity; import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityType; import net.minecraft.entity.EntityType;
import net.minecraft.entity.LivingEntity; import net.minecraft.entity.LivingEntity;
@@ -28,33 +29,35 @@ public abstract class LivingEntity_maxCollisionsMixin extends Entity
@Shadow protected abstract void pushAway(Entity entity_1); @Shadow protected abstract void pushAway(Entity entity_1);
@Inject(method = "tickCramming", cancellable = true, at = @At("HEAD")) @Inject(method = "tickCramming", cancellable = true, at = @At("HEAD"))
private void tickPushingReplacement(CallbackInfo ci) private void tickPushingReplacement(CallbackInfo ci) {
{ List<Entity> entities;
List<Entity> list_1 = this.world.getOtherEntities(this, this.getBoundingBox(), EntityPredicates.canBePushedBy(this)); if (CarpetSettings.maxEntityCollisions > 0)
if (!list_1.isEmpty()) { entities = ((WorldInterface) this.world).getOtherEntitiesLimited(
int int_1 = this.world.getGameRules().getInt(GameRules.MAX_ENTITY_CRAMMING); this,
int int_2; this.getBoundingBox(),
if (int_1 > 0 && list_1.size() > int_1 - 1 && this.random.nextInt(4) == 0) { EntityPredicates.canBePushedBy(this),
int_2 = 0; CarpetSettings.maxEntityCollisions);
else
entities = this.world.getOtherEntities(this, this.getBoundingBox(), EntityPredicates.canBePushedBy(this));
for(int int_3 = 0; int_3 < list_1.size(); ++int_3) { if (!entities.isEmpty()) {
if (!((Entity)list_1.get(int_3)).hasVehicle()) { int maxEntityCramming = this.world.getGameRules().getInt(GameRules.MAX_ENTITY_CRAMMING);
++int_2; if (maxEntityCramming > 0 && entities.size() > maxEntityCramming - 1 && this.random.nextInt(4) == 0) {
int candidates = 0;
for (Entity entity : entities) {
if (!entity.hasVehicle()) {
++candidates;
} }
} }
if (int_2 > int_1 - 1) { if (candidates > maxEntityCramming - 1) {
this.damage(DamageSource.CRAMMING, 6.0F); this.damage(DamageSource.CRAMMING, 6.0F);
} }
} }
int limit = list_1.size(); for (Entity entity : entities) {
if (CarpetSettings.maxEntityCollisions > 0) this.pushAway(entity);
limit = Math.min(limit, CarpetSettings.maxEntityCollisions);
for(int_2 = 0; int_2 < limit; ++int_2) {
Entity entity_1 = (Entity)list_1.get(int_2);
this.pushAway(entity_1);
} }
} }
ci.cancel(); ci.cancel();
@@ -138,7 +138,7 @@ public class SpawnHelperMixin
{ {
if (CarpetSettings.lagFreeSpawning) if (CarpetSettings.lagFreeSpawning)
{ {
Map<EntityType, Entity> precookedMobs = ((WorldInterface)world_1).getPrecookedMobs(); Map<EntityType<?>, Entity> precookedMobs = ((WorldInterface)world_1).getPrecookedMobs();
if (precookedMobs.containsKey(entityType)) if (precookedMobs.containsKey(entityType))
//this mob has been <init>'s but not used yet //this mob has been <init>'s but not used yet
return precookedMobs.get(entityType); return precookedMobs.get(entityType);
@@ -0,0 +1,63 @@
package carpet.mixins;
import carpet.fakes.WorldInterface;
import com.google.common.collect.Lists;
import net.minecraft.entity.Entity;
import net.minecraft.entity.boss.dragon.EnderDragonEntity;
import net.minecraft.entity.boss.dragon.EnderDragonPart;
import net.minecraft.util.math.Box;
import net.minecraft.util.profiler.Profiler;
import net.minecraft.world.World;
import net.minecraft.world.entity.EntityLookup;
import org.jetbrains.annotations.Nullable;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Predicate;
@Mixin(World.class)
public abstract class World_getOtherEntitiesLimited implements WorldInterface {
private static final RuntimeException CONTROL_FLOW_EXCEPTION = new RuntimeException("Should be caught for control flow in World_getOtherEntitiesLimited!");
@Override
public List<Entity> getOtherEntitiesLimited(@Nullable Entity except, Box box, Predicate<? super Entity> predicate, int limit) {
this.getProfiler().visit("getEntities");
AtomicInteger checkedEntities = new AtomicInteger();
List<Entity> list = Lists.newArrayList();
try {
this.getEntityLookup().forEachIntersects(box, (entity) -> {
if (checkedEntities.getAndIncrement() > limit) {
throw CONTROL_FLOW_EXCEPTION;
}
if (entity != except && predicate.test(entity)) {
list.add(entity);
}
if (entity instanceof EnderDragonEntity) {
EnderDragonPart[] var4 = ((EnderDragonEntity) entity).getBodyParts();
for (EnderDragonPart enderDragonPart : var4) {
if (entity != except && predicate.test(enderDragonPart)) {
list.add(enderDragonPart);
}
}
}
});
} catch (RuntimeException e) {
if (e != CONTROL_FLOW_EXCEPTION)
// If it wasn't the exception we were watching, rethrow it
throw e;
}
return list;
}
@Shadow
public abstract Profiler getProfiler();
@Shadow
protected abstract EntityLookup<Entity> getEntityLookup();
}
@@ -3,23 +3,18 @@ package carpet.mixins;
import carpet.fakes.WorldInterface; import carpet.fakes.WorldInterface;
import carpet.helpers.TickSpeed; import carpet.helpers.TickSpeed;
import carpet.utils.CarpetProfiler; import carpet.utils.CarpetProfiler;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.entity.Entity; import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityType; import net.minecraft.entity.EntityType;
import net.minecraft.entity.player.PlayerEntity; import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.util.profiler.Profiler;
import net.minecraft.world.World; import net.minecraft.world.World;
import org.spongepowered.asm.mixin.Final; import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow; import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.Redirect;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.asm.mixin.injection.callback.LocalCapture;
import java.util.HashMap; import java.util.HashMap;
import java.util.Iterator;
import java.util.Map; import java.util.Map;
import java.util.function.Consumer; import java.util.function.Consumer;
@@ -30,9 +25,9 @@ public abstract class World_tickMixin implements WorldInterface
CarpetProfiler.ProfilerToken currentSection; CarpetProfiler.ProfilerToken currentSection;
CarpetProfiler.ProfilerToken entitySection; CarpetProfiler.ProfilerToken entitySection;
Map<EntityType, Entity> precookedMobs = new HashMap<>(); Map<EntityType<?>, Entity> precookedMobs = new HashMap<>();
public Map<EntityType, Entity> getPrecookedMobs() public Map<EntityType<?>, Entity> getPrecookedMobs()
{ {
return precookedMobs; return precookedMobs;
} }
+1
View File
@@ -86,6 +86,7 @@
"PistonHandler_pushLimitMixin", "PistonHandler_pushLimitMixin",
"PoweredRailBlock_powerLimitMixin", "PoweredRailBlock_powerLimitMixin",
"LivingEntity_maxCollisionsMixin", "LivingEntity_maxCollisionsMixin",
"World_getOtherEntitiesLimited",
"CoralBlockMixin", "CoralBlockMixin",
"CoralFanBlockMixin", "CoralFanBlockMixin",
"CoralFeatureMixin", "CoralFeatureMixin",