From 596159a81d8b128c9783640e5a8a6bd08d0b68de Mon Sep 17 00:00:00 2001 From: Voidsong Dragonfly Date: Sat, 19 Jul 2025 21:28:29 -0400 Subject: [PATCH 1/4] Initial implementation of removal of unnecessary surface rule caching --- .../SurfaceRulesMixin.java | 147 ++++++++++++++++++ .../main/resources/modernfix.accesswidener | 24 ++- 2 files changed, 170 insertions(+), 1 deletion(-) create mode 100644 common/src/main/java/org/embeddedt/modernfix/common/mixin/perf/worldgen_allocation/SurfaceRulesMixin.java diff --git a/common/src/main/java/org/embeddedt/modernfix/common/mixin/perf/worldgen_allocation/SurfaceRulesMixin.java b/common/src/main/java/org/embeddedt/modernfix/common/mixin/perf/worldgen_allocation/SurfaceRulesMixin.java new file mode 100644 index 00000000..6bb9d16d --- /dev/null +++ b/common/src/main/java/org/embeddedt/modernfix/common/mixin/perf/worldgen_allocation/SurfaceRulesMixin.java @@ -0,0 +1,147 @@ +package org.embeddedt.modernfix.common.mixin.perf.worldgen_allocation; + +import net.minecraft.resources.ResourceKey; +import net.minecraft.resources.ResourceLocation; +import net.minecraft.util.Mth; +import net.minecraft.util.RandomSource; +import net.minecraft.world.level.biome.Biome; +import net.minecraft.world.level.levelgen.PositionalRandomFactory; +import net.minecraft.world.level.levelgen.SurfaceRules; +import net.minecraft.world.level.levelgen.VerticalAnchor; +import net.minecraft.world.level.levelgen.placement.CaveSurface; +import org.spongepowered.asm.mixin.Final; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Overwrite; +import org.spongepowered.asm.mixin.Shadow; + +import java.util.function.Predicate; + +@Mixin(SurfaceRules.class) +public class SurfaceRulesMixin { + @Mixin(value = SurfaceRules.BiomeConditionSource.class, priority = 100) + public static final class BiomeConditionSource { + @Final + @Shadow + Predicate> biomeNameTest; + /** + * @author VoidsongDragonfly + * @reason Replacing Vanilla's use of {@link SurfaceRules.LazyYCondition LazyYCondition} that causes performance detriments due to unused caching behavior + */ + @Overwrite + public SurfaceRules.Condition apply(final SurfaceRules.Context pContext) { + class BiomeCondition implements SurfaceRules.Condition { + @Override + public boolean test() { + return pContext.biome.get().is(biomeNameTest); + } + } + + return new BiomeCondition(); + } + } + + @Mixin(value = SurfaceRules.StoneDepthCheck.class, priority = 100) + public record StoneDepthCheck(int offset, boolean addSurfaceDepth, int secondaryDepthRange, CaveSurface surfaceType) { + /** + * @author VoidsongDragonfly + * @reason Replacing Vanilla's use of {@link SurfaceRules.LazyYCondition LazyYCondition} that causes performance detriments due to unused caching behavior + */ + @Overwrite + public SurfaceRules.Condition apply(final SurfaceRules.Context pContext) { + // Copied Vanilla variables + final boolean flag = this.surfaceType == CaveSurface.CEILING; + + class StoneDepthCondition implements SurfaceRules.Condition { + @Override + public boolean test() { + int i = flag ? pContext.stoneDepthBelow : pContext.stoneDepthAbove; + int j = addSurfaceDepth ? pContext.surfaceDepth : 0; + int k = secondaryDepthRange == 0 + ? 0 + : (int) Mth.map(pContext.getSurfaceSecondary(), -1.0, 1.0, 0.0, secondaryDepthRange); + return i <= 1 + offset + j + k; + } + } + + return new StoneDepthCondition(); + } + } + + @Mixin(value = SurfaceRules.VerticalGradientConditionSource.class, priority = 100) + public record VerticalGradientConditionSource(ResourceLocation randomName, VerticalAnchor trueAtAndBelow, VerticalAnchor falseAtAndAbove) { + /** + * @author VoidsongDragonfly + * @reason Replacing Vanilla's use of {@link SurfaceRules.LazyYCondition LazyYCondition} that causes performance detriments due to unused caching behavior + */ + @Overwrite + public SurfaceRules.Condition apply(final SurfaceRules.Context pContext) { + // Copied Vanilla variables + final int i = this.trueAtAndBelow().resolveY(pContext.context); + final int j = this.falseAtAndAbove().resolveY(pContext.context); + final PositionalRandomFactory positionalrandomfactory = pContext.randomState.getOrCreateRandomFactory(this.randomName()); + + class VerticalGradientCondition implements SurfaceRules.Condition { + @Override + public boolean test() { + int k = pContext.blockY; + if (k <= i) { + return true; + } else if (k >= j) { + return false; + } else { + double d0 = Mth.map(k, i, j, 1.0, 0.0); + RandomSource randomsource = positionalrandomfactory.at(pContext.blockX, k, pContext.blockZ); + return (double)randomsource.nextFloat() < d0; + } + } + } + + return new VerticalGradientCondition(); + } + } + + @Mixin(value = SurfaceRules.WaterConditionSource.class, priority = 100) + public record WaterConditionSource(int offset, int surfaceDepthMultiplier, boolean addStoneDepth) { + /** + * @author VoidsongDragonfly + * @reason Replacing Vanilla's use of {@link SurfaceRules.LazyYCondition LazyYCondition} that causes performance detriments due to unused caching behavior + */ + @Overwrite + public SurfaceRules.Condition apply(final SurfaceRules.Context pContext) { + class WaterCondition implements SurfaceRules.Condition { + @Override + public boolean test() { + return pContext.waterHeight == Integer.MIN_VALUE + || pContext.blockY + (addStoneDepth ? pContext.stoneDepthAbove : 0) + >= pContext.waterHeight + + offset + + pContext.surfaceDepth * surfaceDepthMultiplier; + } + } + + return new WaterCondition(); + } + + } + + @Mixin(value = SurfaceRules.YConditionSource.class, priority = 100) + public record YConditionSource(VerticalAnchor anchor, int surfaceDepthMultiplier, boolean addStoneDepth) { + /** + * @author VoidsongDragonfly + * @reason Replacing Vanilla's use of {@link SurfaceRules.LazyYCondition LazyYCondition} that causes performance detriments due to unused caching behavior + */ + @Overwrite + public SurfaceRules.Condition apply(final SurfaceRules.Context pContext) { + class YCondition implements SurfaceRules.Condition { + @Override + public boolean test() { + return pContext.blockY + (addStoneDepth ? pContext.stoneDepthAbove : 0) + >= anchor.resolveY(pContext.context) + + pContext.surfaceDepth * surfaceDepthMultiplier; + } + } + + return new YCondition(); + } + } +} diff --git a/common/src/main/resources/modernfix.accesswidener b/common/src/main/resources/modernfix.accesswidener index 57322acc..4e2d26e2 100644 --- a/common/src/main/resources/modernfix.accesswidener +++ b/common/src/main/resources/modernfix.accesswidener @@ -11,8 +11,30 @@ accessible field net/minecraft/world/level/Level blockEntityTickers Ljava/util/L accessible class net/minecraft/client/renderer/RenderType$CompositeRenderType accessible method net/minecraft/nbt/CompoundTag (Ljava/util/Map;)V +# Non-context access necessary +accessible class net/minecraft/world/level/levelgen/SurfaceRules$Condition +accessible class net/minecraft/world/level/levelgen/SurfaceRules$LazyYCondition +accessible class net/minecraft/world/level/levelgen/SurfaceRules$StoneDepthCheck +accessible class net/minecraft/world/level/levelgen/SurfaceRules$WaterConditionSource +accessible class net/minecraft/world/level/levelgen/SurfaceRules$VerticalGradientConditionSource +accessible class net/minecraft/world/level/levelgen/SurfaceRules$YConditionSource +accessible class net/minecraft/world/level/levelgen/SurfaceRules$BiomeConditionSource accessible class net/minecraft/world/level/levelgen/SurfaceRules$SequenceRule accessible class net/minecraft/world/level/levelgen/SurfaceRules$SurfaceRule +# Access to allow working with Context +accessible class net/minecraft/world/level/levelgen/SurfaceRules$Context +accessible field net/minecraft/world/level/levelgen/SurfaceRules$Context context Lnet/minecraft/world/level/levelgen/SurfaceRules$Context; +accessible field net/minecraft/world/level/levelgen/SurfaceRules$Context biome Ljava/util/function/Supplier; +accessible field net/minecraft/world/level/levelgen/SurfaceRules$Context blockX I +accessible field net/minecraft/world/level/levelgen/SurfaceRules$Context blockY I +accessible field net/minecraft/world/level/levelgen/SurfaceRules$Context blockZ I +accessible field net/minecraft/world/level/levelgen/SurfaceRules$Context stoneDepthBelow I +accessible field net/minecraft/world/level/levelgen/SurfaceRules$Context randomState Lnet/minecraft/world/level/levelgen/RandomState; +accessible field net/minecraft/world/level/levelgen/SurfaceRules$Context waterHeight I +accessible field net/minecraft/world/level/levelgen/SurfaceRules$Context stoneDepthAbove I +accessible field net/minecraft/world/level/levelgen/SurfaceRules$Context surfaceDepth I +accessible method net/minecraft/world/level/levelgen/SurfaceRules$Context getSurfaceSecondary ()D +# Density function access accessible class net/minecraft/world/level/levelgen/DensityFunctions$Marker accessible class net/minecraft/world/level/levelgen/DensityFunctions$Marker$Type accessible method net/minecraft/world/level/levelgen/DensityFunctions$Marker (Lnet/minecraft/world/level/levelgen/DensityFunctions$Marker$Type;Lnet/minecraft/world/level/levelgen/DensityFunction;)V @@ -69,4 +91,4 @@ accessible class net/minecraft/world/item/crafting/Ingredient$Value accessible field net/minecraft/world/item/crafting/Ingredient$TagValue tag Lnet/minecraft/tags/TagKey; accessible field net/minecraft/world/item/crafting/Ingredient$ItemValue item Lnet/minecraft/world/item/ItemStack; accessible class net/minecraft/world/item/crafting/Ingredient$ItemValue -accessible class net/minecraft/client/searchtree/SearchRegistry$TreeEntry \ No newline at end of file +accessible class net/minecraft/client/searchtree/SearchRegistry$TreeEntry From 52f06e85677e0d9f122bede3964eb4e68d6f7e96 Mon Sep 17 00:00:00 2001 From: Voidsong Dragonfly Date: Sat, 19 Jul 2025 23:15:13 -0400 Subject: [PATCH 2/4] Implement fix for record mixins & update shadowed variable access definitions in context mixin --- .../SurfaceRulesContextMixin.java | 17 ++++--- .../SurfaceRulesMixin.java | 49 +++++++++++++------ .../main/resources/modernfix.accesswidener | 2 +- 3 files changed, 44 insertions(+), 24 deletions(-) diff --git a/common/src/main/java/org/embeddedt/modernfix/common/mixin/perf/worldgen_allocation/SurfaceRulesContextMixin.java b/common/src/main/java/org/embeddedt/modernfix/common/mixin/perf/worldgen_allocation/SurfaceRulesContextMixin.java index c2be9c3d..71e7dfd5 100644 --- a/common/src/main/java/org/embeddedt/modernfix/common/mixin/perf/worldgen_allocation/SurfaceRulesContextMixin.java +++ b/common/src/main/java/org/embeddedt/modernfix/common/mixin/perf/worldgen_allocation/SurfaceRulesContextMixin.java @@ -3,6 +3,7 @@ import net.minecraft.core.BlockPos; import net.minecraft.core.Holder; import net.minecraft.world.level.biome.Biome; +import net.minecraft.world.level.levelgen.SurfaceRules; import org.embeddedt.modernfix.world.gen.PositionalBiomeGetter; import org.spongepowered.asm.mixin.Final; import org.spongepowered.asm.mixin.Mixin; @@ -12,23 +13,23 @@ import java.util.function.Function; import java.util.function.Supplier; -@Mixin(targets = {"net/minecraft/world/level/levelgen/SurfaceRules$Context"}, priority = 100) +@Mixin(value = SurfaceRules.Context.class, priority = 100) public class SurfaceRulesContextMixin { - @Shadow private long lastUpdateY; + @Shadow long lastUpdateY; - @Shadow private int blockY; + @Shadow public int blockY; - @Shadow private int waterHeight; + @Shadow public int waterHeight; - @Shadow private int stoneDepthBelow; + @Shadow public int stoneDepthBelow; - @Shadow private int stoneDepthAbove; + @Shadow public int stoneDepthAbove; - @Shadow private Supplier> biome; + @Shadow public Supplier> biome; @Shadow @Final private Function> biomeGetter; - @Shadow @Final private BlockPos.MutableBlockPos pos; + @Shadow @Final BlockPos.MutableBlockPos pos; /** * @author embeddedt diff --git a/common/src/main/java/org/embeddedt/modernfix/common/mixin/perf/worldgen_allocation/SurfaceRulesMixin.java b/common/src/main/java/org/embeddedt/modernfix/common/mixin/perf/worldgen_allocation/SurfaceRulesMixin.java index 6bb9d16d..c265d2db 100644 --- a/common/src/main/java/org/embeddedt/modernfix/common/mixin/perf/worldgen_allocation/SurfaceRulesMixin.java +++ b/common/src/main/java/org/embeddedt/modernfix/common/mixin/perf/worldgen_allocation/SurfaceRulesMixin.java @@ -20,12 +20,11 @@ public class SurfaceRulesMixin { @Mixin(value = SurfaceRules.BiomeConditionSource.class, priority = 100) public static final class BiomeConditionSource { - @Final - @Shadow - Predicate> biomeNameTest; + @Final @Shadow Predicate> biomeNameTest; /** * @author VoidsongDragonfly - * @reason Replacing Vanilla's use of {@link SurfaceRules.LazyYCondition LazyYCondition} that causes performance detriments due to unused caching behavior + * @reason Replacing Vanilla's use of {@link SurfaceRules.LazyYCondition LazyYCondition} that causes performance detriments due to unused caching behavior. + * This is an exact reimplementation of the surface rule, without the caching; check code and effect are identical. */ @Overwrite public SurfaceRules.Condition apply(final SurfaceRules.Context pContext) { @@ -41,10 +40,16 @@ public boolean test() { } @Mixin(value = SurfaceRules.StoneDepthCheck.class, priority = 100) - public record StoneDepthCheck(int offset, boolean addSurfaceDepth, int secondaryDepthRange, CaveSurface surfaceType) { + public static final class StoneDepthCheck { + @Final @Shadow int offset; + @Final @Shadow boolean addSurfaceDepth; + @Final @Shadow int secondaryDepthRange; + @Final @Shadow private CaveSurface surfaceType; + /** * @author VoidsongDragonfly - * @reason Replacing Vanilla's use of {@link SurfaceRules.LazyYCondition LazyYCondition} that causes performance detriments due to unused caching behavior + * @reason Replacing Vanilla's use of {@link SurfaceRules.LazyYCondition LazyYCondition} that causes performance detriments due to unused caching behavior. + * This is an exact reimplementation of the surface rule, without the caching; check code and effect are identical. */ @Overwrite public SurfaceRules.Condition apply(final SurfaceRules.Context pContext) { @@ -68,17 +73,22 @@ public boolean test() { } @Mixin(value = SurfaceRules.VerticalGradientConditionSource.class, priority = 100) - public record VerticalGradientConditionSource(ResourceLocation randomName, VerticalAnchor trueAtAndBelow, VerticalAnchor falseAtAndAbove) { + public static final class VerticalGradientConditionSource { + @Final @Shadow private ResourceLocation randomName; + @Final @Shadow private VerticalAnchor trueAtAndBelow; + @Final @Shadow private VerticalAnchor falseAtAndAbove; + /** * @author VoidsongDragonfly - * @reason Replacing Vanilla's use of {@link SurfaceRules.LazyYCondition LazyYCondition} that causes performance detriments due to unused caching behavior + * @reason Replacing Vanilla's use of {@link SurfaceRules.LazyYCondition LazyYCondition} that causes performance detriments due to unused caching behavior. + * This is an exact reimplementation of the surface rule, without the caching; check code and effect are identical. */ @Overwrite public SurfaceRules.Condition apply(final SurfaceRules.Context pContext) { // Copied Vanilla variables - final int i = this.trueAtAndBelow().resolveY(pContext.context); - final int j = this.falseAtAndAbove().resolveY(pContext.context); - final PositionalRandomFactory positionalrandomfactory = pContext.randomState.getOrCreateRandomFactory(this.randomName()); + final int i = trueAtAndBelow.resolveY(pContext.context); + final int j = falseAtAndAbove.resolveY(pContext.context); + final PositionalRandomFactory positionalrandomfactory = pContext.randomState.getOrCreateRandomFactory(randomName); class VerticalGradientCondition implements SurfaceRules.Condition { @Override @@ -101,10 +111,15 @@ public boolean test() { } @Mixin(value = SurfaceRules.WaterConditionSource.class, priority = 100) - public record WaterConditionSource(int offset, int surfaceDepthMultiplier, boolean addStoneDepth) { + public static final class WaterConditionSource { + @Final @Shadow int offset; + @Final @Shadow int surfaceDepthMultiplier; + @Final @Shadow boolean addStoneDepth; + /** * @author VoidsongDragonfly - * @reason Replacing Vanilla's use of {@link SurfaceRules.LazyYCondition LazyYCondition} that causes performance detriments due to unused caching behavior + * @reason Replacing Vanilla's use of {@link SurfaceRules.LazyYCondition LazyYCondition} that causes performance detriments due to unused caching behavior. + * This is an exact reimplementation of the surface rule, without the caching; check code and effect are identical. */ @Overwrite public SurfaceRules.Condition apply(final SurfaceRules.Context pContext) { @@ -125,10 +140,14 @@ public boolean test() { } @Mixin(value = SurfaceRules.YConditionSource.class, priority = 100) - public record YConditionSource(VerticalAnchor anchor, int surfaceDepthMultiplier, boolean addStoneDepth) { + public static final class YConditionSource { + @Final @Shadow VerticalAnchor anchor; + @Final @Shadow int surfaceDepthMultiplier; + @Final @Shadow boolean addStoneDepth; /** * @author VoidsongDragonfly - * @reason Replacing Vanilla's use of {@link SurfaceRules.LazyYCondition LazyYCondition} that causes performance detriments due to unused caching behavior + * @reason Replacing Vanilla's use of {@link SurfaceRules.LazyYCondition LazyYCondition} that causes performance detriments due to unused caching behavior. + * This is an exact reimplementation of the surface rule, without the caching; check code and effect are identical. */ @Overwrite public SurfaceRules.Condition apply(final SurfaceRules.Context pContext) { diff --git a/common/src/main/resources/modernfix.accesswidener b/common/src/main/resources/modernfix.accesswidener index 4e2d26e2..61255b2b 100644 --- a/common/src/main/resources/modernfix.accesswidener +++ b/common/src/main/resources/modernfix.accesswidener @@ -23,7 +23,7 @@ accessible class net/minecraft/world/level/levelgen/SurfaceRules$SequenceRule accessible class net/minecraft/world/level/levelgen/SurfaceRules$SurfaceRule # Access to allow working with Context accessible class net/minecraft/world/level/levelgen/SurfaceRules$Context -accessible field net/minecraft/world/level/levelgen/SurfaceRules$Context context Lnet/minecraft/world/level/levelgen/SurfaceRules$Context; +accessible field net/minecraft/world/level/levelgen/SurfaceRules$Context context Lnet/minecraft/world/level/levelgen/WorldGenerationContext; accessible field net/minecraft/world/level/levelgen/SurfaceRules$Context biome Ljava/util/function/Supplier; accessible field net/minecraft/world/level/levelgen/SurfaceRules$Context blockX I accessible field net/minecraft/world/level/levelgen/SurfaceRules$Context blockY I From 4c312026ef0856dd17582a764b78dfc8793d8bcb Mon Sep 17 00:00:00 2001 From: Voidsong Dragonfly Date: Sat, 2 Aug 2025 23:32:10 -0400 Subject: [PATCH 3/4] Removal of inner classes on mixins, factor them out --- .../SurfaceRulesMixin.java | 79 +++---------------- .../world/gen/SurfaceConditionRecords.java | 68 ++++++++++++++++ 2 files changed, 81 insertions(+), 66 deletions(-) create mode 100644 common/src/main/java/org/embeddedt/modernfix/world/gen/SurfaceConditionRecords.java diff --git a/common/src/main/java/org/embeddedt/modernfix/common/mixin/perf/worldgen_allocation/SurfaceRulesMixin.java b/common/src/main/java/org/embeddedt/modernfix/common/mixin/perf/worldgen_allocation/SurfaceRulesMixin.java index c265d2db..e7f80df1 100644 --- a/common/src/main/java/org/embeddedt/modernfix/common/mixin/perf/worldgen_allocation/SurfaceRulesMixin.java +++ b/common/src/main/java/org/embeddedt/modernfix/common/mixin/perf/worldgen_allocation/SurfaceRulesMixin.java @@ -2,13 +2,12 @@ import net.minecraft.resources.ResourceKey; import net.minecraft.resources.ResourceLocation; -import net.minecraft.util.Mth; -import net.minecraft.util.RandomSource; import net.minecraft.world.level.biome.Biome; import net.minecraft.world.level.levelgen.PositionalRandomFactory; import net.minecraft.world.level.levelgen.SurfaceRules; import net.minecraft.world.level.levelgen.VerticalAnchor; import net.minecraft.world.level.levelgen.placement.CaveSurface; +import org.embeddedt.modernfix.world.gen.SurfaceConditionRecords; import org.spongepowered.asm.mixin.Final; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Overwrite; @@ -16,7 +15,7 @@ import java.util.function.Predicate; -@Mixin(SurfaceRules.class) + public class SurfaceRulesMixin { @Mixin(value = SurfaceRules.BiomeConditionSource.class, priority = 100) public static final class BiomeConditionSource { @@ -28,14 +27,8 @@ public static final class BiomeConditionSource { */ @Overwrite public SurfaceRules.Condition apply(final SurfaceRules.Context pContext) { - class BiomeCondition implements SurfaceRules.Condition { - @Override - public boolean test() { - return pContext.biome.get().is(biomeNameTest); - } - } - - return new BiomeCondition(); + // Return the condition; this record is kept outside the class at embeddedt's wishes due to mixin compat concerns + return new SurfaceConditionRecords.PerformantBiomeCondition(pContext, biomeNameTest); } } @@ -55,20 +48,8 @@ public static final class StoneDepthCheck { public SurfaceRules.Condition apply(final SurfaceRules.Context pContext) { // Copied Vanilla variables final boolean flag = this.surfaceType == CaveSurface.CEILING; - - class StoneDepthCondition implements SurfaceRules.Condition { - @Override - public boolean test() { - int i = flag ? pContext.stoneDepthBelow : pContext.stoneDepthAbove; - int j = addSurfaceDepth ? pContext.surfaceDepth : 0; - int k = secondaryDepthRange == 0 - ? 0 - : (int) Mth.map(pContext.getSurfaceSecondary(), -1.0, 1.0, 0.0, secondaryDepthRange); - return i <= 1 + offset + j + k; - } - } - - return new StoneDepthCondition(); + // Return the condition; this record is kept outside the class at embeddedt's wishes due to mixin compat concerns + return new SurfaceConditionRecords.PerformantStoneDepthCondition(pContext, offset, addSurfaceDepth, secondaryDepthRange, flag); } } @@ -88,25 +69,9 @@ public SurfaceRules.Condition apply(final SurfaceRules.Context pContext) { // Copied Vanilla variables final int i = trueAtAndBelow.resolveY(pContext.context); final int j = falseAtAndAbove.resolveY(pContext.context); - final PositionalRandomFactory positionalrandomfactory = pContext.randomState.getOrCreateRandomFactory(randomName); - - class VerticalGradientCondition implements SurfaceRules.Condition { - @Override - public boolean test() { - int k = pContext.blockY; - if (k <= i) { - return true; - } else if (k >= j) { - return false; - } else { - double d0 = Mth.map(k, i, j, 1.0, 0.0); - RandomSource randomsource = positionalrandomfactory.at(pContext.blockX, k, pContext.blockZ); - return (double)randomsource.nextFloat() < d0; - } - } - } - - return new VerticalGradientCondition(); + final PositionalRandomFactory positionalRandomFactory = pContext.randomState.getOrCreateRandomFactory(randomName); + // Return the condition; this record is kept outside the class at embeddedt's wishes due to mixin compat concerns + return new SurfaceConditionRecords.PerformantVerticalGradientCondition(pContext, i, j, positionalRandomFactory); } } @@ -123,18 +88,8 @@ public static final class WaterConditionSource { */ @Overwrite public SurfaceRules.Condition apply(final SurfaceRules.Context pContext) { - class WaterCondition implements SurfaceRules.Condition { - @Override - public boolean test() { - return pContext.waterHeight == Integer.MIN_VALUE - || pContext.blockY + (addStoneDepth ? pContext.stoneDepthAbove : 0) - >= pContext.waterHeight - + offset - + pContext.surfaceDepth * surfaceDepthMultiplier; - } - } - - return new WaterCondition(); + // Return the condition; this record is kept outside the class at embeddedt's wishes due to mixin compat concerns + return new SurfaceConditionRecords.PerformantWaterCondition(pContext, offset, surfaceDepthMultiplier, addStoneDepth); } } @@ -151,16 +106,8 @@ public static final class YConditionSource { */ @Overwrite public SurfaceRules.Condition apply(final SurfaceRules.Context pContext) { - class YCondition implements SurfaceRules.Condition { - @Override - public boolean test() { - return pContext.blockY + (addStoneDepth ? pContext.stoneDepthAbove : 0) - >= anchor.resolveY(pContext.context) - + pContext.surfaceDepth * surfaceDepthMultiplier; - } - } - - return new YCondition(); + // Return the condition; this record is kept outside the class at embeddedt's wishes due to mixin compat concerns + return new SurfaceConditionRecords.PerformantYCondition(pContext, anchor, surfaceDepthMultiplier, addStoneDepth); } } } diff --git a/common/src/main/java/org/embeddedt/modernfix/world/gen/SurfaceConditionRecords.java b/common/src/main/java/org/embeddedt/modernfix/world/gen/SurfaceConditionRecords.java new file mode 100644 index 00000000..af8a9fa3 --- /dev/null +++ b/common/src/main/java/org/embeddedt/modernfix/world/gen/SurfaceConditionRecords.java @@ -0,0 +1,68 @@ +package org.embeddedt.modernfix.world.gen; + +import net.minecraft.resources.ResourceKey; +import net.minecraft.util.Mth; +import net.minecraft.util.RandomSource; +import net.minecraft.world.level.biome.Biome; +import net.minecraft.world.level.levelgen.PositionalRandomFactory; +import net.minecraft.world.level.levelgen.SurfaceRules; +import net.minecraft.world.level.levelgen.VerticalAnchor; + +import java.util.function.Predicate; + +public class SurfaceConditionRecords { + public record PerformantBiomeCondition(SurfaceRules.Context pContext, Predicate> biomeNameTest) implements SurfaceRules.Condition { + @Override + public boolean test() { + return pContext.biome.get().is(biomeNameTest); + } + } + + public record PerformantStoneDepthCondition(SurfaceRules.Context pContext, int offset, boolean addSurfaceDepth, int secondaryDepthRange, boolean flag) implements SurfaceRules.Condition { + @Override + public boolean test() { + int i = flag ? pContext.stoneDepthBelow : pContext.stoneDepthAbove; + int j = addSurfaceDepth ? pContext.surfaceDepth : 0; + int k = secondaryDepthRange == 0 + ? 0 + : (int) Mth.map(pContext.getSurfaceSecondary(), -1.0, 1.0, 0.0, secondaryDepthRange); + return i <= 1 + offset + j + k; + } + } + + public record PerformantVerticalGradientCondition(SurfaceRules.Context pContext, int i, int j, PositionalRandomFactory positionalRandomFactory) implements SurfaceRules.Condition { + @Override + public boolean test() { + int k = pContext.blockY; + if (k <= i) { + return true; + } else if (k >= j) { + return false; + } else { + double d0 = Mth.map(k, i, j, 1.0, 0.0); + RandomSource randomsource = positionalRandomFactory.at(pContext.blockX, k, pContext.blockZ); + return (double)randomsource.nextFloat() < d0; + } + } + } + + public record PerformantWaterCondition(SurfaceRules.Context pContext, int offset, int surfaceDepthMultiplier, boolean addStoneDepth) implements SurfaceRules.Condition { + @Override + public boolean test() { + return pContext.waterHeight == Integer.MIN_VALUE + || pContext.blockY + (addStoneDepth ? pContext.stoneDepthAbove : 0) + >= pContext.waterHeight + + offset + + pContext.surfaceDepth * surfaceDepthMultiplier; + } + } + + public record PerformantYCondition(SurfaceRules.Context pContext, VerticalAnchor anchor, int surfaceDepthMultiplier, boolean addStoneDepth) implements SurfaceRules.Condition { + @Override + public boolean test() { + return pContext.blockY + (addStoneDepth ? pContext.stoneDepthAbove : 0) + >= anchor.resolveY(pContext.context) + + pContext.surfaceDepth * surfaceDepthMultiplier; + } + } +} From e4ed578c4d091aacd38cebb434b450ca423d9b53 Mon Sep 17 00:00:00 2001 From: Voidsong Dragonfly Date: Sat, 2 Aug 2025 23:32:20 -0400 Subject: [PATCH 4/4] Enable worldgen mixins by default --- .../embeddedt/modernfix/core/config/ModernFixEarlyConfig.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/common/src/main/java/org/embeddedt/modernfix/core/config/ModernFixEarlyConfig.java b/common/src/main/java/org/embeddedt/modernfix/core/config/ModernFixEarlyConfig.java index c120dcd7..4b737a25 100644 --- a/common/src/main/java/org/embeddedt/modernfix/core/config/ModernFixEarlyConfig.java +++ b/common/src/main/java/org/embeddedt/modernfix/core/config/ModernFixEarlyConfig.java @@ -165,7 +165,7 @@ public DefaultSettingMapBuilder put(String key, Boolean value) { .put("mixin.feature.stalled_chunk_load_detection", false) .put("mixin.perf.blast_search_trees.force", false) .put("mixin.bugfix.restore_old_dragon_movement", false) - .put("mixin.perf.worldgen_allocation", false) // experimental + .put("mixin.perf.worldgen_allocation", true) .put("mixin.feature.cause_lag_by_disabling_threads", false) .put("mixin.bugfix.missing_block_entities", false) .put("mixin.feature.blockentity_incorrect_thread", false)