Update tick limiter removal and TE/E removal optimizations

This commit is contained in:
Aikar 2015-10-24 02:12:33 -05:00
parent eac397cbab
commit ee9886c278
2 changed files with 121 additions and 118 deletions

View file

@ -1,6 +1,6 @@
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Zach Brown <1254957+zachbr@users.noreply.github.com>
Date: Fri, 16 Oct 2015 21:39:07 -0500
Date: Sat, 24 Oct 2015 01:16:22 -0500
Subject: [PATCH] Disable spigot tick limiters
@ -9,21 +9,29 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
--- a/src/main/java/net/minecraft/server/World.java
+++ b/src/main/java/net/minecraft/server/World.java
@@ -0,0 +0,0 @@ public abstract class World implements IBlockAccess {
guardEntityList = true; // Spigot
// CraftBukkit start - Use field for loop variable
int entitiesThisCycle = 0;
if (tickPosition < 0) tickPosition = 0;
for (entityLimiter.initTick();
- if (tickPosition < 0) tickPosition = 0;
- for (entityLimiter.initTick();
- entitiesThisCycle < entityList.size() && (entitiesThisCycle % 10 != 0 || entityLimiter.shouldContinue());
+ entitiesThisCycle < entityList.size() /*&& (entitiesThisCycle % 10 == 0 || entityLimiter.shouldContinue())*/; // PaperSpigot - Disable tick limiters
tickPosition++, entitiesThisCycle++) {
- tickPosition++, entitiesThisCycle++) {
+ // PaperSpigot start - Disable tick limiters
+ //if (tickPosition < 0) tickPosition = 0;
+ for (tickPosition = 0; tickPosition < entityList.size(); tickPosition++) {
+ // PaperSpigot end
tickPosition = (tickPosition < entityList.size()) ? tickPosition : 0;
entity = (Entity) this.entityList.get(this.tickPosition);
// CraftBukkit end
@@ -0,0 +0,0 @@ public abstract class World implements IBlockAccess {
// Spigot start
int tilesThisCycle = 0;
for (tileLimiter.initTick();
- for (tileLimiter.initTick();
- tilesThisCycle < tileEntityList.size() && (tilesThisCycle % 10 != 0 || tileLimiter.shouldContinue());
+ tilesThisCycle < tileEntityList.size() /*&& (tilesThisCycle % 10 == 0 || tileLimiter.shouldContinue())*/; // PaperSpigot - Disable tick limiters
tileTickPosition++, tilesThisCycle++) {
- tileTickPosition++, tilesThisCycle++) {
+ for (tileTickPosition = 0; tileTickPosition < tileEntityList.size(); tileTickPosition++) { // PaperSpigot - Disable tick limiters
tileTickPosition = (tileTickPosition < tileEntityList.size()) ? tileTickPosition : 0;
TileEntity tileentity = (TileEntity) this.tileEntityList.get(tileTickPosition);
// Spigot start
--

View file

@ -38,12 +38,12 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
// CraftBukkit end
-public abstract class Entity implements ICommandListener {
+// PaperSpigot start - Optimized entity and tileentity removal
+public abstract class Entity implements ICommandListener, org.github.paperspigot.OptimizedRemoveAll.Marker {
+// PaperSpigot start - Optimized TE/E Removal
+public abstract class Entity implements ICommandListener, org.github.paperspigot.OptimizedRemoveAllArrayList.Marker {
+ private boolean needsRemoved = false;
+ public boolean isToBeRemoved() { return needsRemoved; }
+ public void markToBeRemoved() { needsRemoved = true; }
+ // PaperSpigot end
+ public void setRemovalState(boolean removalState) { needsRemoved = removalState; }
+ // PaperSpigot End
// CraftBukkit start
private static final int CURRENT_LEVEL = 2;
@ -56,68 +56,75 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
import org.bukkit.inventory.InventoryHolder; // CraftBukkit
-public abstract class TileEntity {
+// PaperSpigot start - Optimized entity and tileentity removal
+public abstract class TileEntity implements org.github.paperspigot.OptimizedRemoveAll.Marker {
+// PaperSpigot start - Optimized TE/E Removal
+public abstract class TileEntity implements org.github.paperspigot.OptimizedRemoveAllArrayList.Marker {
+ private boolean needsRemoved = false;
+ public boolean isToBeRemoved() { return needsRemoved; }
+ public void markToBeRemoved() { needsRemoved = true; }
+ public void setRemovalState(boolean removalState) { needsRemoved = removalState; }
+ // PaperSpigot end
public CustomTimingsHandler tickTimer = org.bukkit.craftbukkit.SpigotTimings.getTileEntityTimings(this); // Spigot
private static final Logger a = LogManager.getLogger();
@@ -0,0 +0,0 @@ public abstract class TileEntity {
}
public boolean x() {
- return this.d;
+ return this.d && !isToBeRemoved(); // PaperSpigot - Optimize TE/E Removal
}
public void y() {
diff --git a/src/main/java/net/minecraft/server/World.java b/src/main/java/net/minecraft/server/World.java
index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
--- a/src/main/java/net/minecraft/server/World.java
+++ b/src/main/java/net/minecraft/server/World.java
@@ -0,0 +0,0 @@ public abstract class World implements IBlockAccess {
}
this.methodProfiler.c("remove");
- this.entityList.removeAll(this.g);
+ tickPosition = org.github.paperspigot.OptimizedRemoveAll.removeAll(this.entityList, this.g, tickPosition); // PaperSpigot
int j;
int k;
private int a = 63;
protected boolean e;
// Spigot start - guard entity list from removals
- public final List<Entity> entityList = new java.util.ArrayList<Entity>()
+ public final List<Entity> entityList = new org.github.paperspigot.OptimizedRemoveAllArrayList<Entity>() // PaperSpigot - Optimized TE/E Removal
{
@Override
public Entity remove(int index)
@@ -0,0 +0,0 @@ public abstract class World implements IBlockAccess {
// Spigot end
protected final List<Entity> g = Lists.newArrayList();
//public final List<TileEntity> h = Lists.newArrayList(); // PaperSpigot - Remove unused list
- public final List<TileEntity> tileEntityList = Lists.newArrayList();
+ public final List<TileEntity> tileEntityList = new org.github.paperspigot.OptimizedRemoveAllArrayList<TileEntity>(); // PaperSpigot - Optimized TE/E Removal
private final List<TileEntity> b = Lists.newArrayList();
private final List<TileEntity> c = Lists.newArrayList();
public final List<EntityHuman> players = Lists.newArrayList();
@@ -0,0 +0,0 @@ public abstract class World implements IBlockAccess {
}
guardEntityList = false; // Spigot
- this.entityList.remove(this.tickPosition--); // CraftBukkit - Use field for loop variable
+ if (entity instanceof EntityPlayer) this.entityList.remove(this.tickPosition--); // CraftBukkit - Use field for loop variable // PaperSpigot
+ else entity.markToBeRemoved(); // PaperSpigot
+ this.entityList.remove(entity); // CraftBukkit - Use field for loop variable // PaperSpigot - Optimized TE/E Removal
guardEntityList = true; // Spigot
this.b(entity);
}
this.methodProfiler.b();
}
+ tickPosition = org.github.paperspigot.OptimizedRemoveAll.removeAll(this.entityList, null, tickPosition); // PaperSpigot
+ this.entityList.removeAll(null); // PaperSpigot - Optimize TE/E Removal
guardEntityList = false; // Spigot
timings.entityTick.stopTiming(); // Spigot
@@ -0,0 +0,0 @@ public abstract class World implements IBlockAccess {
this.M = true;
// CraftBukkit start - From below, clean up tile entities before ticking them
if (!this.c.isEmpty()) {
- this.tileEntityList.removeAll(this.c);
+ tileTickPosition = org.github.paperspigot.OptimizedRemoveAll.removeAll(this.tileEntityList, this.c, tileTickPosition); // PaperSpigot
//this.h.removeAll(this.c); // PaperSpigot - Remove unused list
this.c.clear();
}
@@ -0,0 +0,0 @@ public abstract class World implements IBlockAccess {
if (tileentity.x()) {
tilesThisCycle--;
- this.tileEntityList.remove(tileTickPosition--);
+ tileentity.markToBeRemoved(); // PaperSpigot
+ //this.tileEntityList.remove(tileTickPosition--);
+ this.tileEntityList.remove(tileentity); // PaperSpigot - Optimized TE/E Removal
//this.h.remove(tileentity); // PaperSpigot - Remove unused list
if (this.isLoaded(tileentity.getPosition())) {
this.getChunkAtWorldCoords(tileentity.getPosition()).e(tileentity.getPosition());
}
}
}
+ tileTickPosition = org.github.paperspigot.OptimizedRemoveAll.removeAll(this.tileEntityList, null, tileTickPosition); // PaperSpigot
+ this.tileEntityList.removeAll(null); // PaperSpigot - Optimized TE/E Removal
timings.tileEntityTick.stopTiming(); // Spigot
timings.tileEntityPending.startTiming(); // Spigot
@ -125,7 +132,7 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
while (iterator.hasNext()) {
Entity entity = (Entity) iterator.next();
+ if (entity.isToBeRemoved()) { continue; } // PaperSpigot
+ if (entity.isToBeRemoved()) { continue; } // PaperSpigot - Optimized TE/E Removal
if (oclass.isAssignableFrom(entity.getClass()) && predicate.apply((T) entity)) { // CraftBukkit - fix decompile error
arraylist.add(entity);
@ -133,104 +140,92 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
while (iterator.hasNext()) {
Entity entity = (Entity) iterator.next();
+ if (entity.isToBeRemoved()) { continue; } // PaperSpigot
+ if (entity.isToBeRemoved()) { continue; } // PaperSpigot - Optimized TE/E Removal
// CraftBukkit start - Split out persistent check, don't apply it to special persistent mobs
if (entity instanceof EntityInsentient) {
EntityInsentient entityinsentient = (EntityInsentient) entity;
diff --git a/src/main/java/org/bukkit/craftbukkit/CraftWorld.java b/src/main/java/org/bukkit/craftbukkit/CraftWorld.java
index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
--- a/src/main/java/org/bukkit/craftbukkit/CraftWorld.java
+++ b/src/main/java/org/bukkit/craftbukkit/CraftWorld.java
@@ -0,0 +0,0 @@ public class CraftWorld implements World {
Entity bukkitEntity = mcEnt.getBukkitEntity();
// Assuming that bukkitEntity isn't null
- if (bukkitEntity != null) {
+ if (bukkitEntity != null && !mcEnt.isToBeRemoved()) { // PaperSpigot
list.add(bukkitEntity);
}
}
@@ -0,0 +0,0 @@ public class CraftWorld implements World {
Entity bukkitEntity = mcEnt.getBukkitEntity();
// Assuming that bukkitEntity isn't null
- if (bukkitEntity != null && bukkitEntity instanceof LivingEntity) {
+ if (bukkitEntity != null && bukkitEntity instanceof LivingEntity && !mcEnt.isToBeRemoved()) { // PaperSpigot
list.add((LivingEntity) bukkitEntity);
}
}
@@ -0,0 +0,0 @@ public class CraftWorld implements World {
if (entity instanceof net.minecraft.server.Entity) {
Entity bukkitEntity = ((net.minecraft.server.Entity) entity).getBukkitEntity();
- if (bukkitEntity == null) {
+ if (bukkitEntity == null || ((net.minecraft.server.Entity) entity).isToBeRemoved()) { // PaperSpigot
continue;
}
@@ -0,0 +0,0 @@ public class CraftWorld implements World {
if (entity instanceof net.minecraft.server.Entity) {
Entity bukkitEntity = ((net.minecraft.server.Entity) entity).getBukkitEntity();
- if (bukkitEntity == null) {
+ if (bukkitEntity == null || ((net.minecraft.server.Entity) entity).isToBeRemoved()) { // PaperSpigot
continue;
}
diff --git a/src/main/java/org/github/paperspigot/OptimizedRemoveAll.java b/src/main/java/org/github/paperspigot/OptimizedRemoveAll.java
diff --git a/src/main/java/org/github/paperspigot/OptimizedRemoveAllArrayList.java b/src/main/java/org/github/paperspigot/OptimizedRemoveAllArrayList.java
new file mode 100644
index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000
--- /dev/null
+++ b/src/main/java/org/github/paperspigot/OptimizedRemoveAll.java
+++ b/src/main/java/org/github/paperspigot/OptimizedRemoveAllArrayList.java
@@ -0,0 +0,0 @@
+package org.github.paperspigot;
+
+import java.util.List;
+import org.github.paperspigot.OptimizedRemoveAllArrayList.Marker;
+
+import java.util.ArrayList;
+import java.util.Collection;
+
+/**
+ * Improved algorithim for bulk removing entries from a list
+ * <p>
+ *
+ * WARNING: This system only works on Identity Based lists,
+ * unlike traditional .removeAll() that operates on object equality.
+ *
+ */
+public final class OptimizedRemoveAll {
+ private OptimizedRemoveAll() {
+public class OptimizedRemoveAllArrayList<T extends Marker> extends ArrayList<T> {
+ public OptimizedRemoveAllArrayList(int initialCapacity) {
+ super(initialCapacity);
+ }
+
+ public OptimizedRemoveAllArrayList() {
+ }
+
+ public OptimizedRemoveAllArrayList(Collection<? extends T> c) {
+ super(c);
+ }
+
+ public OptimizedRemoveAllArrayList<T> clone() {
+ return new OptimizedRemoveAllArrayList<T>(this);
+ }
+
+ @Override
+ public boolean addAll(Collection<? extends T> c) {
+ for (T t : c) {
+ t.setRemovalState(false);
+ }
+ return super.addAll(c);
+ }
+
+ @Override
+ public boolean add(T t) {
+ t.setRemovalState(false);
+ return super.add(t);
+ }
+
+ @Override
+ public boolean remove(Object o) {
+ ((Marker) o).setRemovalState(true);
+ return true;
+ }
+
+ @Override
+ public boolean removeAll(Collection<?> c) {
+ if (c != null) {
+ for (Object o : c) {
+ ((Marker) o).setRemovalState(true);
+ }
+ }
+
+ int size = size();
+ int insertAt = 0;
+
+ for (int i = 0; i < size; i++) {
+ T el = get(i);
+
+ if (el != null && !el.isToBeRemoved()) {
+ set(insertAt++, el);
+ }
+ }
+ subList(insertAt, size).clear();
+
+ return size() != size;
+ }
+
+ public interface Marker {
+ boolean isToBeRemoved();
+
+ void markToBeRemoved();
+ void setRemovalState(boolean removalState);
+ }
+
+ /**
+ * More effecient removeAll method
+ *
+ * @param tickPosition Previous Tick Position
+ * @return New Tick Position
+ */
+ public static <E extends Marker> int removeAll(List<E> list, List<E> removal, int tickPosition) {
+ if (removal != null && !removal.isEmpty()) {
+ int removalSize = removal.size();
+ for (int i = 0; i < removalSize; i++) {
+ removal.get(i).markToBeRemoved();
+ }
+ }
+
+ int size = list.size();
+ int insertAt = 0;
+ for (int i = 0; i < size; i++) {
+ E el = list.get(i);
+ if (i == tickPosition) {
+ tickPosition = insertAt;
+ }
+ if (el != null && !el.isToBeRemoved()) {
+ list.set(insertAt++, el);
+ }
+ }
+ list.subList(insertAt, size).clear();
+ return tickPosition;
+ }
+
+}
--