Skip to content

Commit

Permalink
perf: reduce allocation in AquiferSamplerImpl
Browse files Browse the repository at this point in the history
  • Loading branch information
ishland committed Oct 7, 2024
1 parent 032f52c commit 0b7231d
Showing 1 changed file with 52 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,9 @@ protected static double maxDistance(int i, int a) {
@Unique
private long c2me$pos3;

@Unique
private double c2me$mutableDoubleThingy;

@Inject(method = "<init>", at = @At("RETURN"))
private void onInit(CallbackInfo info) {
// preload position cache
Expand Down Expand Up @@ -185,35 +188,36 @@ public BlockState apply(DensityFunction.NoisePos pos, double density) {
this.needsFluidTick = true;
return blockState;
} else {
MutableDouble mutableDouble = new MutableDouble(Double.NaN);
// MutableDouble mutableDouble = new MutableDouble(Double.NaN); // 234MB/s alloc rate at 480 cps
this.c2me$mutableDoubleThingy = Double.NaN;
AquiferSampler.FluidLevel fluidLevel3 = this.getWaterLevel(this.c2me$pos2);
double e = d * this.calculateDensity(pos, mutableDouble, fluidLevel2, fluidLevel3);
double e = d * this.c2me$calculateDensityModified(pos, fluidLevel2, fluidLevel3);
if (density + e > 0.0) {
this.needsFluidTick = false;
return null;
} else {
return aquiferExtracted$getFinalBlockState(pos, density, d, mutableDouble, fluidLevel2, fluidLevel3, blockState);
return aquiferExtracted$getFinalBlockState(pos, density, d, fluidLevel2, fluidLevel3, blockState);
}
}
}

@Unique
private BlockState aquiferExtracted$getFinalBlockState(DensityFunction.NoisePos pos, double density, double d, MutableDouble mutableDouble, AquiferSampler.FluidLevel fluidLevel2, AquiferSampler.FluidLevel fluidLevel3, BlockState blockState) {
private BlockState aquiferExtracted$getFinalBlockState(DensityFunction.NoisePos pos, double density, double d, AquiferSampler.FluidLevel fluidLevel2, AquiferSampler.FluidLevel fluidLevel3, BlockState blockState) {
AquiferSampler.FluidLevel fluidLevel4 = this.getWaterLevel(this.c2me$pos3);
double f = maxDistance(this.c2me$dist1, this.c2me$dist3);
if (aquiferExtracted$extractedCheckFG(pos, density, d, mutableDouble, fluidLevel2, f, fluidLevel4)) return null;
if (aquiferExtracted$extractedCheckFG(pos, density, d, fluidLevel2, f, fluidLevel4)) return null;

double g = maxDistance(this.c2me$dist2, this.c2me$dist3);
if (aquiferExtracted$extractedCheckFG(pos, density, d, mutableDouble, fluidLevel3, g, fluidLevel4)) return null;
if (aquiferExtracted$extractedCheckFG(pos, density, d, fluidLevel3, g, fluidLevel4)) return null;

this.needsFluidTick = true;
return blockState;
}

@Unique
private boolean aquiferExtracted$extractedCheckFG(DensityFunction.NoisePos pos, double density, double d, MutableDouble mutableDouble, AquiferSampler.FluidLevel fluidLevel2, double f, AquiferSampler.FluidLevel fluidLevel4) {
private boolean aquiferExtracted$extractedCheckFG(DensityFunction.NoisePos pos, double density, double d, AquiferSampler.FluidLevel fluidLevel2, double f, AquiferSampler.FluidLevel fluidLevel4) {
if (f > 0.0) {
double g = d * f * this.calculateDensity(pos, mutableDouble, fluidLevel2, fluidLevel4);
double g = d * f * this.c2me$calculateDensityModified(pos, fluidLevel2, fluidLevel4);
if (density + g > 0.0) {
this.needsFluidTick = false;
return true;
Expand Down Expand Up @@ -357,6 +361,27 @@ private double calculateDensity(
}
}

private double c2me$calculateDensityModified(
DensityFunction.NoisePos pos, AquiferSampler.FluidLevel fluidLevel, AquiferSampler.FluidLevel fluidLevel2
) {
int i = pos.blockY();
BlockState blockState = fluidLevel.getBlockState(i);
BlockState blockState2 = fluidLevel2.getBlockState(i);
if ((!blockState.isOf(Blocks.LAVA) || !blockState2.isOf(Blocks.WATER)) && (!blockState.isOf(Blocks.WATER) || !blockState2.isOf(Blocks.LAVA))) {
int j = Math.abs(fluidLevel.y - fluidLevel2.y);
if (j == 0) {
return 0.0;
} else {
double d = 0.5 * (double)(fluidLevel.y + fluidLevel2.y);
final double q = aquiferExtracted$getQ(i, d, j);

return aquiferExtracted$postCalculateDensityModified(pos, q);
}
} else {
return 2.0;
}
}

@Unique
private double aquiferExtracted$postCalculateDensity(DensityFunction.NoisePos pos, MutableDouble mutableDouble, double q) {
double r;
Expand All @@ -376,6 +401,25 @@ private double calculateDensity(
return 2.0 * (r + q);
}

@Unique
private double aquiferExtracted$postCalculateDensityModified(DensityFunction.NoisePos pos, double q) {
double r;
if (!(q < -2.0) && !(q > 2.0)) {
double s = this.c2me$mutableDoubleThingy;
if (Double.isNaN(s)) {
double t = this.barrierNoise.sample(pos);
this.c2me$mutableDoubleThingy = t;
r = t;
} else {
r = s;
}
} else {
r = 0.0;
}

return 2.0 * (r + q);
}

@Unique
private static double aquiferExtracted$getQ(double i, double d, double j) {
double e = i + 0.5 - d;
Expand Down

0 comments on commit 0b7231d

Please sign in to comment.