Make smartBored return a bored copy instead of changing the pack's object

Boring an object mutated the shared IrisObject, so what a placement saw
depended on which placement bored it first. IrisObject now keeps one bored
copy, made on the first ask and dropped when its blocks change, so later
asks cost a field read. The bore runs under the read lock without
changing the original.
This commit is contained in:
piexel committed 2026-11-06 12:59:35 +01:00
1 parent 699ad3f6fe
commit 0b5b0190f5
5 files changed
+139 -60

No files matched your search

@@ -21,7 +21,6 @@ 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.prefab.IrisObject;
import art.arcane.iris.engine.object.prefab.IrisObjectShaping;
import art.arcane.iris.engine.object.prefab.ScaledObjects;
import art.arcane.iris.spi.IrisLogging;
import art.arcane.iris.util.common.data.B;
@@ -84,9 +83,7 @@ final class IrisPackObjects implements PackObjects {
@Override
public PlaceableObject smartBored(PlaceableObject object) {
IrisObject bored = irisObject(object);
IrisObjectShaping.ensureSmartBored(bored);
return bored;
return irisObject(object).smartBoredCopy();
}
@Override
@@ -78,9 +78,7 @@ public class IrisObject extends IrisRegistrant implements PlaceableObject {
}
public transient final Lock readLock;
protected transient final Lock writeLock;
@Getter
@Setter
protected transient volatile boolean smartBored = false;
private transient final AtomicCache<IrisObject> smartBoredCopy = new AtomicCache<>();
@Setter
protected transient AtomicCache<AxisAlignedBB> aabb = new AtomicCache<>();
transient final AtomicCache<KList<IrisBlockVector>> surfaceSupportOffsets = new AtomicCache<>();
@@ -290,10 +288,23 @@ public class IrisObject extends IrisRegistrant implements PlaceableObject {
return geometry.aquireOrThrow(() -> ObjectGeometry.of(this));
}
/**
* This object with air in its rooms and pockets, a copy made once until the blocks change: this object keeps its blocks,
* and asking again costs nothing. The copy is its own smart bored copy.
*/
public IrisObject smartBoredCopy() {
return smartBoredCopy.aquireOrThrow(() -> {
IrisObject bored = IrisObjectShaping.smartBoredCopyOf(this);
bored.smartBoredCopy.aquireOrThrow(() -> bored);
return bored;
});
}
/** Drops what is computed from the blocks, after they change. */
void forgetDerivedShapes() {
surfaceSupportOffsets.reset();
geometry.reset();
smartBoredCopy.reset();
}
public KList<IrisBlockVector> getSurfaceSupportOffsets() {
@@ -41,52 +41,43 @@ public final class IrisObjectShaping {
private IrisObjectShaping() {
}
public static void ensureSmartBored(IrisObject self) {
if (self.smartBored) {
return;
}
// Whole computation under the object's write lock: two racing callers would otherwise
// both compute, with the loser reading self.blocks while the winner merges into it.
self.writeLock.lock();
try {
if (self.smartBored) {
return;
}
ensureSmartBoredLocked(self);
} finally {
self.writeLock.unlock();
}
}
private static void ensureSmartBoredLocked(IrisObject self) {
/**
* A copy of {@code self} with air in its rooms and pockets, found by casting rays through it. {@code self} keeps its
* blocks, so objects that share it see no change; {@link IrisObject#smartBoredCopy()} keeps the result.
* An object without blocks has nothing to bore and is returned as it is.
*/
static IrisObject smartBoredCopyOf(IrisObject self) {
PrecisionStopwatch p = PrecisionStopwatch.start();
PlatformBlockState vair = IrisObject.States.VAIR;
AtomicInteger applied = new AtomicInteger();
IrisBlockVector max = new IrisBlockVector(Double.MIN_VALUE, Double.MIN_VALUE, Double.MIN_VALUE);
IrisBlockVector min = new IrisBlockVector(Double.MAX_VALUE, Double.MAX_VALUE, Double.MAX_VALUE);
VectorMap<PlatformBlockState> source;
self.readLock.lock();
try {
if (self.blocks.isEmpty()) {
IrisLogging.warn("Cannot Smart Bore " + self.getLoadKey() + " because it has 0 blocks in it.");
self.smartBored = true;
return;
return self;
}
source = self.blocks;
for (IrisBlockVector i : self.blocks.keys()) {
max.setX(Math.max(i.getX(), max.getX()));
min.setX(Math.min(i.getX(), min.getX()));
max.setY(Math.max(i.getY(), max.getY()));
min.setY(Math.min(i.getY(), min.getY()));
max.setZ(Math.max(i.getZ(), max.getZ()));
min.setZ(Math.min(i.getZ(), min.getZ()));
}
AtomicInteger applied = new AtomicInteger();
VectorMap<PlatformBlockState> bore = boreVolume(self.blocks, applied);
IrisObject bored = self.copy();
bore.forEach((v, s) -> bored.blocks.computeIfAbsent(v, (vv) -> s));
IrisLogging.debug("Smart Bore: " + self.getLoadKey() + " in " + Form.duration(p.getMilliseconds(), 2) + " (" + Form.f(applied.get()) + ")");
return bored;
} finally {
self.readLock.unlock();
}
}
private static VectorMap<PlatformBlockState> boreVolume(VectorMap<PlatformBlockState> source, AtomicInteger applied) {
PlatformBlockState vair = IrisObject.States.VAIR;
IrisBlockVector max = new IrisBlockVector(Double.MIN_VALUE, Double.MIN_VALUE, Double.MIN_VALUE);
IrisBlockVector min = new IrisBlockVector(Double.MAX_VALUE, Double.MAX_VALUE, Double.MAX_VALUE);
for (IrisBlockVector i : source.keys()) {
max.setX(Math.max(i.getX(), max.getX()));
min.setX(Math.min(i.getX(), min.getX()));
max.setY(Math.max(i.getY(), max.getY()));
min.setY(Math.min(i.getY(), min.getY()));
max.setZ(Math.max(i.getZ(), max.getZ()));
min.setZ(Math.min(i.getZ(), min.getZ()));
}
VectorMap<PlatformBlockState> bore = new VectorMap<>();
// Inline on purpose: the three axis passes read AND write the shared bore map, so the
@@ -169,19 +160,7 @@ public final class IrisObjectShaping {
}
burst.complete();
self.writeLock.lock();
try {
if (!self.smartBored) {
bore.forEach((v, s) -> self.blocks.computeIfAbsent(v, (vv) -> s));
self.forgetDerivedShapes();
self.smartBored = true;
}
} finally {
self.writeLock.unlock();
}
IrisLogging.debug("Smart Bore: " + self.getLoadKey() + " in " + Form.duration(p.getMilliseconds(), 2) + " (" + Form.f(applied.get()) + ")");
return bore;
}
private static boolean boreContains(VectorMap<PlatformBlockState> source, VectorMap<PlatformBlockState> bore, IrisBlockVector v) {
@@ -0,0 +1,87 @@
package art.arcane.iris.core.modules;
import art.arcane.iris.core.IrisSettings;
import art.arcane.iris.core.loader.IrisData;
import art.arcane.iris.engine.UsableBlockPlatform;
import art.arcane.iris.engine.object.prefab.IrisObject;
import com.volmit.object.PackObjects;
import com.volmit.object.PlaceableObject;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertSame;
import static org.mockito.Mockito.mock;
/** Smart boring hands back a bored copy and leaves the pack's own object as it was. */
public class IrisPackObjectsSmartBoredTest {
private static final int SIZE = 3;
private static final int SHELL_BLOCKS = SIZE * SIZE * SIZE - 1;
private static final int MIDDLE = 1;
private final PackObjects objects = new IrisPackObjects(mock(IrisData.class), mock(IrisPackResources.class));
private static IrisSettings previousSettings;
@BeforeClass
public static void bindPlatform() {
UsableBlockPlatform.bind();
previousSettings = IrisSettings.settings;
IrisSettings.settings = new IrisSettings();
}
@AfterClass
public static void unbindPlatform() {
IrisSettings.settings = previousSettings;
UsableBlockPlatform.unbind();
}
@Test
public void theBoredObjectIsACopyAndThePacksObjectKeepsItsBlocks() {
IrisObject shell = hollowCube();
PlaceableObject bored = objects.smartBored(shell);
assertNotSame(shell, bored);
assertEquals(SHELL_BLOCKS, shell.getBlocks().size());
assertEquals(SHELL_BLOCKS + 1, ((IrisObject) bored).getBlocks().size());
assertEquals(IrisObject.States.VAIR, ((IrisObject) bored).getBlocks().get(shell.getSigned(MIDDLE, MIDDLE, MIDDLE)));
}
@Test
public void boringAgainGivesTheSameCopyWithoutBoringAgain() {
IrisObject shell = hollowCube();
PlaceableObject bored = objects.smartBored(shell);
assertSame(bored, objects.smartBored(shell));
assertSame(bored, objects.smartBored(bored));
}
@Test
public void changingTheObjectMakesTheNextBoredCopyFromItsNewBlocks() {
IrisObject shell = hollowCube();
PlaceableObject before = objects.smartBored(shell);
shell.setUnsigned(0, 0, 0, null);
PlaceableObject after = objects.smartBored(shell);
assertNotSame(before, after);
}
private static IrisObject hollowCube() {
IrisObject cube = new IrisObject(SIZE, SIZE, SIZE);
for (int x = 0; x < SIZE; x++) {
for (int y = 0; y < SIZE; y++) {
for (int z = 0; z < SIZE; z++) {
if (x != MIDDLE || y != MIDDLE || z != MIDDLE) {
cube.setUnsigned(x, y, z, IrisObject.States.STONE);
}
}
}
}
return cube;
}
}
@@ -18,6 +18,7 @@ import art.arcane.iris.builtin.prepare.ObjectRotation;
import art.arcane.iris.builtin.prepare.ObjectTranslate;
import art.arcane.iris.engine.object.prefab.TileData;
import art.arcane.iris.engine.object.terrain.carving.CarveResult;
import art.arcane.iris.core.IrisSettings;
import art.arcane.iris.core.loader.IrisData;
import art.arcane.iris.core.modules.BuiltInPack;
import art.arcane.iris.core.modules.InMemoryMantle;
@@ -246,16 +247,20 @@ public class ObjectPlacementRegressionTest {
public void debugPlacementDoesNotMutateSmartBoreCache() {
IrisObject object = new IrisObject(1, 1, 1);
object.setUnsigned(0, 0, 0, IrisObject.States.VAIR);
object.setSmartBored(true);
ObjectPlacement.Builder placement = placement();
placement.smartBore(true);
RecordingPlacer debug = new RecordingPlacer(engine);
RecordingPlacer normal = new RecordingPlacer(null);
SmartBoreDebugSettings shown = TestSettings.of(SmartBoreDebugSettings.class, "{\"show\":true}");
when(engine.getDimension().getModuleSettings()).thenReturn(BuiltInPack.settings(shown));
placed(object, 0, ANCHOR_Y, 0, debug, placement.build(), new RNG(2L), data);
placed(object, 0, ANCHOR_Y, 0, normal, placement.build(), new RNG(2L), data);
IrisSettings previousSettings = IrisSettings.settings;
IrisSettings.settings = new IrisSettings();
try {
placed(object, 0, ANCHOR_Y, 0, debug, placement.build(), new RNG(2L), data);
placed(object, 0, ANCHOR_Y, 0, normal, placement.build(), new RNG(2L), data);
} finally {
IrisSettings.settings = previousSettings;
}
PlatformBlockState cachedState = object.getBlocks().get(object.getSigned(0, 0, 0));
assertSame(IrisObject.States.VAIR, cachedState);