Skip to content

Commit

Permalink
some patches
Browse files Browse the repository at this point in the history
  • Loading branch information
AltronMaxX committed Jan 5, 2025
1 parent 285786b commit 88b1d6d
Show file tree
Hide file tree
Showing 7 changed files with 1,386 additions and 147 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: AltronMaxX <[email protected]>
Date: Sat, 4 Jan 2025 14:37:23 +0400
Subject: [PATCH] Pufferfish: Optimize random calls in chunk ticking


diff --git a/src/main/java/net/minecraft/server/level/ServerLevel.java b/src/main/java/net/minecraft/server/level/ServerLevel.java
index 0bbff929f55371e0f11369dd621b0b27b807e8fb..8f743959197fab8a516105b4ee11237bca3a5146 100644
--- a/src/main/java/net/minecraft/server/level/ServerLevel.java
+++ b/src/main/java/net/minecraft/server/level/ServerLevel.java
@@ -994,7 +994,7 @@ public class ServerLevel extends Level implements ServerEntityGetter, WorldGenLe
ProfilerFiller gameprofilerfiller = Profiler.get();

gameprofilerfiller.push("thunder");
- if (!this.paperConfig().environment.disableThunder && flag && this.isThundering() && this.spigotConfig.thunderChance > 0 && simpleRandom.nextInt(this.spigotConfig.thunderChance) == 0) { // Spigot // Paper - Option to disable thunder // Paper - optimise random ticking
+ if (!this.paperConfig().environment.disableThunder && flag && this.isThundering() && this.spigotConfig.thunderChance > 0 && chunk.shouldDoLightning(this.simpleRandom)) { // Spigot // Paper - Option to disable thunder // Paper - optimise random ticking // Pufferfish // Mizi
BlockPos blockposition = this.findLightningTargetAround(this.getBlockRandomPos(j, 0, k, 15));

if (this.isRainingAt(blockposition)) {
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 6003e3495e61073c39928918b9d9f4c2e20f3a49..644101f1a3518c2928522fd2295661970090be33 100644
--- a/src/main/java/net/minecraft/world/level/chunk/LevelChunk.java
+++ b/src/main/java/net/minecraft/world/level/chunk/LevelChunk.java
@@ -96,6 +96,18 @@ public class LevelChunk extends ChunkAccess implements ca.spottedleaf.moonrise.p
private final LevelChunkTicks<Fluid> fluidTicks;
private LevelChunk.UnsavedListener unsavedListener;

+ // Pufferfish start - instead of using a random every time the chunk is ticked, define when lightning strikes preemptively
+ private int lightningTick;
+ // shouldDoLightning compiles down to 29 bytes, which with the default of 35 byte inlining should guarantee an inline
+ public final boolean shouldDoLightning(net.minecraft.util.RandomSource random) {
+ if (this.lightningTick-- <= 0) {
+ this.lightningTick = random.nextInt(this.level.spigotConfig.thunderChance) << 1;
+ return true;
+ }
+ return false;
+ }
+ // Pufferfish end
+
public LevelChunk(Level world, ChunkPos pos) {
this(world, pos, UpgradeData.EMPTY, new LevelChunkTicks<>(), new LevelChunkTicks<>(), 0L, (LevelChunkSection[]) null, (LevelChunk.PostLoadProcessor) null, (BlendingData) null);
}
@@ -129,6 +141,8 @@ public class LevelChunk extends ChunkAccess implements ca.spottedleaf.moonrise.p
this.debug = !empty && this.level.isDebug();
this.defaultBlockState = empty ? VOID_AIR_BLOCKSTATE : AIR_BLOCKSTATE;
// Paper end - get block chunk optimisation
+
+ this.lightningTick = new java.util.Random().nextInt(100000) << 1; // Pufferfish - initialize lightning tick
}

// CraftBukkit start
77 changes: 77 additions & 0 deletions patches/server/0042-Lithium-fast-entity-retrieval.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: AltronMaxX <[email protected]>
Date: Sat, 4 Jan 2025 14:42:42 +0400
Subject: [PATCH] Lithium fast entity retrieval


diff --git a/src/main/java/net/minecraft/world/level/entity/EntitySectionStorage.java b/src/main/java/net/minecraft/world/level/entity/EntitySectionStorage.java
index af92a3ae38df4df02a5028d1d7568b674a60da51..9fade710ed998a29edda2791c78b134dc19f0a81 100644
--- a/src/main/java/net/minecraft/world/level/entity/EntitySectionStorage.java
+++ b/src/main/java/net/minecraft/world/level/entity/EntitySectionStorage.java
@@ -35,6 +35,8 @@ public class EntitySectionStorage<T extends EntityAccess> {
}

public void forEachAccessibleNonEmptySection(AABB box, AbortableIterationConsumer<EntitySection<T>> consumer) {
+ // Mizi start - lithium fast entity retrieval
+ /*
int i = SectionPos.posToSectionCoord(box.minX - 2.0);
int j = SectionPos.posToSectionCoord(box.minY - 4.0);
int k = SectionPos.posToSectionCoord(box.minZ - 2.0);
@@ -60,10 +62,57 @@ public class EntitySectionStorage<T extends EntityAccess> {
return;
}
}
+ */
+ int minX = SectionPos.posToSectionCoord(box.minX - 2.0D);
+ int minY = SectionPos.posToSectionCoord(box.minY - 4.0D);
+ int minZ = SectionPos.posToSectionCoord(box.minZ - 2.0D);
+ int maxX = SectionPos.posToSectionCoord(box.maxX + 2.0D);
+ int maxY = SectionPos.posToSectionCoord(box.maxY + 0.0D);
+ int maxZ = SectionPos.posToSectionCoord(box.maxZ + 2.0D);
+
+ if (maxX >= minX + 4 || maxZ >= minZ + 4) {
+ return;
+ }
+ for (int x = minX; x <= maxX; x++) {
+ for (int z = Math.max(minZ, 0); z <= maxZ; z++) {
+ if (this.forEachInColumn(x, minY, maxY, z, consumer).shouldAbort()) {
+ return;
+ }
+ }
+ int bound = Math.min(-1, maxZ);
+ for (int z = minZ; z <= bound; z++) {
+ if (this.forEachInColumn(x, minY, maxY, z, consumer).shouldAbort()) {
+ return;
+ }
}
}
}

+ private AbortableIterationConsumer.Continuation forEachInColumn(int x, int minY, int maxY, int z, AbortableIterationConsumer<EntitySection<T>> consumer) {
+ AbortableIterationConsumer.Continuation ret = AbortableIterationConsumer.Continuation.CONTINUE;
+ //y from negative to positive, but y is treated as unsigned
+ for (int y = Math.max(minY, 0); y <= maxY; y++) {
+ if ((ret = this.consumeSection(SectionPos.asLong(x, y, z), consumer)).shouldAbort()) {
+ return ret;
+ }
+ }
+ int bound = Math.min(-1, maxY);
+ for (int y = minY; y <= bound; y++) {
+ if ((ret = this.consumeSection(SectionPos.asLong(x, y, z), consumer)).shouldAbort()) {
+ return ret;
+ }
+ }
+ return ret;
+ }
+
+ private AbortableIterationConsumer.Continuation consumeSection(long sectionPos, AbortableIterationConsumer<EntitySection<T>> consumer) {
+ EntitySection<T> section = this.getSection(sectionPos);
+ if (section != null && 0 != section.size() && section.getStatus().isAccessible()) {
+ return consumer.accept(section);
+ }
+ return AbortableIterationConsumer.Continuation.CONTINUE;
+ } // Mizi end
+
public LongStream getExistingSectionPositionsInChunk(long chunkPos) {
int i = ChunkPos.getX(chunkPos);
int j = ChunkPos.getZ(chunkPos);
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: AltronMaxX <[email protected]>
Date: Sat, 4 Jan 2025 14:49:52 +0400
Subject: [PATCH] Pufferfish: Remove lambda from ticking guard


diff --git a/src/main/java/net/minecraft/server/level/ServerLevel.java b/src/main/java/net/minecraft/server/level/ServerLevel.java
index 8f743959197fab8a516105b4ee11237bca3a5146..36ef59f82f3e8bda974962fd2cddbc8b4b8c35f3 100644
--- a/src/main/java/net/minecraft/server/level/ServerLevel.java
+++ b/src/main/java/net/minecraft/server/level/ServerLevel.java
@@ -834,7 +834,21 @@ public class ServerLevel extends Level implements ServerEntityGetter, WorldGenLe
}

gameprofilerfiller.push("tick");
- this.guardEntityTick(this::tickNonPassenger, entity);
+ // this.guardEntityTick(this::tickNonPassenger, entity);
+ // Pufferfish start - copied from this.guardEntityTick
+ try {
+ this.tickNonPassenger(entity); // Pufferfish - changed
+ } catch (Throwable throwable) {
+ if (throwable instanceof ThreadDeath) throw throwable; // Paper
+ // Paper start - Prevent tile entity and entity crashes
+ final String msg = String.format("Entity threw exception at %s:%s,%s,%s", entity.level().getWorld().getName(), entity.getX(), entity.getY(), entity.getZ());
+ MinecraftServer.LOGGER.error(msg, throwable);
+ getCraftServer().getPluginManager().callEvent(new com.destroystokyo.paper.event.server.ServerExceptionEvent(new com.destroystokyo.paper.exception.ServerInternalException(msg, throwable)));
+ entity.discard(org.bukkit.event.entity.EntityRemoveEvent.Cause.DISCARD);
+ // Paper end
+ }
+ this.moonrise$midTickTasks(); // Paper - rewrite chunk system
+ // Pufferfish end
gameprofilerfiller.pop();
}
}
80 changes: 80 additions & 0 deletions patches/server/0044-Leaf-Remove-stream-in-Brain.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: AltronMaxX <[email protected]>
Date: Sat, 4 Jan 2025 14:52:34 +0400
Subject: [PATCH] Leaf: Remove stream in Brain


