Skip to content

Commit

Permalink
Patches!!!
Browse files Browse the repository at this point in the history
  • Loading branch information
Owen1212055 committed Dec 6, 2023
1 parent 46be245 commit 25dc4af
Show file tree
Hide file tree
Showing 53 changed files with 227 additions and 219 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,34 +7,33 @@ Plugins leaking scoreboards will make this very expensive,
let server owners debug it easily

diff --git a/src/main/java/co/aikar/timings/MinecraftTimings.java b/src/main/java/co/aikar/timings/MinecraftTimings.java
index 4d861f9a58f8ea238471af22f387854d855b1801..678a31fb85947ee8807d02d0fe4e11a73d2dafaa 100644
index 4d861f9a58f8ea238471af22f387854d855b1801..e8aad2eecd8263118e3eca9e2be84a00277bc15e 100644
--- a/src/main/java/co/aikar/timings/MinecraftTimings.java
+++ b/src/main/java/co/aikar/timings/MinecraftTimings.java
@@ -46,6 +46,8 @@ public final class MinecraftTimings {
@@ -46,6 +46,7 @@ public final class MinecraftTimings {

public static final Timing antiXrayUpdateTimer = Timings.ofSafe("anti-xray - update");
public static final Timing antiXrayObfuscateTimer = Timings.ofSafe("anti-xray - obfuscate");
+ public static final Timing scoreboardScoreSearch = Timings.ofSafe("Scoreboard score search"); // Paper - add timings for scoreboard search
+

private static final Map<Class<?>, String> taskNameCache = new MapMaker().weakKeys().makeMap();

diff --git a/src/main/java/org/bukkit/craftbukkit/scoreboard/CraftScoreboardManager.java b/src/main/java/org/bukkit/craftbukkit/scoreboard/CraftScoreboardManager.java
index f06f011375b9be307a858ccb6579f74942b1d325..24c5e6e3dd41e72d22819964bc8e77f0ebc2d089 100644
index c7ca6210d6ae37fe95068c9baa5fb654f95307e0..cad42a0f3c016bf65181e50d139ae4e2fb9158a5 100644
--- a/src/main/java/org/bukkit/craftbukkit/scoreboard/CraftScoreboardManager.java
+++ b/src/main/java/org/bukkit/craftbukkit/scoreboard/CraftScoreboardManager.java
@@ -113,9 +113,18 @@ public final class CraftScoreboardManager implements ScoreboardManager {

// CraftBukkit method
public void getScoreboardScores(ObjectiveCriteria criteria, String name, Consumer<Score> consumer) {
public void forAllObjectives(ObjectiveCriteria criteria, ScoreHolder holder, Consumer<ScoreAccess> consumer) {
+ // Paper start - add timings for scoreboard search
+ // plugins leaking scoreboards will make this very expensive, let server owners debug it easily
+ co.aikar.timings.MinecraftTimings.scoreboardScoreSearch.startTimingIfSync();
+ try {
+ // Paper end - add timings for scoreboard search
for (CraftScoreboard scoreboard : this.scoreboards) {
Scoreboard board = scoreboard.board;
board.forAllObjectives(criteria, name, (score) -> consumer.accept(score));
board.forAllObjectives(criteria, holder, (score) -> consumer.accept(score));
}
+ } finally { // Paper start - add timings for scoreboard search
+ co.aikar.timings.MinecraftTimings.scoreboardScoreSearch.stopTimingIfSync();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,35 +6,26 @@ Subject: [PATCH] Execute chunk tasks mid-tick
This will help the server load chunks if tick times are high.

diff --git a/src/main/java/co/aikar/timings/MinecraftTimings.java b/src/main/java/co/aikar/timings/MinecraftTimings.java
index 678a31fb85947ee8807d02d0fe4e11a73d2dafaa..0fc4feb00940079c4eb9554a3a48d19cbbeeb7bc 100644
index e8aad2eecd8263118e3eca9e2be84a00277bc15e..190f6727351bc1d4e071da095d2f6365de095379 100644
--- a/src/main/java/co/aikar/timings/MinecraftTimings.java
+++ b/src/main/java/co/aikar/timings/MinecraftTimings.java
@@ -49,6 +49,8 @@ public final class MinecraftTimings {
@@ -48,6 +48,8 @@ public final class MinecraftTimings {
public static final Timing antiXrayObfuscateTimer = Timings.ofSafe("anti-xray - obfuscate");
public static final Timing scoreboardScoreSearch = Timings.ofSafe("Scoreboard score search"); // Paper - add timings for scoreboard search


+ public static final Timing midTickChunkTasks = Timings.ofSafe("Mid Tick Chunk Tasks");
+
private static final Map<Class<?>, String> taskNameCache = new MapMaker().weakKeys().makeMap();

private MinecraftTimings() {}
diff --git a/src/main/java/net/minecraft/server/MinecraftServer.java b/src/main/java/net/minecraft/server/MinecraftServer.java
index d74313b58b660f3c03b8a6db4f80b6b073b6b795..f7c1d07c95f7b67e32bd6679af88612aec74f54f 100644
index 6f17e5631aace53f75e6badf514dbb77c50065b9..0a23cfeaba071c2179de1931caa86bc46462c338 100644
--- a/src/main/java/net/minecraft/server/MinecraftServer.java
+++ b/src/main/java/net/minecraft/server/MinecraftServer.java
@@ -1285,6 +1285,7 @@ public abstract class MinecraftServer extends ReentrantBlockableEventLoop<TickTa

private boolean pollTaskInternal() {
if (super.pollTask()) {
+ this.executeMidTickTasks(); // Paper - execute chunk tasks mid tick
return true;
} else {
if (this.haveTime()) {
@@ -2704,4 +2705,74 @@ public abstract class MinecraftServer extends ReentrantBlockableEventLoop<TickTa
}
@@ -1347,8 +1347,79 @@ public abstract class MinecraftServer extends ReentrantBlockableEventLoop<TickTa
return flag;
}
// Paper end
+

+ // Paper start - execute chunk tasks mid tick
+ static final long CHUNK_TASK_QUEUE_BACKOFF_MIN_TIME = 25L * 1000L; // 25us
+ static final long MAX_CHUNK_EXEC_TIME = 1000L; // 1us
Expand Down Expand Up @@ -104,48 +95,54 @@ index d74313b58b660f3c03b8a6db4f80b6b073b6b795..f7c1d07c95f7b67e32bd6679af88612a
+ }
+ }
+ // Paper end - execute chunk tasks mid tick
}
+
private boolean pollTaskInternal() {
if (super.pollTask()) {
+ this.executeMidTickTasks(); // Paper - execute chunk tasks mid tick
return true;
} else {
if (this.tickRateManager.isSprinting() || this.haveTime()) {
diff --git a/src/main/java/net/minecraft/server/level/ServerChunkCache.java b/src/main/java/net/minecraft/server/level/ServerChunkCache.java
index 4e58f31e4852a0811f85596653f6db630a75b069..8907396dc21dbfb31ff15286a7daa2110f6e34c0 100644
index e725dd92ae15f62b078bda3572d0663468f37939..1f7586b71c579d48de32a307c0ce543130f0851b 100644
--- a/src/main/java/net/minecraft/server/level/ServerChunkCache.java
+++ b/src/main/java/net/minecraft/server/level/ServerChunkCache.java
@@ -571,6 +571,7 @@ public class ServerChunkCache extends ChunkSource {
// Paper end
Iterator iterator1 = list.iterator();
@@ -566,6 +566,7 @@ public class ServerChunkCache extends ChunkSource {
boolean flag1 = this.level.ticksPerSpawnCategory.getLong(org.bukkit.entity.SpawnCategory.ANIMAL) != 0L && this.level.getLevelData().getGameTime() % this.level.ticksPerSpawnCategory.getLong(org.bukkit.entity.SpawnCategory.ANIMAL) == 0L; // CraftBukkit
Iterator iterator1 = list.iterator();

+ int chunksTicked = 0; // Paper
while (iterator1.hasNext()) {
ServerChunkCache.ChunkAndHolder chunkproviderserver_a = (ServerChunkCache.ChunkAndHolder) iterator1.next();
LevelChunk chunk1 = chunkproviderserver_a.chunk;
@@ -584,6 +585,7 @@ public class ServerChunkCache extends ChunkSource {

if (this.level.shouldTickBlocksAt(chunkcoordintpair.toLong())) {
this.level.tickChunk(chunk1, k);
+ if ((chunksTicked++ & 1) == 0) net.minecraft.server.MinecraftServer.getServer().executeMidTickTasks(); // Paper
+ int chunksTicked = 0; // Paper
while (iterator1.hasNext()) {
ServerChunkCache.ChunkAndHolder chunkproviderserver_a = (ServerChunkCache.ChunkAndHolder) iterator1.next();
LevelChunk chunk1 = chunkproviderserver_a.chunk;
@@ -580,6 +581,7 @@ public class ServerChunkCache extends ChunkSource {
if (this.level.shouldTickBlocksAt(chunkcoordintpair.toLong())) {
this.level.timings.doTickTiles.startTiming(); // Spigot
this.level.tickChunk(chunk1, l);
+ if ((chunksTicked++ & 1) == 0) net.minecraft.server.MinecraftServer.getServer().executeMidTickTasks(); // Paper
this.level.timings.doTickTiles.stopTiming(); // Spigot
}
}
}
}
diff --git a/src/main/java/net/minecraft/server/level/ServerLevel.java b/src/main/java/net/minecraft/server/level/ServerLevel.java
index d65e3cfeb8c6e7d26c03af8870d328c2f4094541..8436a2a517c3cff92d017bb3bc4acbd2e590597e 100644
index 938a07d2e90ea2516b064d930b38e8908be91055..81381a4b401046b2ea76627814c740dca73b9592 100644
--- a/src/main/java/net/minecraft/server/level/ServerLevel.java
+++ b/src/main/java/net/minecraft/server/level/ServerLevel.java
@@ -215,6 +215,7 @@ public class ServerLevel extends Level implements WorldGenLevel {
@@ -217,6 +217,7 @@ public class ServerLevel extends Level implements WorldGenLevel {
private final StructureCheck structureCheck;
private final boolean tickTime;
private final RandomSequences randomSequences;
+ public long lastMidTickExecuteFailure; // Paper - execute chunk tasks mid tick

// CraftBukkit start
public final LevelStorageSource.LevelStorageAccess convertable;
@@ -1187,6 +1188,7 @@ public class ServerLevel extends Level implements WorldGenLevel {
@@ -1206,6 +1207,7 @@ public class ServerLevel extends Level implements WorldGenLevel {
if (fluid1.is(fluid)) {
fluid1.tick(this, pos);
}
+ MinecraftServer.getServer().executeMidTickTasks(); // Paper - exec chunk tasks during world tick

}

@@ -1196,6 +1198,7 @@ public class ServerLevel extends Level implements WorldGenLevel {
@@ -1215,6 +1217,7 @@ public class ServerLevel extends Level implements WorldGenLevel {
if (iblockdata.is(block)) {
iblockdata.tick(this, pos, this.random);
}
Expand All @@ -154,12 +151,12 @@ index d65e3cfeb8c6e7d26c03af8870d328c2f4094541..8436a2a517c3cff92d017bb3bc4acbd2
}

diff --git a/src/main/java/net/minecraft/world/level/Level.java b/src/main/java/net/minecraft/world/level/Level.java
index 6bd9b680fa1e84d058ada2354fa6a5b876185dc4..795be71432c4d834004bcfb70a8d1927cf22bfda 100644
index 58394bbf228f96f717e5318cd016caf9d2f09343..c6b590381b8464ff9d7410b833889af5238e206a 100644
--- a/src/main/java/net/minecraft/world/level/Level.java
+++ b/src/main/java/net/minecraft/world/level/Level.java
@@ -918,6 +918,11 @@ public abstract class Level implements LevelAccessor, AutoCloseable {
@@ -929,6 +929,11 @@ public abstract class Level implements LevelAccessor, AutoCloseable {
// Spigot end
} else if (this.shouldTickBlocksAt(tickingblockentity.getPos())) {
} else if (flag && this.shouldTickBlocksAt(tickingblockentity.getPos())) {
tickingblockentity.tick();
+ // Paper start - execute chunk tasks during tick
+ if ((this.tileTickPosition & 7) == 0) {
Expand All @@ -169,7 +166,7 @@ index 6bd9b680fa1e84d058ada2354fa6a5b876185dc4..795be71432c4d834004bcfb70a8d1927
}
}
this.blockEntityTickers.removeAll(toRemove);
@@ -932,6 +937,7 @@ public abstract class Level implements LevelAccessor, AutoCloseable {
@@ -943,6 +948,7 @@ public abstract class Level implements LevelAccessor, AutoCloseable {
public <T extends Entity> void guardEntityTick(Consumer<T> tickConsumer, T entity) {
try {
tickConsumer.accept(entity);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ Apparently the abstract block iteration was taking about
75% of the method call.

diff --git a/src/main/java/net/minecraft/world/level/block/FarmBlock.java b/src/main/java/net/minecraft/world/level/block/FarmBlock.java
index eb6056c5051e5e8a872a10dc993dbd5232787663..502dcba14da9d3dcefc61fdc349a4e1e1d94b478 100644
index 6c1e8ba518c883aa5c079b4c94f068833609acc3..59bbdead2ebd8965d222540c7243dde051bbcc4b 100644
--- a/src/main/java/net/minecraft/world/level/block/FarmBlock.java
+++ b/src/main/java/net/minecraft/world/level/block/FarmBlock.java
@@ -144,19 +144,27 @@ public class FarmBlock extends Block {
@@ -151,19 +151,27 @@ public class FarmBlock extends Block {
}

private static boolean isNearWater(LevelReader world, BlockPos pos) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,10 @@ index 0000000000000000000000000000000000000000..7d93652c1abbb6aee6eb7c26cf35d4d0
+ }
+}
diff --git a/src/main/java/net/minecraft/server/level/ServerLevel.java b/src/main/java/net/minecraft/server/level/ServerLevel.java
index 8436a2a517c3cff92d017bb3bc4acbd2e590597e..1c6cd0671b501dde08cace07043565efa4c740f1 100644
index 81381a4b401046b2ea76627814c740dca73b9592..8144c2f519876a3e33150e4fbebea3aadaf5ce31 100644
--- a/src/main/java/net/minecraft/server/level/ServerLevel.java
+++ b/src/main/java/net/minecraft/server/level/ServerLevel.java
@@ -839,6 +839,10 @@ public class ServerLevel extends Level implements WorldGenLevel {
@@ -856,6 +856,10 @@ public class ServerLevel extends Level implements WorldGenLevel {
entityplayer.stopSleepInBed(false, false);
});
}
Expand All @@ -104,7 +104,7 @@ index 8436a2a517c3cff92d017bb3bc4acbd2e590597e..1c6cd0671b501dde08cace07043565ef

public void tickChunk(LevelChunk chunk, int randomTickSpeed) {
ChunkPos chunkcoordintpair = chunk.getPos();
@@ -848,8 +852,10 @@ public class ServerLevel extends Level implements WorldGenLevel {
@@ -865,8 +869,10 @@ public class ServerLevel extends Level implements WorldGenLevel {
ProfilerFiller gameprofilerfiller = this.getProfiler();

gameprofilerfiller.push("thunder");
Expand All @@ -116,18 +116,19 @@ index 8436a2a517c3cff92d017bb3bc4acbd2e590597e..1c6cd0671b501dde08cace07043565ef

if (this.isRainingAt(blockposition)) {
DifficultyInstance difficultydamagescaler = this.getCurrentDifficultyAt(blockposition);
@@ -881,61 +887,68 @@ public class ServerLevel extends Level implements WorldGenLevel {
@@ -898,7 +904,10 @@ public class ServerLevel extends Level implements WorldGenLevel {
if (!this.paperConfig().environment.disableIceAndSnow) { // Paper
for (int l = 0; l < randomTickSpeed; ++l) {
if (this.random.nextInt(48) == 0) {
- this.tickIceAndSnow(flag, this.getBlockRandomPos(j, 0, k, 15));
- this.tickPrecipitation(this.getBlockRandomPos(j, 0, k, 15));
+ // Paper start
+ this.getRandomBlockPosition(j, 0, k, 15, blockposition);
+ this.tickIceAndSnow(flag, blockposition, chunk);
+ this.tickPrecipitation(blockposition, chunk);
+ // Paper end
}
}
} // Paper

+ // Paper start - optimise random block ticking
@@ -906,54 +915,60 @@ public class ServerLevel extends Level implements WorldGenLevel {
gameprofilerfiller.popPush("tickBlocks");
timings.chunkTicksBlocks.startTiming(); // Paper
if (randomTickSpeed > 0) {
Expand All @@ -149,6 +150,7 @@ index 8436a2a517c3cff92d017bb3bc4acbd2e590597e..1c6cd0671b501dde08cace07043565ef
- if (iblockdata.isRandomlyTicking()) {
- iblockdata.randomTick(this, blockposition1, this.random);
- }
+ // Paper start - optimize random block ticking
+ LevelChunkSection[] sections = chunk.getSections();
+ final int minSection = io.papermc.paper.util.WorldUtil.getMinSection(this);
+ for (int sectionIndex = 0; sectionIndex < sections.length; sectionIndex++) {
Expand Down Expand Up @@ -179,9 +181,9 @@ index 8436a2a517c3cff92d017bb3bc4acbd2e590597e..1c6cd0671b501dde08cace07043565ef
- gameprofilerfiller.pop();
- }
+ iblockdata.randomTick(this, blockposition2, this.randomTickRandom);
+ // We drop the fluid tick since LAVA is ALREADY TICKED by the above method (See LiquidBlock).
+ // TODO CHECK ON UPDATE (ping the Canadian)
}
+ // We drop the fluid tick since LAVA is ALREADY TICKED by the above method (See LiquidBlock).
+ // TODO CHECK ON UPDATE (ping the Canadian)
}
}
+ // Paper end - optimise random block ticking
Expand All @@ -190,14 +192,16 @@ index 8436a2a517c3cff92d017bb3bc4acbd2e590597e..1c6cd0671b501dde08cace07043565ef
gameprofilerfiller.pop();
}

- private void tickIceAndSnow(boolean raining, BlockPos pos) {
@VisibleForTesting
- public void tickPrecipitation(BlockPos pos) {
- BlockPos blockposition1 = this.getHeightmapPos(Heightmap.Types.MOTION_BLOCKING, pos);
- BlockPos blockposition2 = blockposition1.below();
+ private void tickIceAndSnow(boolean raining, BlockPos.MutableBlockPos blockposition1, final LevelChunk chunk) { // Paper - optimise chunk ticking
+ public void tickPrecipitation(BlockPos.MutableBlockPos blockposition1, final LevelChunk chunk) { // Paper - optimise chunk ticking
+ // Paper start - optimise chunk ticking
+ int normalY = chunk.getHeight(Heightmap.Types.MOTION_BLOCKING, blockposition1.getX() & 15, blockposition1.getZ() & 15) + 1;
+ int downY = normalY - 1;
+ blockposition1.setY(normalY);
+ // Paper end - optimise chunk ticking
Biome biomebase = (Biome) this.getBiome(blockposition1).value();

- if (biomebase.shouldFreeze(this, blockposition2)) {
Expand All @@ -206,16 +210,15 @@ index 8436a2a517c3cff92d017bb3bc4acbd2e590597e..1c6cd0671b501dde08cace07043565ef
+ if (biomebase.shouldFreeze(this, blockposition1)) {
+ org.bukkit.craftbukkit.event.CraftEventFactory.handleBlockFormEvent(this, blockposition1, Blocks.ICE.defaultBlockState(), null); // CraftBukkit
}
+ // Paper end - optimise chunk ticking

if (raining) {
if (this.isRaining()) {
int i = this.getGameRules().getInt(GameRules.RULE_SNOW_ACCUMULATION_HEIGHT);

+ blockposition1.setY(normalY); // Paper - optimise chunk ticking
if (i > 0 && biomebase.shouldSnow(this, blockposition1)) {
BlockState iblockdata = this.getBlockState(blockposition1);

@@ -953,12 +966,13 @@ public class ServerLevel extends Level implements WorldGenLevel {
@@ -971,12 +986,13 @@ public class ServerLevel extends Level implements WorldGenLevel {
}
}

Expand Down Expand Up @@ -306,7 +309,7 @@ index f0f764f36fb92c64ab2dc8a0a50c3f48321c3c9a..311625277a26c9c187025a1036978229
public void getAll(IntConsumer action) {
for(int i = 0; i < this.size; ++i) {
diff --git a/src/main/java/net/minecraft/world/entity/animal/Turtle.java b/src/main/java/net/minecraft/world/entity/animal/Turtle.java
index 652a8150f7343050b6da6c01f4e73a755138d491..5f57f13d50e051c621e401b63c0b55b3a2a73fa3 100644
index 722e323d2ae3f4ac8aa3197fed79f7d926d5ccfa..f8c1dfc8e121a406955348a1ec00c264d73e73f7 100644
--- a/src/main/java/net/minecraft/world/entity/animal/Turtle.java
+++ b/src/main/java/net/minecraft/world/entity/animal/Turtle.java
@@ -87,7 +87,7 @@ public class Turtle extends Animal {
Expand All @@ -319,10 +322,10 @@ index 652a8150f7343050b6da6c01f4e73a755138d491..5f57f13d50e051c621e401b63c0b55b3

public BlockPos getHomePos() {
diff --git a/src/main/java/net/minecraft/world/level/Level.java b/src/main/java/net/minecraft/world/level/Level.java
index 795be71432c4d834004bcfb70a8d1927cf22bfda..5e7a53d1cae4c94a08fb89d7599d183de21cdfaa 100644
index c6b590381b8464ff9d7410b833889af5238e206a..fe263132ed204dbd1b939d92dc16eec40162fb76 100644
--- a/src/main/java/net/minecraft/world/level/Level.java
+++ b/src/main/java/net/minecraft/world/level/Level.java
@@ -1380,10 +1380,18 @@ public abstract class Level implements LevelAccessor, AutoCloseable {
@@ -1400,10 +1400,18 @@ public abstract class Level implements LevelAccessor, AutoCloseable {
public abstract RecipeManager getRecipeManager();

public BlockPos getBlockRandomPos(int x, int y, int z, int l) {
Expand Down
Loading

0 comments on commit 25dc4af

Please sign in to comment.