spigot/CraftBukkit-Patches/0079-Allow-Capping-Tile-Entity-Tick-Time.patch

117 lines
5.4 KiB
Diff
Raw Normal View History

From f12b7f8a5ca6fcc951590fecb6d8670438e759c6 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.
2021-03-16 09:00:00 +11:00
diff --git a/src/main/java/net/minecraft/world/level/World.java b/src/main/java/net/minecraft/world/level/World.java
2023-06-08 01:30:00 +10:00
index 15e63dbfa..88f200fc1 100644
2021-03-16 09:00:00 +11:00
--- a/src/main/java/net/minecraft/world/level/World.java
+++ b/src/main/java/net/minecraft/world/level/World.java
2023-06-08 01:30:00 +10:00
@@ -158,6 +158,9 @@ public abstract class World implements GeneratorAccess, AutoCloseable {
2019-04-23 12:00:00 +10:00
public final SpigotTimings.WorldTimingsHandler timings; // Spigot
public static BlockPosition lastPhysicsProblem; // Spigot
+ private org.spigotmc.TickLimiter entityLimiter;
+ private org.spigotmc.TickLimiter tileLimiter;
+ private int tileTickPosition;
2016-03-01 08:33:06 +11:00
public CraftWorld getWorld() {
return this.world;
2023-06-08 01:30:00 +10:00
@@ -251,6 +254,8 @@ public abstract class World implements GeneratorAccess, AutoCloseable {
2019-04-23 12:00:00 +10:00
});
2016-03-01 11:23:45 +11:00
// CraftBukkit end
2016-03-01 08:33:06 +11:00
timings = new SpigotTimings.WorldTimingsHandler(this); // Spigot - code below can generate new world and access timings
2019-04-23 12:00:00 +10:00
+ this.entityLimiter = new org.spigotmc.TickLimiter(spigotConfig.entityMaxTickTime);
+ this.tileLimiter = new org.spigotmc.TickLimiter(spigotConfig.tileMaxTickTime);
}
2019-04-23 12:00:00 +10:00
@Override
2023-06-08 01:30:00 +10:00
@@ -638,20 +643,28 @@ public abstract class World implements GeneratorAccess, AutoCloseable {
2021-06-11 15:00:00 +10:00
timings.tileEntityPending.stopTiming(); // Spigot
2021-06-11 15:00:00 +10:00
timings.tileEntityTick.startTiming(); // Spigot
- Iterator iterator = this.blockEntityTickers.iterator();
2017-09-18 20:00:00 +10:00
-
- while (iterator.hasNext()) {
2021-06-11 15:00:00 +10:00
- TickingBlockEntity tickingblockentity = (TickingBlockEntity) iterator.next();
+ // Spigot start
2021-06-11 15:00:00 +10:00
+ // Iterator iterator = this.blockEntityTickers.iterator();
+ int tilesThisCycle = 0;
+ for (tileLimiter.initTick();
2021-06-11 15:00:00 +10:00
+ tilesThisCycle < blockEntityTickers.size() && (tilesThisCycle % 10 != 0 || tileLimiter.shouldContinue());
+ tileTickPosition++, tilesThisCycle++) {
2021-06-11 15:00:00 +10:00
+ tileTickPosition = (tileTickPosition < blockEntityTickers.size()) ? tileTickPosition : 0;
+ TickingBlockEntity tickingblockentity = (TickingBlockEntity) this.blockEntityTickers.get(tileTickPosition);
// Spigot start
2021-06-11 15:00:00 +10:00
if (tickingblockentity == null) {
2021-06-11 21:39:23 +10:00
getCraftServer().getLogger().severe("Spigot has detected a null entity and has removed it, preventing a crash");
- iterator.remove();
+ tilesThisCycle--;
2021-06-11 15:00:00 +10:00
+ this.blockEntityTickers.remove(tileTickPosition--);
continue;
}
// Spigot end
2021-11-22 09:00:00 +11:00
if (tickingblockentity.isRemoved()) {
- iterator.remove();
2019-04-23 12:00:00 +10:00
+ // Spigot start
+ tilesThisCycle--;
2021-06-11 15:00:00 +10:00
+ this.blockEntityTickers.remove(tileTickPosition--);
2019-04-23 12:00:00 +10:00
+ // Spigot end
2022-06-08 02:00:00 +10:00
} else if (this.shouldTickBlocksAt(tickingblockentity.getPos())) {
2021-11-22 09:00:00 +11:00
tickingblockentity.tick();
2021-06-11 15:00:00 +10:00
}
diff --git a/src/main/java/org/spigotmc/SpigotWorldConfig.java b/src/main/java/org/spigotmc/SpigotWorldConfig.java
index 76aeff332..68cf01386 100644
--- a/src/main/java/org/spigotmc/SpigotWorldConfig.java
+++ b/src/main/java/org/spigotmc/SpigotWorldConfig.java
@@ -373,4 +373,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 000000000..23a39382b
--- /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;
+ }
+}
--
2023-06-08 01:30:00 +10:00
2.40.1