From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: peaches94 Date: Sun, 10 Jul 2022 13:29:20 -0500 Subject: [PATCH] Petal: reduce work done by game event system Original license: GPL v3 Original project: https://github.com/Bloom-host/Petal 1. going into game event dispatching can be expensive so run the checks before dispatching 2. EuclideanGameEventListenerRegistry is not used concurrently so we ban that usage for improved performance with allays diff --git a/src/main/java/net/minecraft/world/level/block/entity/SculkCatalystBlockEntity.java b/src/main/java/net/minecraft/world/level/block/entity/SculkCatalystBlockEntity.java index a74732902c0494c67e6acf2fc04581ff9c46b832..8b60c922750e71f526976782a1f156e94bc0aa35 100644 --- a/src/main/java/net/minecraft/world/level/block/entity/SculkCatalystBlockEntity.java +++ b/src/main/java/net/minecraft/world/level/block/entity/SculkCatalystBlockEntity.java @@ -68,7 +68,7 @@ public class SculkCatalystBlockEntity extends BlockEntity implements GameEventLi return this.catalystListener; } - public static class CatalystListener implements GameEventListener { + public class CatalystListener implements GameEventListener { // Leaf - petal public static final int PULSE_TICKS = 8; final SculkSpreader sculkSpreader; @@ -139,6 +139,13 @@ public class SculkCatalystBlockEntity extends BlockEntity implements GameEventLi world.playSound((Player) null, pos, SoundEvents.SCULK_CATALYST_BLOOM, SoundSource.BLOCKS, 2.0F, 0.6F + random.nextFloat() * 0.4F); } + // Leaf start - petal + @Override + public boolean listensToEvent(GameEvent gameEvent, GameEvent.Context context) { + return !SculkCatalystBlockEntity.this.isRemoved() && gameEvent == GameEvent.ENTITY_DIE.value() && context.sourceEntity() instanceof LivingEntity; + } + // Leaf end - petal + private void tryAwardItSpreadsAdvancement(Level world, LivingEntity deadEntity) { LivingEntity entityliving1 = deadEntity.getLastHurtByMob(); diff --git a/src/main/java/net/minecraft/world/level/chunk/LevelChunk.java b/src/main/java/net/minecraft/world/level/chunk/LevelChunk.java index 070e7b5db06bd670f97ac7f25767be1d33c5d02e..4e3da128942262a6b0230c0ffaad191112e9ec25 100644 --- a/src/main/java/net/minecraft/world/level/chunk/LevelChunk.java +++ b/src/main/java/net/minecraft/world/level/chunk/LevelChunk.java @@ -81,7 +81,18 @@ public class LevelChunk extends ChunkAccess implements ca.spottedleaf.moonrise.p private Supplier fullStatus; @Nullable private LevelChunk.PostLoadProcessor postLoad; - private final Int2ObjectMap gameEventListenerRegistrySections; + // Leaf start - petal + private final GameEventListenerRegistry[] gameEventListenerRegistrySections; + private static final int GAME_EVENT_DISPATCHER_RADIUS = 2; + + private static int getGameEventSectionIndex(int sectionIndex) { + return sectionIndex + GAME_EVENT_DISPATCHER_RADIUS; + } + + private static int getGameEventSectionLength(int sectionCount) { + return sectionCount + (GAME_EVENT_DISPATCHER_RADIUS * 2); + } + // Leaf end - petal private final LevelChunkTicks blockTicks; private final LevelChunkTicks fluidTicks; @@ -105,7 +116,7 @@ public class LevelChunk extends ChunkAccess implements ca.spottedleaf.moonrise.p super(pos, upgradeData, world, net.minecraft.server.MinecraftServer.getServer().registryAccess().registryOrThrow(Registries.BIOME), inhabitedTime, sectionArrayInitializer, blendingData); // Paper - Anti-Xray - The world isn't ready yet, use server singleton for registry this.tickersInLevel = Maps.newHashMap(); this.level = (ServerLevel) world; // CraftBukkit - type - this.gameEventListenerRegistrySections = new Int2ObjectOpenHashMap(); + this.gameEventListenerRegistrySections = new GameEventListenerRegistry[getGameEventSectionLength(this.getSectionsCount())]; // Leaf - petal Heightmap.Types[] aheightmap_type = Heightmap.Types.values(); int j = aheightmap_type.length; @@ -244,9 +255,23 @@ public class LevelChunk extends ChunkAccess implements ca.spottedleaf.moonrise.p Level world = this.level; if (world instanceof ServerLevel worldserver) { - return (GameEventListenerRegistry) this.gameEventListenerRegistrySections.computeIfAbsent(ySectionCoord, (j) -> { - return new EuclideanGameEventListenerRegistry(worldserver, ySectionCoord, this::removeGameEventListenerRegistry); - }); + // Leaf start - petal + int sectionIndex = getGameEventSectionIndex(this.getSectionIndexFromSectionY(ySectionCoord)); + + // drop game events that are too far away (32 blocks) from loaded sections + // this matches the highest radius of game events in the game + if (sectionIndex < 0 || sectionIndex >= this.gameEventListenerRegistrySections.length) { + return GameEventListenerRegistry.NOOP; + } + + var dispatcher = this.gameEventListenerRegistrySections[sectionIndex]; + + if (dispatcher == null) { + dispatcher = this.gameEventListenerRegistrySections[sectionIndex] = new EuclideanGameEventListenerRegistry(worldserver, ySectionCoord, this::removeGameEventListenerRegistry); + } + + return dispatcher; + // Leaf end - petal } else { return super.getListenerRegistry(ySectionCoord); } @@ -630,7 +655,7 @@ public class LevelChunk extends ChunkAccess implements ca.spottedleaf.moonrise.p } private void removeGameEventListenerRegistry(int ySectionCoord) { - this.gameEventListenerRegistrySections.remove(ySectionCoord); + this.gameEventListenerRegistrySections[getGameEventSectionIndex(this.getSectionIndexFromSectionY(ySectionCoord))] = null; // Leaf - petal } private void removeBlockEntityTicker(BlockPos pos) { diff --git a/src/main/java/net/minecraft/world/level/gameevent/EuclideanGameEventListenerRegistry.java b/src/main/java/net/minecraft/world/level/gameevent/EuclideanGameEventListenerRegistry.java index 1e93f3a1b11196a431a1f5a0957036fe0c9191a4..f1f0dbb20d4ee2f3f47e202f2cb3dd4bde9ac0b3 100644 --- a/src/main/java/net/minecraft/world/level/gameevent/EuclideanGameEventListenerRegistry.java +++ b/src/main/java/net/minecraft/world/level/gameevent/EuclideanGameEventListenerRegistry.java @@ -14,8 +14,8 @@ import net.minecraft.world.phys.Vec3; public class EuclideanGameEventListenerRegistry implements GameEventListenerRegistry { private final List listeners = Lists.newArrayList(); - private final Set listenersToRemove = Sets.newHashSet(); - private final List listenersToAdd = Lists.newArrayList(); + //private final Set listenersToRemove = Sets.newHashSet(); // Leaf - petal - Not necessary + //private final List listenersToAdd = Lists.newArrayList(); // Leaf - petal private boolean processing; private final ServerLevel level; private final int sectionY; @@ -35,7 +35,7 @@ public class EuclideanGameEventListenerRegistry implements GameEventListenerRegi @Override public void register(GameEventListener listener) { if (this.processing) { - this.listenersToAdd.add(listener); + throw new java.util.ConcurrentModificationException(); // Leaf - petal - Disallow concurrent modification } else { this.listeners.add(listener); } @@ -46,7 +46,7 @@ public class EuclideanGameEventListenerRegistry implements GameEventListenerRegi @Override public void unregister(GameEventListener listener) { if (this.processing) { - this.listenersToRemove.add(listener); + throw new java.util.ConcurrentModificationException(); // Leaf - petal - Disallow concurrent modification } else { this.listeners.remove(listener); } @@ -66,7 +66,7 @@ public class EuclideanGameEventListenerRegistry implements GameEventListenerRegi while (iterator.hasNext()) { GameEventListener gameEventListener = iterator.next(); - if (this.listenersToRemove.remove(gameEventListener)) { + if (false) { // Leaf - petal - Disallow concurrent modification iterator.remove(); } else { Optional optional = getPostableListenerPosition(this.level, pos, gameEventListener); @@ -80,6 +80,8 @@ public class EuclideanGameEventListenerRegistry implements GameEventListenerRegi this.processing = false; } + // Leaf start - petal + /* if (!this.listenersToAdd.isEmpty()) { this.listeners.addAll(this.listenersToAdd); this.listenersToAdd.clear(); @@ -89,6 +91,8 @@ public class EuclideanGameEventListenerRegistry implements GameEventListenerRegi this.listeners.removeAll(this.listenersToRemove); this.listenersToRemove.clear(); } + */ + // Leaf end - petal return bl; } diff --git a/src/main/java/net/minecraft/world/level/gameevent/GameEventDispatcher.java b/src/main/java/net/minecraft/world/level/gameevent/GameEventDispatcher.java index df6c97be1b278c97a20390be5d3e60f429383702..75a34d60d6729f3767dc9bef283db6e9e3baf3a7 100644 --- a/src/main/java/net/minecraft/world/level/gameevent/GameEventDispatcher.java +++ b/src/main/java/net/minecraft/world/level/gameevent/GameEventDispatcher.java @@ -45,6 +45,7 @@ public class GameEventDispatcher { int k1 = SectionPos.blockToSectionCoord(blockposition.getZ() + i); List list = new ArrayList(); GameEventListenerRegistry.ListenerVisitor gameeventlistenerregistry_a = (gameeventlistener, vec3d1) -> { + if (!gameeventlistener.listensToEvent(event.value(), emitter)) return; // Leaf - petal - If they don't listen, ignore if (gameeventlistener.getDeliveryMode() == GameEventListener.DeliveryMode.BY_DISTANCE) { list.add(new GameEvent.ListenerInfo(event, emitterPos, emitter, gameeventlistener, vec3d1)); } else { diff --git a/src/main/java/net/minecraft/world/level/gameevent/GameEventListener.java b/src/main/java/net/minecraft/world/level/gameevent/GameEventListener.java index 0f3a79cc644a5b89fa35fdd413ff5781987c4ef3..9751ddc24899fd4d854899de209e24d9264baebc 100644 --- a/src/main/java/net/minecraft/world/level/gameevent/GameEventListener.java +++ b/src/main/java/net/minecraft/world/level/gameevent/GameEventListener.java @@ -23,4 +23,10 @@ public interface GameEventListener { public interface Provider { T getListener(); } + + // Leaf start - petal - Add check for seeing if this listener cares about an event + default boolean listensToEvent(GameEvent gameEvent, GameEvent.Context context) { + return true; + } + // Leaf end - petal - Add check for seeing if this listener cares about an event }