diff --git a/JAVA_MIGRATION_PROFILE.md b/JAVA_MIGRATION_PROFILE.md index 3f8e28d..8e717a9 100644 --- a/JAVA_MIGRATION_PROFILE.md +++ b/JAVA_MIGRATION_PROFILE.md @@ -39,6 +39,8 @@ iterations per phase: - `updateEntity`: one `TileMultipart.updateEntity()` call, dispatching `update()` to eight ticking parts; - `operate`: one `TileMultipart.operate(Function1)` call with one reused function and eight bound parts; +- `lightValue`: one `TileMultipart.getLightValue()` call across eight parts; +- `getTile`: one `BlockMultipart.getTile()` lookup returning the same non-empty eight-part tile; - `redstoneQueries`: one `strongPowerLevel`, one `getConnectionMask`, and one masked `weakPowerLevel` call on a generated `TRedstoneTile` containing eight `IRedstonePart` implementations. @@ -150,3 +152,18 @@ required no generator change. The two focused steady-state allocation targets identified by this workload are now resolved: `TileMultipart.operate` and generated redstone queries are effectively allocation-free on their normal immutable-list paths. Further optimization should follow a new representative profile rather than extending this synthetic workload speculatively. + +## Multipart read-path baseline captured 2026-08-28 + +A consumer-audit sanity check identified two Java-port allocations that the original three-phase workload did not +exercise. Focused tests now pin empty/non-empty `BlockMultipart.getTile`, direct ordered `Seq` indexing, and read +queries over the mutable `Seq` implementations accepted by the public setter. The two matching profile phases measured: + +| Phase | Elapsed | Operations/s | Allocated bytes | Bytes/operation | +| --- | ---: | ---: | ---: | ---: | +| `lightValue` | 3.170 s | 15,772,691 | 9,196,067,864 | 183.9 | +| `getTile` | 0.271 s | 184,225,439 | 1,200,000,000 | 24.0 | + +`lightValue` pays for `TileMultipart.parts()`'s copied `ArrayList`; `getTile` pays for the Java list wrapper returned by +`jPartList()`. Both are absent from the reference Scala implementation, which reads the published `Seq` directly. +Mutation paths still require a snapshot before publishing a replacement immutable `Seq`. diff --git a/src/functionalTest/java/codechicken/multipart/test/ForgeMultipartProfileWorkload.java b/src/functionalTest/java/codechicken/multipart/test/ForgeMultipartProfileWorkload.java index 6e8f127..89fdab8 100644 --- a/src/functionalTest/java/codechicken/multipart/test/ForgeMultipartProfileWorkload.java +++ b/src/functionalTest/java/codechicken/multipart/test/ForgeMultipartProfileWorkload.java @@ -8,6 +8,13 @@ import java.util.ArrayList; import java.util.List; import java.util.Locale; +import net.minecraft.block.Block; +import net.minecraft.tileentity.TileEntity; +import net.minecraft.world.IBlockAccess; +import net.minecraft.world.biome.BiomeGenBase; +import net.minecraftforge.common.util.ForgeDirection; + +import codechicken.multipart.BlockMultipart; import codechicken.multipart.IRedstonePart; import codechicken.multipart.IRedstoneTile; import codechicken.multipart.MultipartHelper; @@ -32,6 +39,7 @@ final class ForgeMultipartProfileWorkload { tickingParts.add(new ProfilePart()); } TileMultipart tickingTile = MultipartHelper.createTileFromParts(tickingParts); + ProfileBlockAccess blockAccess = new ProfileBlockAccess(tickingTile); List redstoneParts = new ArrayList<>(); for (int i = 0; i < PART_COUNT; i++) { @@ -45,6 +53,8 @@ final class ForgeMultipartProfileWorkload { profileUpdateEntity(tickingTile, WARMUP_ITERATIONS); profileOperate(tickingTile, operation, WARMUP_ITERATIONS); + profileLightValue(tickingTile, WARMUP_ITERATIONS); + profileGetTile(blockAccess, WARMUP_ITERATIONS); profileRedstone(redstoneTile, redstone, WARMUP_ITERATIONS); long updateStartMillis = System.currentTimeMillis(); @@ -61,6 +71,20 @@ final class ForgeMultipartProfileWorkload { long operateNanos = System.nanoTime() - operateStart; long operateBytes = allocationBean.getThreadAllocatedBytes(threadId) - operateStartBytes; + long lightStartMillis = System.currentTimeMillis(); + long lightStartBytes = allocationBean.getThreadAllocatedBytes(threadId); + long lightStart = System.nanoTime(); + long lightChecksum = profileLightValue(tickingTile, PROFILE_ITERATIONS); + long lightNanos = System.nanoTime() - lightStart; + long lightBytes = allocationBean.getThreadAllocatedBytes(threadId) - lightStartBytes; + + long getTileStartMillis = System.currentTimeMillis(); + long getTileStartBytes = allocationBean.getThreadAllocatedBytes(threadId); + long getTileStart = System.nanoTime(); + long getTileChecksum = profileGetTile(blockAccess, PROFILE_ITERATIONS); + long getTileNanos = System.nanoTime() - getTileStart; + long getTileBytes = allocationBean.getThreadAllocatedBytes(threadId) - getTileStartBytes; + long redstoneStartMillis = System.currentTimeMillis(); long redstoneStartBytes = allocationBean.getThreadAllocatedBytes(threadId); long redstoneStart = System.nanoTime(); @@ -72,7 +96,7 @@ final class ForgeMultipartProfileWorkload { for (TMultiPart part : tickingParts) { updateChecksum += ((ProfilePart) part).updates; } - sink = updateChecksum + operation.applications + redstoneChecksum; + sink = updateChecksum + operation.applications + lightChecksum + getTileChecksum + redstoneChecksum; writeResults( updateStartMillis, updateNanos, @@ -80,6 +104,12 @@ final class ForgeMultipartProfileWorkload { operateStartMillis, operateNanos, operateBytes, + lightStartMillis, + lightNanos, + lightBytes, + getTileStartMillis, + getTileNanos, + getTileBytes, redstoneStartMillis, redstoneNanos, redstoneBytes, @@ -98,6 +128,24 @@ final class ForgeMultipartProfileWorkload { } } + private static long profileLightValue(TileMultipart tile, int iterations) { + long checksum = 0; + for (int i = 0; i < iterations; i++) { + checksum += tile.getLightValue(); + } + return checksum; + } + + private static long profileGetTile(IBlockAccess world, int iterations) { + long checksum = 0; + for (int i = 0; i < iterations; i++) { + if (BlockMultipart.getTile(world, 0, 0, 0) != null) { + checksum++; + } + } + return checksum; + } + private static long profileRedstone(TileMultipart tile, IRedstoneTile redstone, int iterations) { long checksum = 0; for (int i = 0; i < iterations; i++) { @@ -110,8 +158,9 @@ final class ForgeMultipartProfileWorkload { } private static void writeResults(long updateStartMillis, long updateNanos, long updateBytes, - long operateStartMillis, long operateNanos, long operateBytes, long redstoneStartMillis, long redstoneNanos, - long redstoneBytes, long checksum) { + long operateStartMillis, long operateNanos, long operateBytes, long lightStartMillis, long lightNanos, + long lightBytes, long getTileStartMillis, long getTileNanos, long getTileBytes, long redstoneStartMillis, + long redstoneNanos, long redstoneBytes, long checksum) { File output = new File("forgemultipart-profile.txt"); try (PrintWriter writer = new PrintWriter(output)) { writer.printf(Locale.ROOT, "java=%s%n", System.getProperty("java.runtime.version")); @@ -120,6 +169,8 @@ final class ForgeMultipartProfileWorkload { writer.printf(Locale.ROOT, "profileIterations=%d%n", PROFILE_ITERATIONS); writeTiming(writer, "updateEntity", updateStartMillis, updateNanos, updateBytes); writeTiming(writer, "operate", operateStartMillis, operateNanos, operateBytes); + writeTiming(writer, "lightValue", lightStartMillis, lightNanos, lightBytes); + writeTiming(writer, "getTile", getTileStartMillis, getTileNanos, getTileBytes); writeTiming(writer, "redstoneQueries", redstoneStartMillis, redstoneNanos, redstoneBytes); writer.printf(Locale.ROOT, "checksum=%d%n", checksum); } catch (FileNotFoundException e) { @@ -210,4 +261,63 @@ final class ForgeMultipartProfileWorkload { return BoxedUnit.UNIT; } } + + private static final class ProfileBlockAccess implements IBlockAccess { + + private final TileEntity tile; + + private ProfileBlockAccess(TileEntity tile) { + this.tile = tile; + } + + @Override + public Block getBlock(int x, int y, int z) { + return null; + } + + @Override + public TileEntity getTileEntity(int x, int y, int z) { + return tile; + } + + @Override + public int getLightBrightnessForSkyBlocks(int x, int y, int z, int minimum) { + return 0; + } + + @Override + public int getBlockMetadata(int x, int y, int z) { + return 0; + } + + @Override + public int isBlockProvidingPowerTo(int x, int y, int z, int direction) { + return 0; + } + + @Override + public boolean isAirBlock(int x, int y, int z) { + return false; + } + + @Override + public BiomeGenBase getBiomeGenForCoords(int x, int z) { + return null; + } + + @Override + public int getHeight() { + return 0; + } + + @Override + public boolean extendedLevelsInChunkCache() { + return false; + } + + @Override + public boolean isSideSolid(int x, int y, int z, ForgeDirection side, boolean defaultValue) { + return defaultValue; + } + } } diff --git a/src/test/java/codechicken/multipart/BlockMultipartCharacterizationTest.java b/src/test/java/codechicken/multipart/BlockMultipartCharacterizationTest.java new file mode 100644 index 0000000..1b0ed31 --- /dev/null +++ b/src/test/java/codechicken/multipart/BlockMultipartCharacterizationTest.java @@ -0,0 +1,95 @@ +package codechicken.multipart; + +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertSame; + +import net.minecraft.block.Block; +import net.minecraft.tileentity.TileEntity; +import net.minecraft.world.IBlockAccess; +import net.minecraft.world.biome.BiomeGenBase; +import net.minecraftforge.common.util.ForgeDirection; + +import org.junit.jupiter.api.Test; + +class BlockMultipartCharacterizationTest { + + @Test + void getTileOnlyReturnsNonEmptyMultipartTiles() { + TestBlockAccess world = new TestBlockAccess(); + + assertNull(BlockMultipart.getTile(world, 1, 2, 3)); + + world.tile = new TileEntity(); + assertNull(BlockMultipart.getTile(world, 1, 2, 3)); + + TileMultipart multipart = new TileMultipart(); + world.tile = multipart; + assertNull(BlockMultipart.getTile(world, 1, 2, 3)); + + multipart.addPart_do(new TestPart()); + assertSame(multipart, BlockMultipart.getTile(world, 1, 2, 3)); + } + + private static final class TestPart extends TMultiPart { + + @Override + public String getType() { + return "block_characterization"; + } + } + + private static final class TestBlockAccess implements IBlockAccess { + + private TileEntity tile; + + @Override + public Block getBlock(int x, int y, int z) { + return null; + } + + @Override + public TileEntity getTileEntity(int x, int y, int z) { + return tile; + } + + @Override + public int getLightBrightnessForSkyBlocks(int x, int y, int z, int minimum) { + return 0; + } + + @Override + public int getBlockMetadata(int x, int y, int z) { + return 0; + } + + @Override + public int isBlockProvidingPowerTo(int x, int y, int z, int direction) { + return 0; + } + + @Override + public boolean isAirBlock(int x, int y, int z) { + return false; + } + + @Override + public BiomeGenBase getBiomeGenForCoords(int x, int z) { + return null; + } + + @Override + public int getHeight() { + return 0; + } + + @Override + public boolean extendedLevelsInChunkCache() { + return false; + } + + @Override + public boolean isSideSolid(int x, int y, int z, ForgeDirection side, boolean defaultValue) { + return defaultValue; + } + } +} diff --git a/src/test/java/codechicken/multipart/TileMultipartCharacterizationTest.java b/src/test/java/codechicken/multipart/TileMultipartCharacterizationTest.java index e235925..14b6c78 100644 --- a/src/test/java/codechicken/multipart/TileMultipartCharacterizationTest.java +++ b/src/test/java/codechicken/multipart/TileMultipartCharacterizationTest.java @@ -73,6 +73,28 @@ class TileMultipartCharacterizationTest { assertEquals(1, tile.jPartList().size()); assertSame(part, tile.jPartList().get(0)); + assertSame(part, tile.partList().apply(0)); + } + + @Test + void readQueriesAndPublishedViewsAcceptAMutableSeq() { + TileMultipart tile = new TileMultipart(); + List backing = new ArrayList<>(); + backing.add(new LightPart(4)); + tile.partList_$eq(JavaConversions.asScalaBuffer(backing)); + List publishedView = tile.jPartList(); + + assertEquals(4, tile.getLightValue()); + assertFalse(tile.canPlaceTorchOnTop()); + + TorchSupportingPart support = new TorchSupportingPart(); + backing.add(new LightPart(11)); + backing.add(support); + + assertEquals(3, publishedView.size(), "The Java ABI exposes a live view of the supplied Seq"); + assertSame(support, tile.partList().apply(2)); + assertEquals(11, tile.getLightValue()); + assertTrue(tile.canPlaceTorchOnTop()); } @Test