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) {