Revert loaded entity list (#3304)

This commit is contained in:
Spottedleaf 2020-05-06 09:31:29 -07:00 committed by GitHub
parent fa87db6b91
commit e2c23475b5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 16 additions and 259 deletions

View file

@ -1,232 +0,0 @@
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Spottedleaf <Spottedleaf@users.noreply.github.com>
Date: Tue, 5 May 2020 19:49:23 -0700
Subject: [PATCH] Optimize entity list iteration requiring entities be in
loaded chunks
We retain a list of loaded entities specifically for this usage
diff --git a/src/main/java/net/minecraft/server/Chunk.java b/src/main/java/net/minecraft/server/Chunk.java
index 750fb07756f7e40b21f8ab0925f2e842aae50f7b..21918ab101b423eadff5c00530f7a7c8e40cde9a 100644
--- a/src/main/java/net/minecraft/server/Chunk.java
+++ b/src/main/java/net/minecraft/server/Chunk.java
@@ -588,6 +588,13 @@ public class Chunk implements IChunkAccess {
entity.chunkZ = this.loc.z;
this.entities.add(entity); // Paper - per chunk entity list
this.entitySlices[k].add(entity); if (entity.hardCollides()) this.hardCollidingEntities[k].add(entity); // Paper - optimise hard colliding entities
+ // Paper start - world loaded entity list
+ if (this.loadedTicketLevel) {
+ ((WorldServer)this.world).loadedEntities.add(entity);
+ } else {
+ ((WorldServer)this.world).loadedEntities.remove(entity);
+ }
+ // Paper end - world loaded entity list
// Paper start
if (entity instanceof EntityItem) {
itemCounts[k]++;
@@ -627,6 +634,9 @@ public class Chunk implements IChunkAccess {
if (entity.hardCollides()) this.hardCollidingEntities[i].remove(entity); if (!this.entitySlices[i].remove(entity)) { // Paper - optimise hard colliding entities
return;
}
+ // Paper start - world loaded entity list
+ ((WorldServer)this.world).loadedEntities.remove(entity);
+ // Paper end - world loaded entity list
if (entity instanceof EntityItem) {
itemCounts[i]--;
} else if (entity instanceof IInventory) {
@@ -801,6 +811,7 @@ public class Chunk implements IChunkAccess {
this.setNeighbourLoaded(0, 0, this);
this.loadedTicketLevel = true;
// Paper end - neighbour cache
+ ((WorldServer)this.world).onChunkLoad(this); // Paper - optimise entity list iteration
org.bukkit.Server server = this.world.getServer();
((WorldServer)this.world).getChunkProvider().addLoadedChunk(this); // Paper
if (server != null) {
@@ -859,6 +870,7 @@ public class Chunk implements IChunkAccess {
this.loadedTicketLevel = false;
this.resetNeighbours();
// Paper end
+ ((WorldServer)this.world).onChunkUnload(this); // Paper - optimise entity list iteration
}
// CraftBukkit end
diff --git a/src/main/java/net/minecraft/server/WorldServer.java b/src/main/java/net/minecraft/server/WorldServer.java
index b3785775ecd8e3c13e7829f641f2c1b5fd0d9d47..d9b3aa285a7b187f692088bab320f1de2160300b 100644
--- a/src/main/java/net/minecraft/server/WorldServer.java
+++ b/src/main/java/net/minecraft/server/WorldServer.java
@@ -189,6 +189,25 @@ public class WorldServer extends World {
}
// Paper end - rewrite ticklistserver
+ // Paper start - Optimize entity list iteration requiring entities be in loaded chunks
+ public final com.destroystokyo.paper.util.maplist.EntityList loadedEntities = new com.destroystokyo.paper.util.maplist.EntityList();
+ void onChunkLoad(final Chunk chunk) {
+ final com.destroystokyo.paper.util.maplist.EntityList list = chunk.entities;
+ final Entity[] entities = list.getRawData();
+ for (int i = 0, size = list.size(); i < size; ++i) {
+ this.loadedEntities.add(entities[i]);
+ }
+ }
+
+ void onChunkUnload(final Chunk chunk) {
+ final com.destroystokyo.paper.util.maplist.EntityList list = chunk.entities;
+ final Entity[] entities = list.getRawData();
+ for (int i = 0, size = list.size(); i < size; ++i) {
+ this.loadedEntities.remove(entities[i]);
+ }
+ }
+ // Paper end - Optimize entity list iteration requiring entities be in loaded chunks
+
// Add env and gen to constructor
public WorldServer(MinecraftServer minecraftserver, Executor executor, WorldNBTStorage worldnbtstorage, WorldData worlddata, DimensionManager dimensionmanager, GameProfilerFiller gameprofilerfiller, WorldLoadListener worldloadlistener, org.bukkit.World.Environment env, org.bukkit.generator.ChunkGenerator gen) {
super(worlddata, dimensionmanager, (world, worldprovider) -> {
@@ -489,14 +508,13 @@ public class WorldServer extends World {
gameprofilerfiller.exitEnter("regular");
this.tickingEntities = true;
- ObjectIterator objectiterator = this.entitiesById.int2ObjectEntrySet().iterator();
+ Iterator<Entity> entityiterator = this.loadedEntities.iterator(); // Paper - use loaded entity list - change var name to compile fail on usage change, we need to hook remove() calls here
org.spigotmc.ActivationRange.activateEntities(this); // Spigot
timings.entityTick.startTiming(); // Spigot
TimingHistory.entityTicks += this.globalEntityList.size(); // Paper
- while (objectiterator.hasNext()) {
- Entry<Entity> entry = (Entry) objectiterator.next();
- Entity entity1 = (Entity) entry.getValue();
+ while (entityiterator.hasNext()) { // Paper - use loaded entity list
+ Entity entity1 = entityiterator.next(); // Paper - use loaded entity list
Entity entity2 = entity1.getVehicle();
/* CraftBukkit start - We prevent spawning in general, so this butchering is not needed
@@ -532,7 +550,7 @@ public class WorldServer extends World {
gameprofilerfiller.enter("remove");
if (entity1.dead) {
this.removeEntityFromChunk(entity1);
- objectiterator.remove();
+ entityiterator.remove(); this.entitiesById.remove(entity1.getId()); // Paper - use loaded entity list
this.unregisterEntity(entity1);
}
@@ -1445,6 +1463,7 @@ public class WorldServer extends World {
if (entity instanceof EntityInsentient) {
this.navigators.remove(((EntityInsentient) entity).getNavigation());
}
+ this.loadedEntities.remove(entity); // Paper - loaded entity list
new com.destroystokyo.paper.event.entity.EntityRemoveFromWorldEvent(entity.getBukkitEntity()).callEvent(); // Paper - fire while valid
entity.valid = false; // CraftBukkit
}
@@ -1514,6 +1533,11 @@ public class WorldServer extends World {
}
// Paper end
entity.shouldBeRemoved = false; // Paper - shouldn't be removed after being re-added
+ // Paper start - loaded entity list
+ if (this.isChunkLoaded(net.minecraft.server.MCUtil.getChunkCoordinate(entity.locX()), net.minecraft.server.MCUtil.getChunkCoordinate(entity.locZ()))) {
+ this.loadedEntities.add(entity);
+ }
+ // Paper end - loaded entity list
new com.destroystokyo.paper.event.entity.EntityAddToWorldEvent(entity.getBukkitEntity()).callEvent(); // Paper - fire while valid
}
diff --git a/src/main/java/org/bukkit/craftbukkit/CraftWorld.java b/src/main/java/org/bukkit/craftbukkit/CraftWorld.java
index ac257d50dea8f42a515f19bbae12ab5680e26bb4..995f706678fa0f4e88078d5a15c62dcac25f5fca 100644
--- a/src/main/java/org/bukkit/craftbukkit/CraftWorld.java
+++ b/src/main/java/org/bukkit/craftbukkit/CraftWorld.java
@@ -1110,16 +1110,16 @@ public class CraftWorld implements World {
@Override
public List<Entity> getEntities() {
- List<Entity> list = new ArrayList<Entity>();
+ List<Entity> list = new ArrayList<Entity>(world.loadedEntities.size()); // Paper - optimize this call
- for (Object o : world.entitiesById.values()) {
+ for (Object o : world.loadedEntities) { // Paper - optimize this call
if (o instanceof net.minecraft.server.Entity) {
net.minecraft.server.Entity mcEnt = (net.minecraft.server.Entity) o;
if (mcEnt.shouldBeRemoved) continue; // Paper
Entity bukkitEntity = mcEnt.getBukkitEntity();
// Assuming that bukkitEntity isn't null
- if (bukkitEntity != null && bukkitEntity.isValid()) {
+ if (bukkitEntity != null && CraftEntity.canBeSeenByPlugins(bukkitEntity)) { // Paper - optimize this call
list.add(bukkitEntity);
}
}
@@ -1130,16 +1130,16 @@ public class CraftWorld implements World {
@Override
public List<LivingEntity> getLivingEntities() {
- List<LivingEntity> list = new ArrayList<LivingEntity>();
+ List<LivingEntity> list = new ArrayList<LivingEntity>(world.loadedEntities.size()); // Paper - optimize this call
- for (Object o : world.entitiesById.values()) {
+ for (Object o : world.loadedEntities) { // Paper - optimize this call
if (o instanceof net.minecraft.server.Entity) {
net.minecraft.server.Entity mcEnt = (net.minecraft.server.Entity) o;
if (mcEnt.shouldBeRemoved) continue; // Paper
Entity bukkitEntity = mcEnt.getBukkitEntity();
// Assuming that bukkitEntity isn't null
- if (bukkitEntity != null && bukkitEntity instanceof LivingEntity && bukkitEntity.isValid()) {
+ if (bukkitEntity != null && bukkitEntity instanceof LivingEntity && CraftEntity.canBeSeenByPlugins(bukkitEntity)) { // Paper - optimize this call
list.add((LivingEntity) bukkitEntity);
}
}
@@ -1160,7 +1160,7 @@ public class CraftWorld implements World {
public <T extends Entity> Collection<T> getEntitiesByClass(Class<T> clazz) {
Collection<T> list = new ArrayList<T>();
- for (Object entity: world.entitiesById.values()) {
+ for (Object entity: world.loadedEntities) { // Paper - optimize this call
if (entity instanceof net.minecraft.server.Entity) {
if (((net.minecraft.server.Entity) entity).shouldBeRemoved) continue; // Paper
Entity bukkitEntity = ((net.minecraft.server.Entity) entity).getBukkitEntity();
@@ -1171,7 +1171,7 @@ public class CraftWorld implements World {
Class<?> bukkitClass = bukkitEntity.getClass();
- if (clazz.isAssignableFrom(bukkitClass) && bukkitEntity.isValid()) {
+ if (clazz.isAssignableFrom(bukkitClass) && CraftEntity.canBeSeenByPlugins(bukkitEntity)) { // Paper - optimize this call
list.add((T) bukkitEntity);
}
}
@@ -1184,7 +1184,7 @@ public class CraftWorld implements World {
public Collection<Entity> getEntitiesByClasses(Class<?>... classes) {
Collection<Entity> list = new ArrayList<Entity>();
- for (Object entity: world.entitiesById.values()) {
+ for (Object entity: world.loadedEntities) { // Paper - optimize this call
if (entity instanceof net.minecraft.server.Entity) {
if (((net.minecraft.server.Entity) entity).shouldBeRemoved) continue; // Paper
Entity bukkitEntity = ((net.minecraft.server.Entity) entity).getBukkitEntity();
@@ -1197,7 +1197,7 @@ public class CraftWorld implements World {
for (Class<?> clazz : classes) {
if (clazz.isAssignableFrom(bukkitClass)) {
- if (bukkitEntity.isValid()) {
+ if (CraftEntity.canBeSeenByPlugins(bukkitEntity)) { // Paper - optimize this call
list.add(bukkitEntity);
}
break;
diff --git a/src/main/java/org/bukkit/craftbukkit/entity/CraftEntity.java b/src/main/java/org/bukkit/craftbukkit/entity/CraftEntity.java
index ff60568ce4320e6ebfa50489a564538b0f57da82..f3771121617ad6ad256562dc72a50f241f530929 100644
--- a/src/main/java/org/bukkit/craftbukkit/entity/CraftEntity.java
+++ b/src/main/java/org/bukkit/craftbukkit/entity/CraftEntity.java
@@ -180,6 +180,18 @@ public abstract class CraftEntity implements org.bukkit.entity.Entity {
this.entity = entity;
}
+ // Paper start
+ // note: this does not check isChunkLoaded, use Entity#isValid to do that
+ public static boolean canBeSeenByPlugins(org.bukkit.entity.Entity entity) {
+ Entity handle = ((CraftEntity)entity).getHandle();
+ // TODO
+ // isAlive is a dumb choice, given living entities aren't alive (but are in the world) if health < 0
+ // this needs to be brought up to spigot to fix though, we are NOT breaking api implementation, especially
+ // if no-one's complained.
+ return !handle.shouldBeRemoved && handle.isAlive() && handle.valid;
+ }
+ // Paper end
+
@Override
public Chunk getChunk() {
net.minecraft.server.Chunk currentChunk = entity.getCurrentChunk();

View file

@ -44,7 +44,7 @@ index 3a88c9a67062eb73ad8257ea786efca7e7e99f65..6d3b34ead9cc95dcc1152dffa8c6c4a8
List<Entity> list = this.tracker.getPassengers();
diff --git a/src/main/java/net/minecraft/server/PlayerChunkMap.java b/src/main/java/net/minecraft/server/PlayerChunkMap.java
index bdf835397aa691c41280f65a7785e777791b2891..d460bc62b8d89582457c1b6ab530b0ddc9971526 100644
index bdf835397aa691c41280f65a7785e777791b2891..3d31f00a22233bd885496d7aac34eb2b634c40e8 100644
--- a/src/main/java/net/minecraft/server/PlayerChunkMap.java
+++ b/src/main/java/net/minecraft/server/PlayerChunkMap.java
@@ -120,21 +120,51 @@ public class PlayerChunkMap extends IChunkLoader implements PlayerChunk.d {
@ -173,7 +173,7 @@ index bdf835397aa691c41280f65a7785e777791b2891..d460bc62b8d89582457c1b6ab530b0dd
if (entity instanceof EntityPlayer) {
EntityPlayer entityplayer = (EntityPlayer) entity;
@@ -1594,7 +1652,48 @@ public class PlayerChunkMap extends IChunkLoader implements PlayerChunk.d {
@@ -1594,7 +1652,37 @@ public class PlayerChunkMap extends IChunkLoader implements PlayerChunk.d {
entity.tracker = null; // Paper - We're no longer tracked
}
@ -181,15 +181,9 @@ index bdf835397aa691c41280f65a7785e777791b2891..d460bc62b8d89582457c1b6ab530b0dd
+ private final void processTrackQueue() {
+ this.world.timings.tracker1.startTiming();
+ try {
+ Entity[] entities = this.world.loadedEntities.getRawData();
+ for (int i = 0, len = this.world.loadedEntities.size(); i < len; ++i) {
+ Entity tracked = entities[i];
+ for (EntityTracker tracker : this.trackedEntities.values()) {
+ // update tracker entry
+ EntityTracker tracker = this.trackedEntities.get(tracked.getId());
+ if (tracker == null) {
+ continue;
+ }
+ tracker.updatePlayers(tracked.getPlayersInTrackRange());
+ tracker.updatePlayers(tracker.tracker.getPlayersInTrackRange());
+ }
+ } finally {
+ this.world.timings.tracker1.stopTiming();
@ -198,13 +192,8 @@ index bdf835397aa691c41280f65a7785e777791b2891..d460bc62b8d89582457c1b6ab530b0dd
+
+ this.world.timings.tracker2.startTiming();
+ try {
+ Entity[] entities = this.world.loadedEntities.getRawData();
+ for (int i = 0, len = this.world.loadedEntities.size(); i < len; ++i) {
+ Entity tracked = entities[i];
+ EntityTracker tracker = this.trackedEntities.get(tracked.getId());
+ if (tracker != null) {
+ tracker.trackerEntry.tick();
+ }
+ for (EntityTracker tracker : this.trackedEntities.values()) {
+ tracker.trackerEntry.tick();
+ }
+ } finally {
+ this.world.timings.tracker2.stopTiming();
@ -222,7 +211,7 @@ index bdf835397aa691c41280f65a7785e777791b2891..d460bc62b8d89582457c1b6ab530b0dd
List<EntityPlayer> list = Lists.newArrayList();
List<EntityPlayer> list1 = this.world.getPlayers();
@@ -1662,23 +1761,31 @@ public class PlayerChunkMap extends IChunkLoader implements PlayerChunk.d {
@@ -1662,23 +1750,31 @@ public class PlayerChunkMap extends IChunkLoader implements PlayerChunk.d {
PacketDebug.a(this.world, chunk.getPos());
List<Entity> list = Lists.newArrayList();
List<Entity> list1 = Lists.newArrayList();
@ -266,7 +255,7 @@ index bdf835397aa691c41280f65a7785e777791b2891..d460bc62b8d89582457c1b6ab530b0dd
Iterator iterator;
Entity entity1;
@@ -1716,7 +1823,7 @@ public class PlayerChunkMap extends IChunkLoader implements PlayerChunk.d {
@@ -1716,7 +1812,7 @@ public class PlayerChunkMap extends IChunkLoader implements PlayerChunk.d {
public class EntityTracker {
@ -275,7 +264,7 @@ index bdf835397aa691c41280f65a7785e777791b2891..d460bc62b8d89582457c1b6ab530b0dd
private final Entity tracker;
private final int trackingDistance;
private SectionPosition e;
@@ -1733,6 +1840,42 @@ public class PlayerChunkMap extends IChunkLoader implements PlayerChunk.d {
@@ -1733,6 +1829,42 @@ public class PlayerChunkMap extends IChunkLoader implements PlayerChunk.d {
this.e = SectionPosition.a(entity);
}
@ -318,7 +307,7 @@ index bdf835397aa691c41280f65a7785e777791b2891..d460bc62b8d89582457c1b6ab530b0dd
public boolean equals(Object object) {
return object instanceof PlayerChunkMap.EntityTracker ? ((PlayerChunkMap.EntityTracker) object).tracker.getId() == this.tracker.getId() : false;
}
@@ -1829,7 +1972,7 @@ public class PlayerChunkMap extends IChunkLoader implements PlayerChunk.d {
@@ -1829,7 +1961,7 @@ public class PlayerChunkMap extends IChunkLoader implements PlayerChunk.d {
int j = entity.getEntityType().getChunkRange() * 16;
j = org.spigotmc.TrackingRange.getEntityTrackingRange(entity, j); // Paper

View file

@ -192,7 +192,7 @@ index 4b341c81fc97076a69aede729008c54674fa7adf..bae9371a1e220f4fc78a3905cad24a2e
// Paper start
diff --git a/src/main/java/net/minecraft/server/PlayerChunkMap.java b/src/main/java/net/minecraft/server/PlayerChunkMap.java
index d460bc62b8d89582457c1b6ab530b0ddc9971526..4317d9b98e4a8a994bc7d215aa71489c0de3a14e 100644
index 3d31f00a22233bd885496d7aac34eb2b634c40e8..b3c9cb67664491c3a8c83a67ac0e79d48561f3fe 100644
--- a/src/main/java/net/minecraft/server/PlayerChunkMap.java
+++ b/src/main/java/net/minecraft/server/PlayerChunkMap.java
@@ -130,6 +130,17 @@ public class PlayerChunkMap extends IChunkLoader implements PlayerChunk.d {

View file

@ -23,7 +23,7 @@ index 4612697569fd6e3683b0e58453b61a9a8d077229..5c8a946d5c895fc2622c7df656cc462c
+ }
}
diff --git a/src/main/java/net/minecraft/server/Chunk.java b/src/main/java/net/minecraft/server/Chunk.java
index 69bfece7d43bc9e0c4cf7f71bffec9a44c2b9a67..2332f126f73d9914ade140fa78f18921787e90b3 100644
index 750fb07756f7e40b21f8ab0925f2e842aae50f7b..8c1f3290d23795b58a30274c9437dc7dc43fa3a1 100644
--- a/src/main/java/net/minecraft/server/Chunk.java
+++ b/src/main/java/net/minecraft/server/Chunk.java
@@ -245,7 +245,51 @@ public class Chunk implements IChunkAccess {
@ -207,7 +207,7 @@ index bae9371a1e220f4fc78a3905cad24a2e7f88771c..9d71c4c455d68bcc82dc56b0706c7305
public CompletableFuture<Either<IChunkAccess, PlayerChunk.Failure>> a(ChunkStatus chunkstatus, PlayerChunkMap playerchunkmap) {
diff --git a/src/main/java/net/minecraft/server/PlayerChunkMap.java b/src/main/java/net/minecraft/server/PlayerChunkMap.java
index 4317d9b98e4a8a994bc7d215aa71489c0de3a14e..a3abad95a11be9ff802063e88a5cc7daadb627bb 100644
index b3c9cb67664491c3a8c83a67ac0e79d48561f3fe..ce7462283873635ad0d3bce2395346ed9c3637a9 100644
--- a/src/main/java/net/minecraft/server/PlayerChunkMap.java
+++ b/src/main/java/net/minecraft/server/PlayerChunkMap.java
@@ -71,7 +71,7 @@ public class PlayerChunkMap extends IChunkLoader implements PlayerChunk.d {
@ -532,7 +532,7 @@ index 4317d9b98e4a8a994bc7d215aa71489c0de3a14e..a3abad95a11be9ff802063e88a5cc7da
}
protected void addEntity(Entity entity) {
@@ -1829,6 +1959,7 @@ public class PlayerChunkMap extends IChunkLoader implements PlayerChunk.d {
@@ -1818,6 +1948,7 @@ public class PlayerChunkMap extends IChunkLoader implements PlayerChunk.d {
}
@ -540,7 +540,7 @@ index 4317d9b98e4a8a994bc7d215aa71489c0de3a14e..a3abad95a11be9ff802063e88a5cc7da
private void a(EntityPlayer entityplayer, Packet<?>[] apacket, Chunk chunk) {
if (apacket[0] == null) {
apacket[0] = new PacketPlayOutMapChunk(chunk, 65535);
@@ -2014,7 +2145,7 @@ public class PlayerChunkMap extends IChunkLoader implements PlayerChunk.d {
@@ -2003,7 +2134,7 @@ public class PlayerChunkMap extends IChunkLoader implements PlayerChunk.d {
ChunkCoordIntPair chunkcoordintpair = new ChunkCoordIntPair(this.tracker.chunkX, this.tracker.chunkZ);
PlayerChunk playerchunk = PlayerChunkMap.this.getVisibleChunk(chunkcoordintpair.pair());
@ -600,7 +600,7 @@ index 899c535c4056cd2375ab8f834f03267d405f4bda..0e6368d0fb3beccb492ae3867fb4e228
if (!this.isClientSide && (i & 1) != 0) {
diff --git a/src/main/java/org/bukkit/craftbukkit/CraftWorld.java b/src/main/java/org/bukkit/craftbukkit/CraftWorld.java
index 995f706678fa0f4e88078d5a15c62dcac25f5fca..ee7ae4638981ab372f65734fb560157cc06c18e9 100644
index ac257d50dea8f42a515f19bbae12ab5680e26bb4..4aafad0762df44990ed9d610a59f3862bd3fe3b0 100644
--- a/src/main/java/org/bukkit/craftbukkit/CraftWorld.java
+++ b/src/main/java/org/bukkit/craftbukkit/CraftWorld.java
@@ -2483,10 +2483,39 @@ public class CraftWorld implements World {