9
0
mirror of https://github.com/Dreeam-qwq/Gale.git synced 2025-12-19 14:59:29 +00:00
Files
Gale/patches/server/0085-Optimize-world-generation-chunk-and-block-access.patch
2022-12-13 14:28:30 +01:00

202 lines
8.9 KiB
Diff

From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Martijn Muijsers <martijnmuijsers@live.nl>
Date: Thu, 1 Dec 2022 14:33:06 +0100
Subject: [PATCH] Optimize world generation chunk and block access
License: LGPL-3.0 (https://www.gnu.org/licenses/lgpl-3.0.html)
Gale - https://galemc.org
This patch is based on the following mixins and classes:
* "me/jellysquid/mods/lithium/common/util/Pos.java"
* "me/jellysquid/mods/lithium/mixin/gen/chunk_region/ChunkRegionMixin.java"
By: SuperCoder7979 <k.pranav@gmail.com>, 2No2Name <2No2Name@web.de>
As part of: Lithium (https://github.com/CaffeineMC/lithium-fabric)
Licensed under: LGPL-3.0 (https://www.gnu.org/licenses/lgpl-3.0.html)
diff --git a/src/main/java/me/jellysquid/mods/lithium/common/util/Pos.java b/src/main/java/me/jellysquid/mods/lithium/common/util/Pos.java
new file mode 100644
index 0000000000000000000000000000000000000000..f2f10b651e4fbecdd1ea1bc28ebf40d685a67cee
--- /dev/null
+++ b/src/main/java/me/jellysquid/mods/lithium/common/util/Pos.java
@@ -0,0 +1,95 @@
+// Gale - Lithium - position utility
+
+package me.jellysquid.mods.lithium.common.util;
+
+import net.minecraft.core.SectionPos;
+import net.minecraft.world.level.LevelHeightAccessor;
+
+public class Pos {
+
+ public static class BlockCoord {
+ public static int getYSize(LevelHeightAccessor view) {
+ return view.getHeight();
+ }
+ public static int getMinY(LevelHeightAccessor view) {
+ return view.getMinBuildHeight();
+ }
+ public static int getMaxYInclusive(LevelHeightAccessor view) {
+ return view.getMaxBuildHeight() - 1;
+ }
+ public static int getMaxYExclusive(LevelHeightAccessor view) {
+ return view.getMaxBuildHeight();
+ }
+
+ public static int getMaxInSectionCoord(int sectionCoord) {
+ return 15 + getMinInSectionCoord(sectionCoord);
+ }
+
+ public static int getMaxYInSectionIndex(LevelHeightAccessor view, int sectionIndex){
+ return getMaxInSectionCoord(SectionYCoord.fromSectionIndex(view, sectionIndex));
+ }
+
+ public static int getMinInSectionCoord(int sectionCoord) {
+ return SectionPos.sectionToBlockCoord(sectionCoord);
+ }
+
+ public static int getMinYInSectionIndex(LevelHeightAccessor view, int sectionIndex) {
+ return getMinInSectionCoord(SectionYCoord.fromSectionIndex(view, sectionIndex));
+ }
+ }
+
+ public static class ChunkCoord {
+ public static int fromBlockCoord(int blockCoord) {
+ return SectionPos.blockToSectionCoord(blockCoord);
+ }
+
+ public static int fromBlockSize(int i) {
+ return i >> 4; //same method as fromBlockCoord, just be clear about coord/size semantic difference
+ }
+ }
+
+ public static class SectionYCoord {
+ public static int getNumYSections(LevelHeightAccessor view) {
+ return view.getSectionsCount();
+ }
+ public static int getMinYSection(LevelHeightAccessor view) {
+ return view.getMinSection();
+ }
+ public static int getMaxYSectionInclusive(LevelHeightAccessor view) {
+ return view.getMaxSection() - 1;
+ }
+ public static int getMaxYSectionExclusive(LevelHeightAccessor view) {
+ return view.getMaxSection();
+ }
+
+ public static int fromSectionIndex(LevelHeightAccessor view, int sectionCoord) {
+ return sectionCoord + SectionYCoord.getMinYSection(view);
+ }
+ public static int fromBlockCoord(int blockCoord) {
+ return SectionPos.blockToSectionCoord(blockCoord);
+ }
+ }
+
+ public static class SectionYIndex {
+ public static int getNumYSections(LevelHeightAccessor view) {
+ return view.getSectionsCount();
+ }
+ public static int getMinYSectionIndex(LevelHeightAccessor view) {
+ return 0;
+ }
+ public static int getMaxYSectionIndexInclusive(LevelHeightAccessor view) {
+ return view.getSectionsCount() - 1;
+ }
+ public static int getMaxYSectionIndexExclusive(LevelHeightAccessor view) {
+ return view.getSectionsCount();
+ }
+
+ public static int fromSectionCoord(LevelHeightAccessor view, int sectionCoord) {
+ return sectionCoord - SectionYCoord.getMinYSection(view);
+ }
+ public static int fromBlockCoord(LevelHeightAccessor view, int blockCoord) {
+ return fromSectionCoord(view, SectionPos.blockToSectionCoord(blockCoord));
+ }
+ }
+
+}
diff --git a/src/main/java/net/minecraft/server/level/WorldGenRegion.java b/src/main/java/net/minecraft/server/level/WorldGenRegion.java
index c9e719e676ea26ee94673538db2dfd9ce5c957ae..0ee2cb850cd1039d8fe47b503027f0fcdbfe2daa 100644
--- a/src/main/java/net/minecraft/server/level/WorldGenRegion.java
+++ b/src/main/java/net/minecraft/server/level/WorldGenRegion.java
@@ -8,6 +8,7 @@ import java.util.concurrent.atomic.AtomicLong;
import java.util.function.Predicate;
import java.util.function.Supplier;
import javax.annotation.Nullable;
+
import net.minecraft.Util;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
@@ -83,6 +84,10 @@ public class WorldGenRegion implements WorldGenLevel {
private Supplier<String> currentlyGenerating;
private final AtomicLong subTickCount = new AtomicLong();
private static final ResourceLocation WORLDGEN_REGION_RANDOM = new ResourceLocation("worldgen_region_random");
+ // Gale start - Lithium - optimize world generation chunk and block access
+ private ChunkAccess[] chunksArr;
+ private int minChunkX, minChunkZ;
+ // Gale end - Lithium - optimize world generation chunk and block access
public WorldGenRegion(ServerLevel world, List<ChunkAccess> chunks, ChunkStatus status, int placementRadius) {
this.generatingStatus = status;
@@ -105,6 +110,11 @@ public class WorldGenRegion implements WorldGenLevel {
this.lastPos = ((ChunkAccess) chunks.get(chunks.size() - 1)).getPos();
this.structureManager = world.structureManager().forWorldGenRegion(this);
}
+ // Gale start - Lithium - optimize world generation chunk and block access
+ this.minChunkX = this.firstPos.x;
+ this.minChunkZ = this.firstPos.z;
+ this.chunksArr = chunks.toArray(new ChunkAccess[0]);
+ // Gale end - Lithium - optimize world generation chunk and block access
}
public boolean isOldChunkAround(ChunkPos chunkPos, int checkRadius) {
@@ -122,9 +132,29 @@ public class WorldGenRegion implements WorldGenLevel {
@Override
public ChunkAccess getChunk(int chunkX, int chunkZ) {
- return this.getChunk(chunkX, chunkZ, ChunkStatus.EMPTY);
+ // Gale start - Lithium - optimize world generation chunk and block access - use the chunk array for faster access
+ int x = chunkX - this.minChunkX;
+ int z = chunkZ - this.minChunkZ;
+ int w = this.size;
+
+ if (x >= 0 && z >= 0 && x < w && z < w) {
+ return this.chunksArr[x + z * w];
+ } else {
+ throw new NullPointerException("No chunk exists at " + new ChunkPos(chunkX, chunkZ));
+ }
+ // Gale end - Lithium - optimize world generation chunk and block access - use the chunk array for faster access
}
+ // Gale start - Lithium - optimize world generation chunk and block access
+ /**
+ * Use our chunk fetch function
+ */
+ public ChunkAccess getChunk(BlockPos pos) {
+ // Skip checking chunk.getStatus().isAtLeast(ChunkStatus.EMPTY) here, because it is always true
+ return this.getChunk(SectionPos.blockToSectionCoord(pos.getX()), SectionPos.blockToSectionCoord(pos.getZ()));
+ }
+ // Gale end - Lithium - optimize world generation chunk and block access
+
@Nullable
@Override
public ChunkAccess getChunk(int chunkX, int chunkZ, ChunkStatus leastStatus, boolean create) {
@@ -182,7 +212,17 @@ public class WorldGenRegion implements WorldGenLevel {
@Override
public BlockState getBlockState(BlockPos pos) {
- return this.getChunk(SectionPos.blockToSectionCoord(pos.getX()), SectionPos.blockToSectionCoord(pos.getZ())).getBlockState(pos);
+ // Gale start - Lithium - optimize world generation chunk and block access - avoid pointer de-referencing, make method easier to inline
+ int x = SectionPos.blockToSectionCoord(pos.getX()) - this.minChunkX;
+ int z = SectionPos.blockToSectionCoord(pos.getZ()) - this.minChunkZ;
+ int w = this.size;
+
+ if (x >= 0 && z >= 0 && x < w && z < w) {
+ return this.chunksArr[x + z * w].getBlockState(pos);
+ } else {
+ throw new NullPointerException("No chunk exists at " + new ChunkPos(pos));
+ }
+ // Gale end - Lithium - optimize world generation chunk and block access - avoid pointer de-referencing, make method easier to inline
}
@Override