Skip to content

Commit

Permalink
Cache tracking range type enum ordinal
Browse files Browse the repository at this point in the history
Yes, I was shocked, flabbergasted even, when I found out that "Enum.ordinal()" was lagging here

But I guess it makes sense: It is a function that is called every tick for each entity, and it is a bit wasteful converting from enum to ordinal every time
  • Loading branch information
MrPowerGamerBR committed Nov 22, 2023
1 parent d03d4b9 commit 53dcccb
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 4 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ SparklyPaper's config file is `sparklypaper.yml`, the file is, by default, place
* The quintessential patch that other performance forks also have for... some reason??? I thought that this optimization was too funny to not do it in SparklyPaper.
* Caches when Bat's spooky season starts and ends, and when Skeleton and Zombies halloween starts and ends. The epoch is updated every 90 days. If your server is running for 90+ days straight without restarts, congratulations!
* Avoids unnecessary date checks, even tho that this shouldn't really improve performance that much... unless you have a lot of bats/zombies/skeletons spawning.
* Cache tracking range type enum ordinal
* Yes, I was shocked, flabbergasted even, when I found out that "Enum.ordinal()" was lagging here.
* But I guess it makes sense: It is a function that is called every tick for each entity, and it is a bit wasteful converting from enum to ordinal every time.
* Check how much MSPT (milliseconds per tick) each world is using in `/mspt`
* Useful to figure out which worlds are lagging your server.
![Per World MSPT](docs/per-world-mspt.png)
Expand Down
55 changes: 55 additions & 0 deletions patches/server/0014-Cache-tracking-range-type-enum-ordinal.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: MrPowerGamerBR <git@mrpowergamerbr.com>
Date: Wed, 22 Nov 2023 01:44:52 -0300
Subject: [PATCH] Cache tracking range type enum ordinal

Yes, I was shocked, flabbergasted even, when I found out that "Enum.ordinal()" was lagging here

But I guess it makes sense: It is a function that is called every tick for each entity, and it is a bit wasteful converting from enum to ordinal every time

