From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: MrHua269 Date: Fri, 9 Feb 2024 23:43:16 +0000 Subject: [PATCH] Gale Optimize world generation chunk and block access 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 8c455cda45ca2d724fbc140d53ffc7ecba3ca44c..dd63a219caf36acc58b73190e67faf9a504aef20 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 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 // Folia start - region threading @Override @@ -112,6 +117,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 } // Paper start - starlight @@ -150,9 +160,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) { @@ -210,7 +240,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