9
0
mirror of https://github.com/SparklyPower/SparklyPaper.git synced 2025-12-19 15:09:27 +00:00
Files
SparklyPaperMC/patches/server/0014-Optimize-tickBlockEntities.patch
MrPowerGamerBR e371a3ef2a Update to 1.21.1
2024-08-19 15:58:08 -03:00

135 lines
8.2 KiB
Diff

From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: MrPowerGamerBR <git@mrpowergamerbr.com>
Date: Sun, 26 Nov 2023 13:11:10 -0300
Subject: [PATCH] Optimize tickBlockEntities
We cache the last `shouldTickBlocksAt` result, because the `shouldTickBlocksAt` is expensive because it requires pulling chunk holder info from an map for each block entity (even if the block entities are on the same chunk!) every single time. So, if the last chunk position is the same as our cached value, we use the last cached `shouldTickBlocksAt` result!
We could use a map for caching, but here's why this is way better than using a map: The block entity ticking list is sorted by chunks! Well, sort of... It is sorted by chunk when the chunk has been loaded, newly placed blocks will be appended to the end of the list until the chunk unloads and loads again. Most block entities are things that players placed to be there for a long time anyway (like hoppers, etc)
But here's the thing: We don't care if we have a small performance penalty if the players have placed new block entities, the small performance hit of when a player placed new block entities is so small ('tis just a long comparsion after all), that the performance boost from already placed block entities is bigger, this helps a lot if your server has a lot of chunks with multiple block entities, and the block entities will be automatically sorted after the chunk is unloaded and loaded again, so it ain't that bad.
And finally, we also cache the chunk's coordinate key when creating the block entity, which is actually "free" because we just reuse the already cached chunk coordinate key from the chunk!
diff --git a/src/main/java/net/minecraft/world/level/Level.java b/src/main/java/net/minecraft/world/level/Level.java
index 32b782bab750351caddafe6d6327e406bb26fb93..38d73f50360685a573740e811f186d7fa582003c 100644
--- a/src/main/java/net/minecraft/world/level/Level.java
+++ b/src/main/java/net/minecraft/world/level/Level.java
@@ -1387,6 +1387,10 @@ public abstract class Level implements LevelAccessor, AutoCloseable, ca.spottedl
int tickedEntities = 0; // Paper - rewrite chunk system
int tilesThisCycle = 0;
+ // SparklyPaper start - optimize tickBlockEntities
+ int shouldTickBlocksAtLastResult = -1; // -1 = undefined
+ long shouldTickBlocksAtChunkPos = 0L;
+ // SparklyPaper end
for (tileTickPosition = 0; tileTickPosition < this.blockEntityTickers.size(); tileTickPosition++) { // Paper - Disable tick limiters
this.tileTickPosition = (this.tileTickPosition < this.blockEntityTickers.size()) ? this.tileTickPosition : 0;
TickingBlockEntity tickingblockentity = (TickingBlockEntity) this.blockEntityTickers.get(this.tileTickPosition);
@@ -1397,13 +1401,25 @@ public abstract class Level implements LevelAccessor, AutoCloseable, ca.spottedl
tilesThisCycle--;
this.blockEntityTickers.markAsRemoved(this.tileTickPosition); // this.blockEntityTickers.remove(this.tileTickPosition--); // SparklyPaper - optimize block entity removals
// Spigot end
- } else if (flag && this.shouldTickBlocksAt(tickingblockentity.getPos())) {
+ // } else if (flag && this.shouldTickBlocksAt(tickingblockentity.getPos())) { // SparklyPaper start - optimize tickBlockEntities
+ } else if (flag) {
+ long chunkPos = tickingblockentity.getChunkCoordinateKey();
+ boolean shouldTick;
+ if (shouldTickBlocksAtChunkPos == chunkPos && shouldTickBlocksAtLastResult != -1) {
+ shouldTick = shouldTickBlocksAtLastResult == 1;
+ } else {
+ shouldTick = this.shouldTickBlocksAt(chunkPos);
+ shouldTickBlocksAtLastResult = shouldTick ? 1 : 0;
+ shouldTickBlocksAtChunkPos = chunkPos;
+ }
+ if (shouldTick) {
tickingblockentity.tick();
// Paper start - rewrite chunk system
if ((++tickedEntities & 7) == 0) {
((ca.spottedleaf.moonrise.patches.chunk_system.level.ChunkSystemLevel)(Level)(Object)this).moonrise$midTickTasks();
}
// Paper end - rewrite chunk system
+ } // SparklyPaper end
}
}
this.blockEntityTickers.removeMarkedEntries(); // SparklyPaper - optimize block entity removals
diff --git a/src/main/java/net/minecraft/world/level/block/entity/TickingBlockEntity.java b/src/main/java/net/minecraft/world/level/block/entity/TickingBlockEntity.java
index 28e3b73507b988f7234cbf29c4024c88180d0aef..427cf73383155c52bca8fb4b32f43029ff619833 100644
--- a/src/main/java/net/minecraft/world/level/block/entity/TickingBlockEntity.java
+++ b/src/main/java/net/minecraft/world/level/block/entity/TickingBlockEntity.java
@@ -10,4 +10,6 @@ public interface TickingBlockEntity {
BlockPos getPos();
String getType();
+
+ long getChunkCoordinateKey(); // SparklyPaper - optimize tickBlockEntities
}
diff --git a/src/main/java/net/minecraft/world/level/chunk/LevelChunk.java b/src/main/java/net/minecraft/world/level/chunk/LevelChunk.java
index 7c11853c5090fbc4fa5b3e73a69acf166158fdec..d7fe57e9a5aa59a7a9d611bcaa6152e72ec4c300 100644
--- a/src/main/java/net/minecraft/world/level/chunk/LevelChunk.java
+++ b/src/main/java/net/minecraft/world/level/chunk/LevelChunk.java
@@ -74,6 +74,13 @@ public class LevelChunk extends ChunkAccess implements ca.spottedleaf.moonrise.p
public String getType() {
return "<null>";
}
+
+ // SparklyPaper start - optimize tickBlockEntities
+ @Override
+ public long getChunkCoordinateKey() {
+ return 0;
+ }
+ // SparklyPaper end
};
private final Map<BlockPos, LevelChunk.RebindableTickingBlockEntityWrapper> tickersInLevel;
public boolean loaded;
@@ -952,7 +959,7 @@ public class LevelChunk extends ChunkAccess implements ca.spottedleaf.moonrise.p
}
private <T extends BlockEntity> TickingBlockEntity createTicker(T blockEntity, BlockEntityTicker<T> blockEntityTicker) {
- return new LevelChunk.BoundTickingBlockEntity<>(blockEntity, blockEntityTicker);
+ return new LevelChunk.BoundTickingBlockEntity<>(blockEntity, blockEntityTicker, this.coordinateKey); // SparklyPaper - optimize tickBlockEntities
}
@FunctionalInterface
@@ -1003,6 +1010,13 @@ public class LevelChunk extends ChunkAccess implements ca.spottedleaf.moonrise.p
public String toString() {
return String.valueOf(this.ticker) + " <wrapped>";
}
+
+ // SparklyPaper start - optimize tickBlockEntities
+ @Override
+ public long getChunkCoordinateKey() {
+ return this.ticker.getChunkCoordinateKey();
+ }
+ // SparklyPaper end
}
private class BoundTickingBlockEntity<T extends BlockEntity> implements TickingBlockEntity {
@@ -1010,10 +1024,12 @@ public class LevelChunk extends ChunkAccess implements ca.spottedleaf.moonrise.p
private final T blockEntity;
private final BlockEntityTicker<T> ticker;
private boolean loggedInvalidBlockState;
+ private final long chunkCoordinateKey; // SparklyPaper - optimize tickBlockEntities
- BoundTickingBlockEntity(final BlockEntity tileentity, final BlockEntityTicker blockentityticker) {
+ BoundTickingBlockEntity(final BlockEntity tileentity, final BlockEntityTicker blockentityticker, long chunkCoordinateKey) { // SparklyPaper - optimize tickBlockEntities
this.blockEntity = (T) tileentity; // CraftBukkit - decompile error
this.ticker = blockentityticker;
+ this.chunkCoordinateKey = chunkCoordinateKey; // SparklyPaper - optimize tickBlockEntities
}
@Override
@@ -1081,5 +1097,12 @@ public class LevelChunk extends ChunkAccess implements ca.spottedleaf.moonrise.p
return "Level ticker for " + s + "@" + String.valueOf(this.getPos());
}
+
+ // SparklyPaper start - optimize tickBlockEntities
+ @Override
+ public long getChunkCoordinateKey() {
+ return this.chunkCoordinateKey;
+ }
+ // SparklyPaper end
}
}