From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: peaches94 Date: Sun, 10 Jul 2022 13:29:20 -0500 Subject: [PATCH] feat: reduce work done by game event system 1. going into game event dispatching can be expensive so run the checks before dispatching 2. euclideangameeventdispatcher is not used concurrently so we ban that usage for improved performance with allays diff --git a/src/main/java/net/minecraft/server/level/ServerLevel.java b/src/main/java/net/minecraft/server/level/ServerLevel.java index 4921c791c7900210e6dd96e03256ea529fd1d03d..a3430ddde87a6c67ce32ae01846d8bf3753f33f6 100644 --- a/src/main/java/net/minecraft/server/level/ServerLevel.java +++ b/src/main/java/net/minecraft/server/level/ServerLevel.java @@ -1707,6 +1707,7 @@ public class ServerLevel extends Level implements WorldGenLevel { if (chunk != null) { for (int j2 = k; j2 <= j1; ++j2) { flag |= chunk.getEventDispatcher(j2).walkListeners(event, emitterPos, emitter, (gameeventlistener, vec3d1) -> { + if (!gameeventlistener.listensToEvent(event, emitter)) return; // petal - if they don't listen, ignore (gameeventlistener.handleEventsImmediately() ? list : this.gameEventMessages).add(new GameEvent.Message(event, emitterPos, emitter, gameeventlistener, vec3d1)); }); } 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 22c309343299e60ed8028229b7f134109001ff35..d5947d29295ddc93ba8ac1c0fc61f7badad582c4 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 @@ -85,6 +85,13 @@ public class SculkCatalystBlockEntity extends BlockEntity implements GameEventLi } } + // petal start + @Override + public boolean listensToEvent(GameEvent gameEvent, GameEvent.Context context) { + return !this.isRemoved() && gameEvent == GameEvent.ENTITY_DIE && context.sourceEntity() instanceof LivingEntity; + } + // petal end + public static void serverTick(Level world, BlockPos pos, BlockState state, SculkCatalystBlockEntity blockEntity) { org.bukkit.craftbukkit.event.CraftEventFactory.sourceBlockOverride = blockEntity.getBlockPos(); // CraftBukkit - SPIGOT-7068: Add source block override, not the most elegant way but better than passing down a BlockPosition up to five methods deep. blockEntity.sculkSpreader.updateCursors(world, pos, world.getRandom(), true); 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 2877441927de3aeba28f5c3dd876e2ae4c15797c..aae74715e9daa1c6a842c851cf14f5030d2f5efb 100644 --- a/src/main/java/net/minecraft/world/level/chunk/LevelChunk.java +++ b/src/main/java/net/minecraft/world/level/chunk/LevelChunk.java @@ -84,7 +84,18 @@ public class LevelChunk extends ChunkAccess { private Supplier fullStatus; @Nullable private LevelChunk.PostLoadProcessor postLoad; - private final Int2ObjectMap gameEventDispatcherSections; + // petal start + private final GameEventDispatcher[] gameEventDispatcherSections; + 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); + } + // petal end private final LevelChunkTicks blockTicks; private final LevelChunkTicks fluidTicks; @@ -113,7 +124,7 @@ public class LevelChunk extends ChunkAccess { this.tickersInLevel = Maps.newHashMap(); this.clientLightReady = false; this.level = (ServerLevel) world; // CraftBukkit - type - this.gameEventDispatcherSections = new Int2ObjectOpenHashMap(); + this.gameEventDispatcherSections = new GameEventDispatcher[getGameEventSectionLength(this.getSectionsCount())]; // petal Heightmap.Types[] aheightmap_type = Heightmap.Types.values(); int j = aheightmap_type.length; @@ -446,9 +457,23 @@ public class LevelChunk extends ChunkAccess { if (world instanceof ServerLevel) { ServerLevel worldserver = (ServerLevel) world; - return (GameEventDispatcher) this.gameEventDispatcherSections.computeIfAbsent(ySectionCoord, (j) -> { - return new EuclideanGameEventDispatcher(worldserver); - }); + // petal start + 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.gameEventDispatcherSections.length) { + return GameEventDispatcher.NOOP; + } + + var dispatcher = this.gameEventDispatcherSections[sectionIndex]; + + if (dispatcher == null) { + dispatcher = this.gameEventDispatcherSections[sectionIndex] = new EuclideanGameEventDispatcher(worldserver); + } + + return dispatcher; + // petal end } else { return super.getEventDispatcher(ySectionCoord); } @@ -812,7 +837,7 @@ public class LevelChunk extends ChunkAccess { gameeventdispatcher.unregister(gameeventlistener); if (gameeventdispatcher.isEmpty()) { - this.gameEventDispatcherSections.remove(i); + this.gameEventDispatcherSections[getGameEventSectionIndex(this.getSectionIndexFromSectionY(i))] = null; // petal } } } diff --git a/src/main/java/net/minecraft/world/level/gameevent/EuclideanGameEventDispatcher.java b/src/main/java/net/minecraft/world/level/gameevent/EuclideanGameEventDispatcher.java index 0dd708ebe81f73710de51215529c05ec61837dd3..f5b402efa86f824c460db8cac20c1c2b090f82d0 100644 --- a/src/main/java/net/minecraft/world/level/gameevent/EuclideanGameEventDispatcher.java +++ b/src/main/java/net/minecraft/world/level/gameevent/EuclideanGameEventDispatcher.java @@ -13,8 +13,8 @@ import net.minecraft.world.phys.Vec3; public class EuclideanGameEventDispatcher implements GameEventDispatcher { private final List listeners = Lists.newArrayList(); - private final Set listenersToRemove = Sets.newHashSet(); - private final List listenersToAdd = Lists.newArrayList(); + //private final Set listenersToRemove = Sets.newHashSet(); // petal - not necessary + //private final List listenersToAdd = Lists.newArrayList(); // petal private boolean processing; private final ServerLevel level; @@ -30,7 +30,7 @@ public class EuclideanGameEventDispatcher implements GameEventDispatcher { @Override public void register(GameEventListener listener) { if (this.processing) { - this.listenersToAdd.add(listener); + throw new java.util.ConcurrentModificationException(); // petal - disallow concurrent modification } else { this.listeners.add(listener); } @@ -41,7 +41,7 @@ public class EuclideanGameEventDispatcher implements GameEventDispatcher { @Override public void unregister(GameEventListener listener) { if (this.processing) { - this.listenersToRemove.add(listener); + throw new java.util.ConcurrentModificationException(); // petal - disallow concurrent modification } else { this.listeners.remove(listener); } @@ -58,7 +58,7 @@ public class EuclideanGameEventDispatcher implements GameEventDispatcher { while(iterator.hasNext()) { GameEventListener gameEventListener = iterator.next(); - if (this.listenersToRemove.remove(gameEventListener)) { + if (false) { // petal - disallow concurrent modification iterator.remove(); } else { Optional optional = getPostableListenerPosition(this.level, pos, gameEventListener); @@ -72,6 +72,8 @@ public class EuclideanGameEventDispatcher implements GameEventDispatcher { this.processing = false; } + // petal start + /* if (!this.listenersToAdd.isEmpty()) { this.listeners.addAll(this.listenersToAdd); this.listenersToAdd.clear(); @@ -81,6 +83,8 @@ public class EuclideanGameEventDispatcher implements GameEventDispatcher { this.listeners.removeAll(this.listenersToRemove); this.listenersToRemove.clear(); } + */ + // petal end return bl; } 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 e5601afe8b739da518f36ae306f5e0cb252238f0..bc8f04424c5e8c416d6988f0e06d8cadbb400ca7 100644 --- a/src/main/java/net/minecraft/world/level/gameevent/GameEventListener.java +++ b/src/main/java/net/minecraft/world/level/gameevent/GameEventListener.java @@ -12,4 +12,10 @@ public interface GameEventListener { int getListenerRadius(); boolean handleGameEvent(ServerLevel world, GameEvent.Message event); + + // petal start - add check for seeing if this listener cares about an event + default boolean listensToEvent(net.minecraft.world.level.gameevent.GameEvent gameEvent, net.minecraft.world.level.gameevent.GameEvent.Context context) { + return true; + } + // petal end } diff --git a/src/main/java/net/minecraft/world/level/gameevent/vibrations/VibrationListener.java b/src/main/java/net/minecraft/world/level/gameevent/vibrations/VibrationListener.java index e45f54534bbf054eaf0008546ff459d4c11ddd50..e49d0d1c2a539fcd7e75262c4010475193964287 100644 --- a/src/main/java/net/minecraft/world/level/gameevent/vibrations/VibrationListener.java +++ b/src/main/java/net/minecraft/world/level/gameevent/vibrations/VibrationListener.java @@ -162,6 +162,13 @@ public class VibrationListener implements GameEventListener { return true; } + // petal start + @Override + public boolean listensToEvent(GameEvent gameEvent, GameEvent.Context context) { + return this.receivingEvent == null && gameEvent.is(this.config.getListenableEvents()); + } + // petal end + public interface VibrationListenerConfig { default TagKey getListenableEvents() {