Initial implementation of removal of unnecessary surface rule caching

This commit is contained in:
Voidsong Dragonfly 2025-07-19 21:28:29 -04:00
parent 87c977a3e6
commit 596159a81d
2 changed files with 170 additions and 1 deletions

View File

@ -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<ResourceKey<Biome>> 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();
}
}
}

View File

@ -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 <init> (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 <init> (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
accessible class net/minecraft/client/searchtree/SearchRegistry$TreeEntry