From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: CullanP Date: Thu, 3 Mar 2016 02:13:38 -0600 Subject: [PATCH] Avoid hopper searches if there are no items Hoppers searching for items and minecarts is the most expensive part of hopper ticking. We keep track of the number of minecarts and items in a chunk. If there are no items in the chunk, we skip searching for items. If there are no minecarts in the chunk, we skip searching for them. Usually hoppers aren't near items, so we can skip most item searches. And since minecart hoppers are used _very_ rarely near we can avoid alot of searching there. Combined, this adds up a lot. diff --git a/src/main/java/net/minecraft/world/level/Level.java b/src/main/java/net/minecraft/world/level/Level.java index 04ef0245877ab7e6978149209549151e4f157920..de069f9f7af5ff3e346d68e254ae7138fb19f3f3 100644 --- a/src/main/java/net/minecraft/world/level/Level.java +++ b/src/main/java/net/minecraft/world/level/Level.java @@ -985,7 +985,7 @@ public abstract class Level implements LevelAccessor, AutoCloseable { } } - }); + }, predicate == net.minecraft.world.entity.EntitySelector.CONTAINER_ENTITY_SELECTOR); // Paper return list; } diff --git a/src/main/java/net/minecraft/world/level/entity/EntitySection.java b/src/main/java/net/minecraft/world/level/entity/EntitySection.java index 06649ebed94f2d3e1a076e06e1027e4ee37a8c37..8cf4373486cbb913f9876891ed3bd6f5ee2b405f 100644 --- a/src/main/java/net/minecraft/world/level/entity/EntitySection.java +++ b/src/main/java/net/minecraft/world/level/entity/EntitySection.java @@ -13,6 +13,10 @@ public class EntitySection { protected static final Logger LOGGER = LogManager.getLogger(); private final ClassInstanceMultiMap storage; private Visibility chunkStatus; + // Paper start - track number of items and minecarts + public int itemCount; + public int inventoryEntityCount; + // Paper end public EntitySection(Class entityClass, Visibility status) { this.chunkStatus = status; @@ -20,10 +24,24 @@ public class EntitySection { } public void add(T entityAccess) { + // Paper start + if (entityAccess instanceof net.minecraft.world.entity.item.ItemEntity) { + this.itemCount++; + } else if (entityAccess instanceof net.minecraft.world.Container) { + this.inventoryEntityCount++; + } + // Paper end this.storage.add(entityAccess); } public boolean remove(T entityAccess) { + // Paper start + if (entityAccess instanceof net.minecraft.world.entity.item.ItemEntity) { + this.itemCount--; + } else if (entityAccess instanceof net.minecraft.world.Container) { + this.inventoryEntityCount--; + } + // Paper end return this.storage.remove(entityAccess); } @@ -42,7 +60,7 @@ public class EntitySection { for(T entityAccess : collection) { U entityAccess2 = (U)((EntityAccess)type.tryCast(entityAccess)); if (entityAccess2 != null && entityAccess.getBoundingBox().intersects(aABB)) { - action.accept((T)entityAccess2); + action.accept(entityAccess2); // Paper - decompile fixes } } diff --git a/src/main/java/net/minecraft/world/level/entity/EntitySectionStorage.java b/src/main/java/net/minecraft/world/level/entity/EntitySectionStorage.java index 2e070899554a04c6be650b5b0557b704f972d63b..344e994887e44477c0fb070b5ddcbdb18e6d8e67 100644 --- a/src/main/java/net/minecraft/world/level/entity/EntitySectionStorage.java +++ b/src/main/java/net/minecraft/world/level/entity/EntitySectionStorage.java @@ -111,13 +111,20 @@ public class EntitySectionStorage { } public void getEntities(AABB box, Consumer action) { + // Paper start + this.getEntities(box, action, false); + } + public void getEntities(AABB box, Consumer action, boolean isContainerSearch) { + // Paper end this.forEachAccessibleNonEmptySection(box, (entitySection) -> { + if (isContainerSearch && entitySection.inventoryEntityCount <= 0) return; // Paper entitySection.getEntities(box, action); }); } public void getEntities(EntityTypeTest filter, AABB box, Consumer action) { this.forEachAccessibleNonEmptySection(box, (entitySection) -> { + if (filter.getBaseClass() == net.minecraft.world.entity.item.ItemEntity.class && entitySection.itemCount <= 0) return; // Paper entitySection.getEntities(filter, box, action); }); } diff --git a/src/main/java/net/minecraft/world/level/entity/LevelEntityGetter.java b/src/main/java/net/minecraft/world/level/entity/LevelEntityGetter.java index 9723a0ad61548c8c6c4c5ef20a150d5b17d80afd..da1ad0b2679e392ed81b50c15f012c63cb5c939e 100644 --- a/src/main/java/net/minecraft/world/level/entity/LevelEntityGetter.java +++ b/src/main/java/net/minecraft/world/level/entity/LevelEntityGetter.java @@ -17,6 +17,7 @@ public interface LevelEntityGetter { void get(EntityTypeTest filter, Consumer action); void get(AABB box, Consumer action); + void get(AABB box, Consumer action, boolean isContainerSearch); // Paper void get(EntityTypeTest filter, AABB box, Consumer action); } diff --git a/src/main/java/net/minecraft/world/level/entity/LevelEntityGetterAdapter.java b/src/main/java/net/minecraft/world/level/entity/LevelEntityGetterAdapter.java index d5129c12c79eb6fe6b7e5f8eed4d24226423f5fd..3b13f6ea36a3bfecabe09221eb5c48dddab119db 100644 --- a/src/main/java/net/minecraft/world/level/entity/LevelEntityGetterAdapter.java +++ b/src/main/java/net/minecraft/world/level/entity/LevelEntityGetterAdapter.java @@ -38,7 +38,13 @@ public class LevelEntityGetterAdapter implements LevelEn @Override public void get(AABB box, Consumer action) { - this.sectionStorage.getEntities(box, action); + // Paper start + this.get(box, action, false); + } + @Override + public void get(AABB box, Consumer action, boolean isContainerSearch) { + this.sectionStorage.getEntities(box, action, isContainerSearch); + // Paper end } @Override