9
0
mirror of https://github.com/Winds-Studio/Leaf.git synced 2025-12-19 15:09:25 +00:00
Files
Leaf/leaf-server/minecraft-patches/features/0161-Reduce-worldgen-allocations.patch
2025-06-28 10:37:38 +08:00

102 lines
5.2 KiB
Diff

From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: HaHaWTH <102713261+HaHaWTH@users.noreply.github.com>
Date: Fri, 14 Jun 2024 23:19:55 +0800
Subject: [PATCH] Reduce worldgen allocations
This change optimizes the way SurfaceRules update their biome supplier,avoiding unnecessary object creations and thus reducing memory allocations
during world generation. The update method now reuses the existing PositionalBiomeGetter object if it's already present, otherwise it
initializes a new one.
Additionally, the tryApply method in SurfaceRules now avoids iterator
allocation by directly accessing the rules list, which further contributes
to reducing garbage collection pressure during world generation.
diff --git a/net/minecraft/world/level/levelgen/NoiseChunk.java b/net/minecraft/world/level/levelgen/NoiseChunk.java
index f861f9e087182470a3bbb22678dbdacb8a73e943..a3d0d17178eedfaef83e2e0df6b1c2d7784d8656 100644
--- a/net/minecraft/world/level/levelgen/NoiseChunk.java
+++ b/net/minecraft/world/level/levelgen/NoiseChunk.java
@@ -362,7 +362,17 @@ public class NoiseChunk implements DensityFunction.ContextProvider, DensityFunct
}
protected DensityFunction wrap(DensityFunction densityFunction) {
- return this.wrapped.computeIfAbsent(densityFunction, this::wrapNew);
+ // Leaf start - Reduce worldgen allocations
+ // Avoid lambda allocation
+ DensityFunction func = this.wrapped.get(densityFunction);
+
+ if (func == null) {
+ func = this.wrapNew(densityFunction);
+ this.wrapped.put(densityFunction, func);
+ }
+
+ return func;
+ // Leaf end - Reduce worldgen allocations
}
private DensityFunction wrapNew(DensityFunction densityFunction) {
diff --git a/net/minecraft/world/level/levelgen/SurfaceRules.java b/net/minecraft/world/level/levelgen/SurfaceRules.java
index 0948c8db90605a15a043b5c5bc74edecd7f9db1b..6cba88415a4715527e163e54662db9b3ab37c747 100644
--- a/net/minecraft/world/level/levelgen/SurfaceRules.java
+++ b/net/minecraft/world/level/levelgen/SurfaceRules.java
@@ -313,8 +313,15 @@ public class SurfaceRules {
}
protected void updateY(int stoneDepthAbove, int stoneDepthBelow, int waterHeight, int blockX, int blockY, int blockZ) {
- this.lastUpdateY++;
- this.biome = Suppliers.memoize(() -> this.biomeGetter.apply(this.pos.set(blockX, blockY, blockZ)));
+ // Leaf start - Reduce worldgen allocations
+ // Reuse supplier object instead of creating new ones every time
+ ++this.lastUpdateY;
+ Supplier<Holder<Biome>> getter = this.biome;
+ if (getter == null) {
+ this.biome = getter = new org.dreeam.leaf.world.biome.PositionalBiomeGetter(this.biomeGetter, this.pos);
+ }
+ ((org.dreeam.leaf.world.biome.PositionalBiomeGetter) getter).update(blockX, blockY, blockZ);
+ // Leaf end - Reduce worldgen allocations
this.blockY = blockY;
this.waterHeight = waterHeight;
this.stoneDepthBelow = stoneDepthBelow;
@@ -582,8 +589,13 @@ public class SurfaceRules {
@Nullable
@Override
public BlockState tryApply(int x, int y, int z) {
- for (SurfaceRules.SurfaceRule surfaceRule : this.rules) {
- BlockState blockState = surfaceRule.tryApply(x, y, z);
+ // Leaf start - Reduce worldgen allocations
+ // Avoid iterator allocation
+ int size = this.rules.size();
+ //noinspection ForLoopReplaceableByForEach
+ for (int i = 0; i < size; i++) {
+ BlockState blockState = this.rules.get(i).tryApply(x, y, z);
+ // Leaf end - Reduce worldgen allocations
if (blockState != null) {
return blockState;
}
diff --git a/net/minecraft/world/level/levelgen/material/MaterialRuleList.java b/net/minecraft/world/level/levelgen/material/MaterialRuleList.java
index 1605cc013d5a89a5d3cb68365bdcc18e2dd0a921..a3b5f74b5f9a0f4e62dee67e50f51e9e6b78d7fd 100644
--- a/net/minecraft/world/level/levelgen/material/MaterialRuleList.java
+++ b/net/minecraft/world/level/levelgen/material/MaterialRuleList.java
@@ -9,13 +9,17 @@ public record MaterialRuleList(NoiseChunk.BlockStateFiller[] materialRuleList) i
@Nullable
@Override
public BlockState calculate(DensityFunction.FunctionContext context) {
- for (NoiseChunk.BlockStateFiller blockStateFiller : this.materialRuleList) {
- BlockState blockState = blockStateFiller.calculate(context);
- if (blockState != null) {
- return blockState;
- }
+ // Leaf start - Reduce worldgen allocations
+ // Avoid iterator allocation
+ BlockState blockState = null;
+ int length = this.materialRuleList.length;
+
+ for (int i = 0; blockState == null && i < length; i++) {
+ NoiseChunk.BlockStateFiller blockStateFiller = this.materialRuleList[i];
+ blockState = blockStateFiller.calculate(context);
}
- return null;
+ return blockState;
+ // Leaf end - Reduce worldgen allocations
}
}