diff --git a/src/main/java/net/minecraft/world/entity/Entity.java b/src/main/java/net/minecraft/world/entity/Entity.java
index e544081e8214802facb77defc1e9aa765834be2a..6d9aa481cf0a3c31505977b98ca5f2a6b812a757 100644
--- a/src/main/java/net/minecraft/world/entity/Entity.java
+++ b/src/main/java/net/minecraft/world/entity/Entity.java
@@ -501,6 +501,7 @@ public abstract class Entity implements Nameable, EntityAccess, CommandSource {
// Paper end
// Paper start - optimise entity tracking
final org.spigotmc.TrackingRange.TrackingRangeType trackingRangeType = org.spigotmc.TrackingRange.getTrackingRangeType(this);
+ final int trackingRangeTypeOrdinal = trackingRangeType.ordinal(); // SparklyPaper - cache tracking range type enum ordinal
// Paper start - make end portalling safe
public BlockPos portalBlock;
public ServerLevel portalWorld;
@@ -541,24 +542,26 @@ public abstract class Entity implements Nameable, EntityAccess, CommandSource {
public final com.destroystokyo.paper.util.misc.PooledLinkedHashSets.PooledObjectLinkedOpenHashSet<ServerPlayer> getPlayersInTrackRange() {
// determine highest range of passengers
if (this.passengers.isEmpty()) {
- return ((ServerLevel)this.level).getChunkSource().chunkMap.playerEntityTrackerTrackMaps[this.trackingRangeType.ordinal()]
+ return ((ServerLevel)this.level).getChunkSource().chunkMap.playerEntityTrackerTrackMaps[this.trackingRangeTypeOrdinal] // SparklyPaper - cache tracking range type enum ordinal
.getObjectsInRange(MCUtil.getCoordinateKey(this));
}
Iterable<Entity> passengers = this.getIndirectPassengers();
net.minecraft.server.level.ChunkMap chunkMap = ((ServerLevel)this.level).getChunkSource().chunkMap;
- org.spigotmc.TrackingRange.TrackingRangeType type = this.trackingRangeType;
- int range = chunkMap.getEntityTrackerRange(type.ordinal());
+ int typeOrdinal = this.trackingRangeTypeOrdinal; // SparklyPaper - cache tracking range type enum ordinal
+ int range = chunkMap.getEntityTrackerRange(typeOrdinal); // SparklyPaper - cache tracking range type enum ordinal

for (Entity passenger : passengers) {
- org.spigotmc.TrackingRange.TrackingRangeType passengerType = passenger.trackingRangeType;
- int passengerRange = chunkMap.getEntityTrackerRange(passengerType.ordinal());
+ // SparklyPaper start - cache tracking range type enum ordinal
+ int passengerTypeOrdinal = passenger.trackingRangeTypeOrdinal;
+ int passengerRange = chunkMap.getEntityTrackerRange(passengerTypeOrdinal);
if (passengerRange > range) {
- type = passengerType;
+ typeOrdinal = passengerTypeOrdinal; // SparklyPaper - cache tracking range type enum ordinal
range = passengerRange;
}
+ // SparklyPaper end
}

- return chunkMap.playerEntityTrackerTrackMaps[type.ordinal()].getObjectsInRange(MCUtil.getCoordinateKey(this));
+ return chunkMap.playerEntityTrackerTrackMaps[typeOrdinal].getObjectsInRange(MCUtil.getCoordinateKey(this)); // SparklyPaper - cache tracking range type enum ordinal
}
// Paper end - optimise entity tracking

Original file line number Diff line number Diff line change
Expand Up @@ -1083,10 +1083,10 @@ index 33abcf12b4426572b74ca4c813e4392c823494bc..07198f2f8f7cb082c9e575a5c1e56c14

entityplayer1.connection = entityplayer.connection;
diff --git a/src/main/java/net/minecraft/world/entity/Entity.java b/src/main/java/net/minecraft/world/entity/Entity.java
index e544081e8214802facb77defc1e9aa765834be2a..f979d22f5bf83492133a87119686a4e136923bc0 100644
index 6d9aa481cf0a3c31505977b98ca5f2a6b812a757..90efb34aeb8ab4718f34fd68cb9db3c006dc912f 100644
--- a/src/main/java/net/minecraft/world/entity/Entity.java
+++ b/src/main/java/net/minecraft/world/entity/Entity.java
@@ -934,11 +934,11 @@ public abstract class Entity implements Nameable, EntityAccess, CommandSource {
@@ -937,11 +937,11 @@ public abstract class Entity implements Nameable, EntityAccess, CommandSource {
// This will be called every single tick the entity is in lava, so don't throw an event
this.setSecondsOnFire(15, false);
}
Expand All @@ -1100,7 +1100,7 @@ index e544081e8214802facb77defc1e9aa765834be2a..f979d22f5bf83492133a87119686a4e1
// CraftBukkit end - we also don't throw an event unless the object in lava is living, to save on some event calls

}
@@ -3390,9 +3390,9 @@ public abstract class Entity implements Nameable, EntityAccess, CommandSource {
@@ -3393,9 +3393,9 @@ public abstract class Entity implements Nameable, EntityAccess, CommandSource {
if (this.fireImmune()) {
return;
}
Expand All @@ -1112,7 +1112,7 @@ index e544081e8214802facb77defc1e9aa765834be2a..f979d22f5bf83492133a87119686a4e1
return;
}
// CraftBukkit end
@@ -3903,6 +3903,7 @@ public abstract class Entity implements Nameable, EntityAccess, CommandSource {
@@ -3906,6 +3906,7 @@ public abstract class Entity implements Nameable, EntityAccess, CommandSource {
this.teleportPassengers();
this.setYHeadRot(yaw);
} else {
Expand Down

0 comments on commit 53dcccb

Please sign in to comment.