spigot/CraftBukkit-Patches/0098-Allow-Capping-Tile-Entity-Tick-Time.patch
2019-04-23 12:00:00 +10:00

118 lines
5.5 KiB
Diff

From ca538d7ab540b257d3c98c25d7c81276bf9e0766 Mon Sep 17 00:00:00 2001
From: md_5 <git@md-5.net>
Date: Fri, 20 Feb 2015 21:39:31 +1100
Subject: [PATCH] Allow Capping (Tile)Entity Tick Time.
This patch adds world configuration options for max-tick-time.entity / max-tick-time.tile which allows setting a hard cap on the amount of time (in milliseconds) that a tick can consume. The default values of 50ms each are very conservative and mean this feature will not activate until the server is well below 15tps (minimum). Values of 20ms each have been reported to provide a good performance increase, however I personally think 25ms for entities and 10-15ms for tiles would give even more significant gains, assuming that these things are not a large priority on your server.
For tiles there is very little tradeoff for this option, as tile ticks are based on wall time for most things, however for entities setting this option too low could lead to jerkiness / lag. The gain however is a faster and more responsive server to other actions such as blocks, chat, combat etc.
This feature was commisioned by Chunkr.
diff --git a/src/main/java/net/minecraft/server/World.java b/src/main/java/net/minecraft/server/World.java
index f9cd0e95..230547e7 100644
--- a/src/main/java/net/minecraft/server/World.java
+++ b/src/main/java/net/minecraft/server/World.java
@@ -88,6 +88,9 @@ public abstract class World implements IIBlockAccess, GeneratorAccess, AutoClose
public final SpigotTimings.WorldTimingsHandler timings; // Spigot
public static BlockPosition lastPhysicsProblem; // Spigot
+ private org.spigotmc.TickLimiter entityLimiter;
+ private org.spigotmc.TickLimiter tileLimiter;
+ private int tileTickPosition;
public CraftWorld getWorld() {
return this.world;
@@ -145,6 +148,8 @@ public abstract class World implements IIBlockAccess, GeneratorAccess, AutoClose
});
// CraftBukkit end
timings = new SpigotTimings.WorldTimingsHandler(this); // Spigot - code below can generate new world and access timings
+ this.entityLimiter = new org.spigotmc.TickLimiter(spigotConfig.entityMaxTickTime);
+ this.tileLimiter = new org.spigotmc.TickLimiter(spigotConfig.tileMaxTickTime);
}
@Override
@@ -635,14 +640,19 @@ public abstract class World implements IIBlockAccess, GeneratorAccess, AutoClose
}
this.tickingTileEntities = true;
- Iterator iterator = this.tileEntityListTick.iterator();
-
- while (iterator.hasNext()) {
- TileEntity tileentity = (TileEntity) iterator.next();
+ // Spigot start
+ // Iterator iterator = this.tileEntityListTick.iterator();
+ int tilesThisCycle = 0;
+ for (tileLimiter.initTick();
+ tilesThisCycle < tileEntityListTick.size() && (tilesThisCycle % 10 != 0 || tileLimiter.shouldContinue());
+ tileTickPosition++, tilesThisCycle++) {
+ tileTickPosition = (tileTickPosition < tileEntityListTick.size()) ? tileTickPosition : 0;
+ TileEntity tileentity = (TileEntity) this.tileEntityListTick.get(tileTickPosition);
// Spigot start
if (tileentity == null) {
getServer().getLogger().severe("Spigot has detected a null entity and has removed it, preventing a crash");
- iterator.remove();
+ tilesThisCycle--;
+ this.tileEntityListTick.remove(tileTickPosition--);
continue;
}
// Spigot end
@@ -674,7 +684,10 @@ public abstract class World implements IIBlockAccess, GeneratorAccess, AutoClose
}
if (tileentity.isRemoved()) {
- iterator.remove();
+ // Spigot start
+ tilesThisCycle--;
+ this.tileEntityListTick.remove(tileTickPosition--);
+ // Spigot end
this.tileEntityList.remove(tileentity);
if (this.isLoaded(tileentity.getPosition())) {
this.getChunkAtWorldCoords(tileentity.getPosition()).removeTileEntity(tileentity.getPosition());
diff --git a/src/main/java/org/spigotmc/SpigotWorldConfig.java b/src/main/java/org/spigotmc/SpigotWorldConfig.java
index 932e55eb..88e197db 100644
--- a/src/main/java/org/spigotmc/SpigotWorldConfig.java
+++ b/src/main/java/org/spigotmc/SpigotWorldConfig.java
@@ -298,4 +298,13 @@ public class SpigotWorldConfig
{
hangingTickFrequency = getInt( "hanging-tick-frequency", 100 );
}
+
+ public int tileMaxTickTime;
+ public int entityMaxTickTime;
+ private void maxTickTimes()
+ {
+ tileMaxTickTime = getInt("max-tick-time.tile", 50);
+ entityMaxTickTime = getInt("max-tick-time.entity", 50);
+ log("Tile Max Tick Time: " + tileMaxTickTime + "ms Entity max Tick Time: " + entityMaxTickTime + "ms");
+ }
}
diff --git a/src/main/java/org/spigotmc/TickLimiter.java b/src/main/java/org/spigotmc/TickLimiter.java
new file mode 100644
index 00000000..23a39382
--- /dev/null
+++ b/src/main/java/org/spigotmc/TickLimiter.java
@@ -0,0 +1,20 @@
+package org.spigotmc;
+
+public class TickLimiter {
+
+ private final int maxTime;
+ private long startTime;
+
+ public TickLimiter(int maxtime) {
+ this.maxTime = maxtime;
+ }
+
+ public void initTick() {
+ startTime = System.currentTimeMillis();
+ }
+
+ public boolean shouldContinue() {
+ long remaining = System.currentTimeMillis() - startTime;
+ return remaining < maxTime;
+ }
+}
--
2.20.1