From b6a216462d39378e5fdb28b14f907eca52baf079 Mon Sep 17 00:00:00 2001 From: piexel Date: Sat, 10 Oct 2026 14:46:22 +0200 Subject: [PATCH 1/4] A loaded pack's modules are handed out without taking the module set's lock: every cave biome asks for them, from every generating thread --- .../arcane/iris/core/modules/PackModuleSet.java | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/core/src/main/java/art/arcane/iris/core/modules/PackModuleSet.java b/core/src/main/java/art/arcane/iris/core/modules/PackModuleSet.java index 1ba63cf99..5276d403e 100644 --- a/core/src/main/java/art/arcane/iris/core/modules/PackModuleSet.java +++ b/core/src/main/java/art/arcane/iris/core/modules/PackModuleSet.java @@ -43,7 +43,8 @@ public final class PackModuleSet implements AutoCloseable { private final File packFolder; private final Supplier irisGson; private State state = State.NOT_LOADED; - private LoadedPack pack; + /** Set only while loaded; volatile so {@link #pack()}, asked on the generation hot path, needs no lock then. */ + private volatile LoadedPack pack; private ModuleSettingsDecoder settings; private PackModulesException failure; private List loadedJars = List.of(); @@ -55,7 +56,15 @@ public final class PackModuleSet implements AutoCloseable { } /** The loaded pack, its built-in modules included. */ - public synchronized LoadedPack pack() { + public LoadedPack pack() { + LoadedPack loaded = pack; + if (loaded != null) { + return loaded; + } + return loadedPack(); + } + + private synchronized LoadedPack loadedPack() { requireLoaded(); return pack; } @@ -110,8 +119,8 @@ public final class PackModuleSet implements AutoCloseable { loadedJars = jarFingerprint(); try { LoadedPack loaded = LoadedPack.load(packFolder.toPath(), IrisModuleHost.get()); - pack = loaded; settings = new ModuleSettingsDecoder(loaded.catalog(), packFolder.toPath(), irisGson); + pack = loaded; state = State.LOADED; } catch (RuntimeException loadFailure) { failure = new PackModulesException(packFolder, loadFailure); From 339efd6174a53cdfedb11840a0be2ea26fff21e0 Mon Sep 17 00:00:00 2001 From: piexel Date: Sat, 10 Oct 2026 14:46:22 +0200 Subject: [PATCH 2/4] Cave biomes ask the world's CaveBiomes provider through CaveBiomeAnswers, kept per world: the provider is found once, each biome's minimum depth asked once and each overriding biome loaded once (it was a catalog scan twice per cave block) --- .../iris/core/modules/CaveBiomeAnswers.java | 87 +++++++++++++++++++ .../iris/core/modules/ModuleServices.java | 15 +--- .../arcane/iris/engine/framework/Engine.java | 8 +- .../engine/framework/EngineCaveBiomeTest.java | 31 +++++++ 4 files changed, 126 insertions(+), 15 deletions(-) create mode 100644 core/src/main/java/art/arcane/iris/core/modules/CaveBiomeAnswers.java diff --git a/core/src/main/java/art/arcane/iris/core/modules/CaveBiomeAnswers.java b/core/src/main/java/art/arcane/iris/core/modules/CaveBiomeAnswers.java new file mode 100644 index 000000000..c1071f8b9 --- /dev/null +++ b/core/src/main/java/art/arcane/iris/core/modules/CaveBiomeAnswers.java @@ -0,0 +1,87 @@ +/* + * Iris is a World Generator for Minecraft Servers + * Copyright (c) 2026 Arcane Arts (Volmit Software) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package art.arcane.iris.core.modules; + +import art.arcane.iris.core.loader.IrisData; +import art.arcane.iris.engine.framework.Engine; +import art.arcane.iris.engine.object.world.biome.IrisBiome; +import com.volmit.service.CaveBiomes; +import com.volmit.world.PackWorld; + +import java.util.Map; +import java.util.Optional; +import java.util.concurrent.ConcurrentHashMap; + +/** + * What the world's {@link CaveBiomes} provider says, kept for the world. The host asks it for every cave block it gives a + * biome, so the provider is found once per world, each biome's minimum depth asked once and each overriding biome + * loaded once. + */ +public final class CaveBiomeAnswers { + private static final CaveBiomes NO_PROVIDER = new CaveBiomes() { + @Override + public Optional overriddenBiome(PackWorld world, int x, int y, int z) { + return Optional.empty(); + } + + @Override + public int minDepthBelowSurface(PackWorld world, String biomeKey) { + return 0; + } + }; + + private final PackWorld world; + private final IrisData data; + private final CaveBiomes provider; + private final Map overridingBiomes = new ConcurrentHashMap<>(); + private final Map minDepths = new ConcurrentHashMap<>(); + + CaveBiomeAnswers(Engine engine, CaveBiomes provider) { + this.world = engine.getGeneration().world(); + this.data = engine.getData(); + this.provider = provider == null ? NO_PROVIDER : provider; + } + + /** The pack biome the module puts at the position whatever the regions say; null where it overrides nothing or names a biome the pack lacks. {@code y} is the height above the dimension's bottom. */ + public IrisBiome overriddenBiome(int x, int y, int z) { + Optional key = provider.overriddenBiome(world, x, y, z); + return key.isEmpty() ? null : overridingBiome(key.get()); + } + + /** How many blocks below the surface the caves of the pack biome {@code biomeKey} begin; 0 where nothing limits them. */ + public int minDepthBelowSurface(String biomeKey) { + Integer depth = minDepths.get(biomeKey); + if (depth != null) { + return depth; + } + return minDepths.computeIfAbsent(biomeKey, key -> provider.minDepthBelowSurface(world, key)); + } + + private IrisBiome overridingBiome(String key) { + IrisBiome biome = overridingBiomes.get(key); + if (biome != null) { + return biome; + } + biome = data.getBiomeLoader().load(key); + if (biome != null) { + overridingBiomes.putIfAbsent(key, biome); + } + return biome; + } +} diff --git a/core/src/main/java/art/arcane/iris/core/modules/ModuleServices.java b/core/src/main/java/art/arcane/iris/core/modules/ModuleServices.java index d186fb042..ead34187e 100644 --- a/core/src/main/java/art/arcane/iris/core/modules/ModuleServices.java +++ b/core/src/main/java/art/arcane/iris/core/modules/ModuleServices.java @@ -50,17 +50,10 @@ public final class ModuleServices { private ModuleServices() { } - /** The pack biome key the module that decides cave biomes puts at the position, whatever the regions say; empty where it overrides nothing. {@code y} is the height above the dimension's bottom. */ - public static Optional overriddenCaveBiome(Engine engine, int x, int y, int z) { - return catalog(engine.getData()).service(CaveBiomes.class) - .flatMap(provider -> provider.overriddenBiome(engine.getGeneration().world(), x, y, z)); - } - - /** How many blocks below the surface the caves of the pack biome {@code biomeKey} begin; 0 where nothing limits them. */ - public static int caveMinDepthBelowSurface(Engine engine, String biomeKey) { - return catalog(engine.getData()).service(CaveBiomes.class) - .map(provider -> provider.minDepthBelowSurface(engine.getGeneration().world(), biomeKey)) - .orElse(0); + /** What the module that decides cave biomes says for the engine's world, kept for the world. */ + public static CaveBiomeAnswers caveBiomes(Engine engine) { + return engine.getGeneration().state(CaveBiomeAnswers.class, owner -> + new CaveBiomeAnswers(owner, catalog(owner.getData()).service(CaveBiomes.class).orElse(null))); } /** The objects every module would place in the chunk at {@code chunkX}, {@code chunkZ}, guessed without generating it. */ diff --git a/core/src/main/java/art/arcane/iris/engine/framework/Engine.java b/core/src/main/java/art/arcane/iris/engine/framework/Engine.java index 4a5b19dba..ac797adc5 100644 --- a/core/src/main/java/art/arcane/iris/engine/framework/Engine.java +++ b/core/src/main/java/art/arcane/iris/engine/framework/Engine.java @@ -28,6 +28,7 @@ import art.arcane.iris.core.loader.IrisData; import art.arcane.iris.core.loader.IrisRegistrant; import art.arcane.iris.core.nms.container.BlockPos; import art.arcane.iris.core.nms.container.Pair; +import art.arcane.iris.core.modules.CaveBiomeAnswers; import art.arcane.iris.core.modules.ModuleServices; import art.arcane.iris.engine.IrisComplex; import art.arcane.iris.engine.UpperDimensionContext; @@ -71,7 +72,6 @@ import org.jetbrains.annotations.Nullable; import java.awt.Color; import java.util.Locale; -import java.util.Optional; import java.util.Set; import java.util.UUID; import java.util.concurrent.CompletableFuture; @@ -275,8 +275,8 @@ public interface Engine extends DataProvider, Fallible, BlockUpdater, Renderer, @BlockCoordinates default IrisBiome getCaveBiome(int x, int y, int z) { IrisBiome surfaceBiome = getSurfaceBiome(x, z); - Optional overriddenKey = ModuleServices.overriddenCaveBiome(this, x, y, z); - IrisBiome overriddenBiome = overriddenKey.map(key -> getData().getBiomeLoader().load(key)).orElse(null); + CaveBiomeAnswers caves = ModuleServices.caveBiomes(this); + IrisBiome overriddenBiome = caves.overriddenBiome(x, y, z); if (overriddenBiome != null) { return overriddenBiome; } @@ -292,7 +292,7 @@ public interface Engine extends DataProvider, Fallible, BlockUpdater, Renderer, return surfaceBiome; } - int minDepth = Math.max(0, ModuleServices.caveMinDepthBelowSurface(this, caveBiome.getLoadKey())); + int minDepth = Math.max(0, caves.minDepthBelowSurface(caveBiome.getLoadKey())); if (depthBelowSurface < minDepth) { return surfaceBiome; } diff --git a/core/src/test/java/art/arcane/iris/engine/framework/EngineCaveBiomeTest.java b/core/src/test/java/art/arcane/iris/engine/framework/EngineCaveBiomeTest.java index 168935504..e0f89c988 100644 --- a/core/src/test/java/art/arcane/iris/engine/framework/EngineCaveBiomeTest.java +++ b/core/src/test/java/art/arcane/iris/engine/framework/EngineCaveBiomeTest.java @@ -39,13 +39,17 @@ import org.junit.Before; import org.junit.Test; import java.util.Optional; +import java.util.concurrent.atomic.AtomicInteger; +import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertSame; import static org.mockito.ArgumentMatchers.anyDouble; import static org.mockito.ArgumentMatchers.anyInt; import static org.mockito.Mockito.CALLS_REAL_METHODS; import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; /** @@ -166,6 +170,33 @@ public class EngineCaveBiomeTest { assertSame(cave, engine.getCaveBiome(10, SURFACE_Y - 1, 20)); } + @Test + public void theWorldAsksTheProviderForABiomesDepthOnceAndLoadsAnOverridingBiomeOnce() { + AtomicInteger depthQuestions = new AtomicInteger(); + CaveBiomes counting = caves(null, 10); + serve(new CaveBiomes() { + @Override + public Optional overriddenBiome(PackWorld world, int x, int y, int z) { + return y > SURFACE_Y ? Optional.of("band/deep") : Optional.empty(); + } + + @Override + public int minDepthBelowSurface(PackWorld world, String biomeKey) { + depthQuestions.incrementAndGet(); + return counting.minDepthBelowSurface(world, biomeKey); + } + }); + + for (int y = 0; y < SURFACE_Y; y++) { + engine.getCaveBiome(10, y, 20); + } + assertSame(band, engine.getCaveBiome(10, SURFACE_Y + 5, 20)); + assertSame(band, engine.getCaveBiome(10, SURFACE_Y + 6, 20)); + + assertEquals(1, depthQuestions.get()); + verify(data.getBiomeLoader(), times(1)).load("band/deep"); + } + @Test public void theCarveModulesProviderReadsTheDepthFromTheBiomesSettings() { IrisBiome deep = mock(IrisBiome.class); From 0d9278c0ac36b52ac6c5ee83f990b6f569b24da1 Mon Sep 17 00:00:00 2001 From: piexel Date: Sat, 10 Oct 2026 14:46:22 +0200 Subject: [PATCH 3/4] IrisTerrainSampler answers surfaceY from the height stream alone, without the slope a column works out --- .../iris/core/modules/IrisTerrainSampler.java | 6 ++ .../core/modules/IrisTerrainSamplerTest.java | 74 +++++++++++++++++++ 2 files changed, 80 insertions(+) create mode 100644 core/src/test/java/art/arcane/iris/core/modules/IrisTerrainSamplerTest.java diff --git a/core/src/main/java/art/arcane/iris/core/modules/IrisTerrainSampler.java b/core/src/main/java/art/arcane/iris/core/modules/IrisTerrainSampler.java index b811cd1df..a48dcfd85 100644 --- a/core/src/main/java/art/arcane/iris/core/modules/IrisTerrainSampler.java +++ b/core/src/main/java/art/arcane/iris/core/modules/IrisTerrainSampler.java @@ -70,6 +70,12 @@ final class IrisTerrainSampler implements TerrainSampler { return new TerrainSample(surfaceY, fluidY, slope); } + @Override + public double surfaceY(int worldX, int worldZ) { + requireInside(worldX, bounds.minY(), worldZ); + return engine.getComplex().getHeightStream().get(worldX, worldZ); + } + @Override public BiomeSample biome(int worldX, int y, int worldZ) { requireInside(worldX, y, worldZ); diff --git a/core/src/test/java/art/arcane/iris/core/modules/IrisTerrainSamplerTest.java b/core/src/test/java/art/arcane/iris/core/modules/IrisTerrainSamplerTest.java new file mode 100644 index 000000000..0b274cce9 --- /dev/null +++ b/core/src/test/java/art/arcane/iris/core/modules/IrisTerrainSamplerTest.java @@ -0,0 +1,74 @@ +/* + * Iris is a World Generator for Minecraft Servers + * Copyright (c) 2026 Arcane Arts (Volmit Software) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package art.arcane.iris.core.modules; + +import art.arcane.iris.engine.IrisComplex; +import art.arcane.iris.engine.framework.Engine; +import art.arcane.iris.engine.object.world.dimension.IrisDimension; +import art.arcane.iris.util.project.stream.ProceduralStream; +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThrows; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +public class IrisTerrainSamplerTest { + private static final int HEIGHT = 384; + private static final int CHUNK_REACH = 16; + + private Engine engine; + private IrisComplex complex; + + @Before + public void mockEngine() { + engine = mock(Engine.class); + complex = mock(IrisComplex.class); + when(complex.getHeightStream()).thenReturn(ProceduralStream.ofDouble((x, z) -> 60D + x + z)); + when(engine.getComplex()).thenReturn(complex); + when(engine.getHeight()).thenReturn(HEIGHT); + when(engine.getDimension()).thenReturn(new IrisDimension()); + } + + @Test + public void aColumnsSurfaceAloneIsTheSurfaceOfTheWholeColumn() { + when(complex.getSlopeStream()).thenReturn(ProceduralStream.ofDouble((x, z) -> 0.5D)); + IrisTerrainSampler terrain = new IrisTerrainSampler(engine, null, 0, 0, CHUNK_REACH); + + assertEquals(terrain.column(3, -7).surfaceY(), terrain.surfaceY(3, -7), 0D); + assertEquals(terrain.column(-16, 31).surfaceY(), terrain.surfaceY(-16, 31), 0D); + } + + @Test + public void aColumnsSurfaceAloneLeavesTheSlopeUnworked() { + when(complex.getSlopeStream()).thenThrow(new AssertionError("the slope was worked out")); + IrisTerrainSampler terrain = new IrisTerrainSampler(engine, null, 0, 0, CHUNK_REACH); + + assertEquals(64D, terrain.surfaceY(1, 3), 0D); + } + + @Test + public void aColumnsSurfaceOutsideTheSampledAreaIsRefusedLikeTheColumn() { + IrisTerrainSampler terrain = new IrisTerrainSampler(engine, null, 0, 0, CHUNK_REACH); + + assertThrows(IndexOutOfBoundsException.class, () -> terrain.surfaceY(32, 0)); + assertThrows(IndexOutOfBoundsException.class, () -> terrain.column(32, 0)); + } +} From 8c253ad6dc248478550f76844936d066a5caa224 Mon Sep 17 00:00:00 2001 From: piexel Date: Sat, 10 Oct 2026 14:46:22 +0200 Subject: [PATCH 4/4] PlatformBlockStates remembers the state it read last: chunk scans read long runs of one state --- .../arcane/iris/core/modules/PlatformBlockStates.java | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/core/src/main/java/art/arcane/iris/core/modules/PlatformBlockStates.java b/core/src/main/java/art/arcane/iris/core/modules/PlatformBlockStates.java index 7ce3af137..5f68df286 100644 --- a/core/src/main/java/art/arcane/iris/core/modules/PlatformBlockStates.java +++ b/core/src/main/java/art/arcane/iris/core/modules/PlatformBlockStates.java @@ -46,6 +46,9 @@ public final class PlatformBlockStates { private final Map read = new HashMap<>(); private final Map written = new HashMap<>(); private final Map origins = new IdentityHashMap<>(); + /** The state read last: scans over a chunk read long runs of one state, such as stone or air, so most reads hit it. */ + private PlatformBlockState lastRead; + private BlockState lastReadAsApi; /** {@code platformStates} turns a block state key into the platform's state, such as {@code B::getState}. */ public PlatformBlockStates(Function platformStates) { @@ -60,6 +63,14 @@ public final class PlatformBlockStates { if (state == null) { return AIR; } + if (state != lastRead) { + lastReadAsApi = convertRead(state); + lastRead = state; + } + return lastReadAsApi; + } + + private BlockState convertRead(PlatformBlockState state) { BlockState known = read.get(state); if (known != null) { return known;