Memoize object block states

- Cache vanilla withProperty variants per state
- Cache rotated states per rotation and quantized spin
- Hoist the stilt dirt state and rotate only target-layer stilt blocks
This commit is contained in:
Brian Neumann-Fopiano committed 2026-09-28 11:22:51 -04:00
1 parent ec07f55fd8
commit 2f8d3b1d1a
6 files changed
+115 -7

No files matched your search

@@ -54,6 +54,7 @@ import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.mockingDetails;
public class BukkitSpiConformanceTest {
@BeforeClass
@@ -181,6 +182,23 @@ public class BukkitSpiConformanceTest {
assertEquals("iristest:append_block[axis=y,lit=true]", merged.key());
}
@Test
public void withPropertyParsesEachVariantOnce() {
String merged = "iristest:memo_leaves[distance=1,persistent=true]";
NativeBlockState state = BukkitBlockState.of(blockData("iristest:memo_leaves[distance=1,persistent=false]"));
NativeBlockState first = state.withProperty("persistent", "true");
NativeBlockState second = state.withProperty("persistent", "true");
assertSame(first, second);
assertEquals(merged, first.key());
long parses = mockingDetails(Bukkit.getServer()).getInvocations().stream()
.filter(invocation -> invocation.getMethod().getName().equals("createBlockData")
&& merged.equals(invocation.getArgument(0)))
.count();
assertEquals(1L, parses);
}
@Test
public void withPropertyAddsBracketSectionWhenAbsent() {
BlockData data = blockData("iristest:bare_block");
@@ -0,0 +1,58 @@
package art.arcane.iris.structure.object;
import art.arcane.iris.platform.bukkit.BukkitBlockState;
import art.arcane.iris.testsupport.BukkitTestServer;
import art.arcane.volmlib.nativelib.terrain.NativeBlockState;
import org.bukkit.Axis;
import org.bukkit.block.data.BlockData;
import org.bukkit.block.data.Orientable;
import org.junit.BeforeClass;
import org.junit.Test;
import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertSame;
import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.withSettings;
public class IrisObjectRotationStateMemoTest {
@BeforeClass
public static void installServer() {
BukkitTestServer.install();
}
@Test
public void rotatedStatesAreComputedOncePerQuantizedSpin() {
AtomicInteger clones = new AtomicInteger();
BlockData original = log("iristest:memo_log[axis=x]", Axis.X);
doAnswer(invocation -> {
clones.incrementAndGet();
return log("iristest:memo_log[axis=x]", Axis.X);
}).when(original).clone();
NativeBlockState state = BukkitBlockState.of(original);
IrisObjectRotation rotation = IrisObjectRotation.of(0, 90, 0);
NativeBlockState first = rotation.rotate(state, 0, 0, 0);
NativeBlockState repeated = rotation.rotate(state, 0, 0, 0);
rotation.rotate(state, 0, 30, 0);
rotation.rotate(state, 0, 90, 0);
assertSame(first, repeated);
assertEquals(2, clones.get());
assertEquals("iristest:memo_log[axis=x]", first.key());
}
private static BlockData log(String key, Axis axis) {
BlockData data = mock(BlockData.class, withSettings().extraInterfaces(Orientable.class));
doReturn(key).when(data).getAsString();
doReturn(key).when(data).getAsString(anyBoolean());
doReturn(axis).when((Orientable) data).getAxis();
doReturn(Set.of(Axis.X, Axis.Y, Axis.Z)).when((Orientable) data).getAxes();
return data;
}
}