Mirror von
https://github.com/PaperMC/Paper.git
synchronisiert 2024-12-16 11:30:06 +01:00
bc48a3172d
loaded chunks We retain a list of loaded entities specifically for this usage
222 Zeilen
12 KiB
Diff
222 Zeilen
12 KiB
Diff
From dfcc75ee822253e5d3fc90c85c6737b47ebc2a88 Mon Sep 17 00:00:00 2001
|
|
From: Spottedleaf <Spottedleaf@users.noreply.github.com>
|
|
Date: Thu, 16 Apr 2020 16:49:18 -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 d745eae42..4a3b9226a 100644
|
|
--- a/src/main/java/net/minecraft/server/Chunk.java
|
|
+++ b/src/main/java/net/minecraft/server/Chunk.java
|
|
@@ -801,6 +801,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) {
|
|
@@ -843,6 +844,7 @@ public class Chunk implements IChunkAccess {
|
|
server.getPluginManager().callEvent(unloadEvent);
|
|
// note: saving can be prevented, but not forced if no saving is actually required
|
|
this.mustNotSave = !unloadEvent.isSaveChunk();
|
|
+ ((WorldServer)this.world).onChunkUnload(this); // Paper - optimise entity list iteration
|
|
((WorldServer)this.world).getChunkProvider().removeLoadedChunk(this); // Paper
|
|
// Paper start - neighbour cache
|
|
int chunkX = this.loc.x;
|
|
diff --git a/src/main/java/net/minecraft/server/WorldServer.java b/src/main/java/net/minecraft/server/WorldServer.java
|
|
index a0db38b58..60c8e0d2f 100644
|
|
--- a/src/main/java/net/minecraft/server/WorldServer.java
|
|
+++ b/src/main/java/net/minecraft/server/WorldServer.java
|
|
@@ -187,6 +187,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(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(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) -> {
|
|
@@ -477,14 +496,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 name so we can catch iterator.remove() calls so we can remove from the entity by id map)
|
|
|
|
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
|
|
@@ -520,7 +538,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);
|
|
}
|
|
|
|
@@ -1111,7 +1129,7 @@ public class WorldServer extends World {
|
|
public int[] countMobs(boolean updatePlayerCounts) {
|
|
int[] ret = new int[EntityPlayer.ENUMCREATURETYPE_TOTAL_ENUMS];
|
|
// Paper end
|
|
- ObjectIterator objectiterator = this.entitiesById.values().iterator();
|
|
+ Iterator objectiterator = this.loadedEntities.iterator(); // Paper - use loaded entity list
|
|
|
|
while (objectiterator.hasNext()) {
|
|
Entity entity = (Entity) objectiterator.next();
|
|
@@ -1438,6 +1456,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
|
|
}
|
|
@@ -1507,6 +1526,12 @@ 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(MCUtil.getChunkCoordinate(entity.locX()), MCUtil.getChunkCoordinate(entity.locZ()))) {
|
|
+ // we need this here, as chunk chunk will not run
|
|
+ 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 f56131e3a..1d7a9f131 100644
|
|
--- a/src/main/java/org/bukkit/craftbukkit/CraftWorld.java
|
|
+++ b/src/main/java/org/bukkit/craftbukkit/CraftWorld.java
|
|
@@ -1106,16 +1106,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);
|
|
}
|
|
}
|
|
@@ -1126,16 +1126,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);
|
|
}
|
|
}
|
|
@@ -1156,7 +1156,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();
|
|
@@ -1167,7 +1167,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);
|
|
}
|
|
}
|
|
@@ -1180,7 +1180,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();
|
|
@@ -1193,7 +1193,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 dfa15372b..1f369202b 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();
|
|
--
|
|
2.25.1
|
|
|