diff --git a/src/main/java/net/minecraft/world/entity/ai/Brain.java b/src/main/java/net/minecraft/world/entity/ai/Brain.java
index d4e6198fdfbefe54e374479a1f1d835ab98ce93a..23331820dfc947b4e4065d52f0832d592858a1f9 100644
--- a/src/main/java/net/minecraft/world/entity/ai/Brain.java
+++ b/src/main/java/net/minecraft/world/entity/ai/Brain.java
@@ -69,13 +69,22 @@ public class Brain<E extends LivingEntity> {
mutableObject.setValue(
(new MapCodec<Brain<E>>() {
public <T> Stream<T> keys(DynamicOps<T> dynamicOps) {
- return memoryModules.stream()
- .flatMap(
- memoryType -> memoryType.getCodec()
- .map(codec -> BuiltInRegistries.MEMORY_MODULE_TYPE.getKey((MemoryModuleType<?>)memoryType))
- .stream()
- )
- .map(id -> dynamicOps.createString(id.toString()));
+ // Leaf start - Remove stream in Brain
+ List<T> results = new java.util.ArrayList<>();
+
+ for (MemoryModuleType<?> memoryType : memoryModules) {
+ final Optional<?> codec = memoryType.getCodec();
+
+ if (codec.isPresent()) {
+ final net.minecraft.resources.ResourceLocation id = BuiltInRegistries.MEMORY_MODULE_TYPE.getKey(memoryType);
+ final T ops = dynamicOps.createString(id.toString());
+
+ results.add(ops);
+ }
+ }
+
+ return results.stream();
+ // Leaf end - Remove stream in Brain
}

public <T> DataResult<Brain<E>> decode(DynamicOps<T> dynamicOps, MapLike<T> mapLike) {
@@ -110,7 +119,8 @@ public class Brain<E extends LivingEntity> {
}

public <T> RecordBuilder<T> encode(Brain<E> brain, DynamicOps<T> dynamicOps, RecordBuilder<T> recordBuilder) {
- brain.memories().forEach(entry -> entry.serialize(dynamicOps, recordBuilder));
+ brain.serializeMemories(dynamicOps, recordBuilder); // Leaf - Remove stream in Brain
+
return recordBuilder;
}
})
@@ -152,8 +162,28 @@ public class Brain<E extends LivingEntity> {
}

Stream<Brain.MemoryValue<?>> memories() {
- return this.memories.entrySet().stream().map(entry -> Brain.MemoryValue.createUnchecked(entry.getKey(), entry.getValue()));
+ // Leaf start - Remove stream in Brain
+ return memoriesList().stream();
+ }
+
+ List<Brain.MemoryValue<?>> memoriesList() {
+ List<Brain.MemoryValue<?>> result = new java.util.ArrayList<>();
+
+ for (Entry<MemoryModuleType<?>, Optional<? extends ExpirableValue<?>>> entry : this.memories.entrySet()) {
+ result.add(Brain.MemoryValue.createUnchecked(entry.getKey(), entry.getValue()));
+ }
+
+ return result;
+ }
+
+ <T> void serializeMemories(DynamicOps<T> dynamicOps, RecordBuilder<T> recordBuilder) {
+ for (Entry<MemoryModuleType<?>, Optional<? extends ExpirableValue<?>>> entry : this.memories.entrySet()) {
+ final Brain.MemoryValue<?> result = Brain.MemoryValue.createUnchecked(entry.getKey(), entry.getValue());
+
+ result.serialize(dynamicOps, recordBuilder);
+ }
}
+ // Leaf end - Remove stream in Brain

public boolean hasMemoryValue(MemoryModuleType<?> type) {
return this.checkMemory(type, MemoryStatus.VALUE_PRESENT);
Loading

0 comments on commit 88b1d6d

Please sign in to comment.