Merge branch 'perf-raster' into perf-review

This commit is contained in:
Brian Neumann-Fopiano committed 2026-10-30 21:29:13 -04:00
commit e9c18cdc08
13 files changed
+463 -54

No files matched your search

@@ -216,6 +216,34 @@ public class HydrologyRuntimeTest {
assertEquals(planned + 1L, runtime.previewRevision());
}
@Test(timeout = 180_000L)
public void concurrentDemandsShareOneRasterBuild() throws Exception {
FakePlanners planners = new FakePlanners(settings(0, false));
HydrologyRuntime runtime = runtime(planners, () -> false);
runtime.prepareWindowsAround(0, 0, 0, 0, 1).get(TIMEOUT_SECONDS, TimeUnit.SECONDS);
CountDownLatch gate = new CountDownLatch(1);
planners.naturalGate = gate;
AtomicReference<ChunkWaterRaster> first = new AtomicReference<>();
AtomicReference<ChunkWaterRaster> second = new AtomicReference<>();
Thread leader = new Thread(() -> first.set(runtime.raster(0, 0)));
Thread follower = new Thread(() -> second.set(runtime.raster(0, 0)));
try {
leader.start();
await(() -> planners.naturalSamples.get() > 0, "the first demand starts sampling");
follower.start();
await(() -> follower.getState() == Thread.State.WAITING || follower.getState() == Thread.State.TIMED_WAITING,
"the second demand waits");
assertEquals(0, planners.naturalSamplesOn(follower));
} finally {
gate.countDown();
leader.join(TimeUnit.SECONDS.toMillis(TIMEOUT_SECONDS));
follower.join(TimeUnit.SECONDS.toMillis(TIMEOUT_SECONDS));
}
assertFalse(first.get().provisional());
assertSame(first.get(), second.get());
assertEquals(0, planners.naturalSamplesOn(follower));
}
@Test(timeout = 180_000L)
public void failedRasterDoesNotAdvancePreviewRevision() throws Exception {
FakePlanners planners = new FakePlanners(settings(0, false));
@@ -0,0 +1,95 @@
package art.arcane.iris.generation.hydrology.raster;
import art.arcane.iris.generation.hydrology.config.IrisBankStyle;
import art.arcane.iris.generation.hydrology.config.IrisHydrologyMaterials;
import art.arcane.iris.generation.hydrology.water.HydrologyTier;
import art.arcane.iris.generation.hydrology.water.Reach;
import art.arcane.iris.generation.hydrology.water.ReachStyle;
import org.junit.Test;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.SplittableRandom;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
public class ReachSegmentsTest {
private static final HydrologyTier TIER = new HydrologyTier("test", 0f, 1f, 4f, 1f, 0.4f, 1, true, 6, 4, IrisBankStyle.SMOOTH,
false, new IrisHydrologyMaterials(), "water", List.of());
@Test
public void coveredChannelLookupsMatchTheFullScan() {
SplittableRandom random = new SplittableRandom(1337L);
int hits = 0;
for (int trial = 0; trial < 40; trial++) {
int originX = random.nextInt(-2000, 2000) << 4;
int originZ = random.nextInt(-2000, 2000) << 4;
List<Reach> reaches = new ArrayList<>();
int count = 1 + random.nextInt(12);
for (int walk = 0; walk < count; walk++) {
reaches.add(randomWalk(trial * 100L + walk, originX + 8, originZ + 8, random));
}
ReachSegments scanned = segments(reaches, originX, originZ);
ReachSegments covered = segments(reaches, originX, originZ);
covered.coverChannels(originX - 17, originZ - 17, originX + 32, originZ + 32);
ReachCursor scan = new ReachCursor();
ReachCursor cover = new ReachCursor();
for (int z = originZ - 20; z <= originZ + 35; z++) {
for (int x = originX - 20; x <= originX + 35; x++) {
ReachSegments.Scope scope = random.nextInt(4) == 0 ? ReachSegments.Scope.SHORE : ReachSegments.Scope.CHANNEL;
scanned.nearest(x, z, scope, scan);
covered.nearest(x, z, scope, cover);
String at = scope + " at " + x + "," + z;
assertSameChoice(at + " channel", scan.channel, cover.channel);
assertSameChoice(at + " bank", scan.bank, cover.bank);
assertSameChoice(at + " edge", scan.edge, cover.edge);
if (scan.channel.reach != null) {
hits++;
}
}
}
}
assertTrue("random walks cross the covered area, hits=" + hits, hits > 1000);
}
private static void assertSameChoice(String what, ReachCursor.Choice expected, ReachCursor.Choice actual) {
assertSame(what, expected.reach, actual.reach);
assertEquals(what, expected.segment, actual.segment);
assertEquals(what, expected.index, actual.index);
assertEquals(what, Double.doubleToLongBits(expected.t), Double.doubleToLongBits(actual.t));
assertEquals(what, Double.doubleToLongBits(expected.distance), Double.doubleToLongBits(actual.distance));
}
private static ReachSegments segments(List<Reach> reaches, int originX, int originZ) {
ReachSegments segments = new ReachSegments(16);
for (Reach reach : reaches) {
segments.addNear(reach, originX - 1f, originZ - 1f, originX + 16f, originZ + 16f, ChunkWaterRaster.MAX_SHORE_DISTANCE + 1f);
}
return segments;
}
private static Reach randomWalk(long id, int centerX, int centerZ, SplittableRandom random) {
int points = 2 + random.nextInt(30);
float[] xs = new float[points];
float[] zs = new float[points];
float[] halfWidth = new float[points];
xs[0] = centerX + (float) random.nextDouble(-60D, 60D);
zs[0] = centerZ + (float) random.nextDouble(-60D, 60D);
halfWidth[0] = (float) random.nextDouble(0.5D, 9D);
for (int point = 1; point < points; point++) {
xs[point] = xs[point - 1] + (float) random.nextDouble(-12D, 12D);
zs[point] = zs[point - 1] + (float) random.nextDouble(-12D, 12D);
halfWidth[point] = random.nextInt(8) == 0 ? halfWidth[point - 1] : (float) random.nextDouble(0.5D, 9D);
}
int[] grade = new int[points];
float[] depth = new float[points];
ReachStyle[] style = new ReachStyle[points];
Arrays.fill(grade, 64);
Arrays.fill(depth, 1f);
Arrays.fill(style, ReachStyle.RUN);
return new Reach(id, TIER, -1L, xs, zs, grade, halfWidth, depth, style, 100f);
}
}
@@ -15,6 +15,7 @@ import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Consumer;
import java.util.function.IntFunction;
@@ -50,18 +51,37 @@ public class CavePoolFillerTest {
return 0;
});
for (int chunkX = 0; chunkX < 1024; chunkX++) {
filler.fill(chunkX, 0, carved, writer, SEED);
filler.fill(chunkX, 0, () -> carved, writer, SEED);
}
assertTrue("half density samples approximately half the candidate columns", columns.size() > 3600 && columns.size() < 4500);
Set<Long> repeated = new HashSet<>(columns);
columns.clear();
for (int chunkX = 1023; chunkX >= 0; chunkX--) {
filler.fill(chunkX, 0, carved, writer, SEED);
filler.fill(chunkX, 0, () -> carved, writer, SEED);
}
assertEquals(repeated, columns);
verifyNoInteractions(writer);
}
@Test
public void chunksWithoutAPoolSiteNeverOpenTheirCaves() {
IrisHydrology config = new IrisHydrology().setDensity(0.5D);
config.getUnderground().getCavePools().setDensity(0.05D);
CavePoolFiller filler = new CavePoolFiller(HydrologySettings.compile(config, SEED, 63, 0, 384));
CarvedTerrain carved = mock(CarvedTerrain.class);
MantleWriter writer = mock(MantleWriter.class);
AtomicInteger opened = new AtomicInteger();
int chunks = 1024;
for (int chunkX = 0; chunkX < chunks; chunkX++) {
filler.fill(chunkX, 0, () -> {
opened.incrementAndGet();
return carved;
}, writer, SEED);
}
assertTrue("only chunks drawing a pool site open their caves, opened " + opened.get(), opened.get() > 0 && opened.get() < chunks / 8);
verifyNoInteractions(writer);
}
@Test
public void zeroGlobalDensityDoesNotReadCaves() {
IrisHydrology config = new IrisHydrology().setDensity(0D);
@@ -69,7 +89,7 @@ public class CavePoolFillerTest {
CavePoolFiller filler = new CavePoolFiller(HydrologySettings.compile(config, SEED, 63, 0, 384));
CarvedTerrain carved = mock(CarvedTerrain.class);
MantleWriter writer = mock(MantleWriter.class);
filler.fill(0, 0, carved, writer, SEED);
filler.fill(0, 0, () -> carved, writer, SEED);
verifyNoInteractions(carved, writer);
}
@@ -143,10 +163,10 @@ public class CavePoolFillerTest {
MantleWriter forwardWriter = writer(forward);
MantleWriter reverseWriter = writer(reverse);
for (int index = 0; index < 9; index++) {
new CavePoolFiller(settings).fill(index % 3 - 1, index / 3 - 1, world, forwardWriter, SEED);
new CavePoolFiller(settings).fill(index % 3 - 1, index / 3 - 1, () -> world, forwardWriter, SEED);
}
for (int index = 8; index >= 0; index--) {
new CavePoolFiller(settings).fill(index % 3 - 1, index / 3 - 1, world, reverseWriter, SEED);
new CavePoolFiller(settings).fill(index % 3 - 1, index / 3 - 1, () -> world, reverseWriter, SEED);
}
assertFalse(forward.isEmpty());
@@ -284,7 +304,7 @@ public class CavePoolFillerTest {
private static Map<Long, HydrologyCaveCell> fill(HydrologySettings settings, VoxelWorld world, int chunkX, int chunkZ) {
Map<Long, HydrologyCaveCell> written = new HashMap<>();
new CavePoolFiller(settings).fill(chunkX, chunkZ, world, writer(written), SEED);
new CavePoolFiller(settings).fill(chunkX, chunkZ, () -> world, writer(written), SEED);
return written;
}
@@ -324,7 +324,7 @@ public class MantleCarvedTerrainTest {
for (SeaInlet inlet : complex.getHydrologyRuntime().inlets(0, chunkZ)) {
rasterizer.write(inlet, 0, chunkZ, writer, HydrologyCaveCell.SEA_FLUID, "", SeaInletRasterizer.SubterrainFluid.NOWHERE);
}
new CavePoolFiller(settings).fill(0, chunkZ, new MantleCarvedTerrain(writer, complex, engine, 0, chunkZ), writer, SEED);
new CavePoolFiller(settings).fill(0, chunkZ, () -> new MantleCarvedTerrain(writer, complex, engine, 0, chunkZ), writer, SEED);
}
return cells;
}