Skip to content

Commit

Permalink
Implement faster block access in tests and jmh
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexProgrammerDE committed Dec 19, 2024
1 parent dd3d8ea commit 429f929
Showing 1 changed file with 35 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,16 @@
import com.soulfiremc.server.data.BlockState;
import com.soulfiremc.server.data.BlockType;
import com.soulfiremc.server.protocol.bot.block.BlockAccessor;
import com.soulfiremc.server.util.VectorHelper;
import it.unimi.dsi.fastutil.longs.Long2ObjectMap;
import it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap;
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
import org.cloudburstmc.math.vector.Vector3i;

import java.util.Map;

public class TestBlockAccessorBuilder {
private final Long2ObjectMap<BlockState> blocks = new Long2ObjectOpenHashMap<>();
private final Map<Vector3i, BlockState> blocks = new Object2ObjectOpenHashMap<>();
private final BlockState defaultBlock;
private int minX = Integer.MAX_VALUE, minY = Integer.MAX_VALUE, minZ = Integer.MAX_VALUE;
private int maxX = Integer.MIN_VALUE, maxY = Integer.MIN_VALUE, maxZ = Integer.MIN_VALUE;

public TestBlockAccessorBuilder() {
this(BlockState.forDefaultBlockType(BlockType.AIR));
Expand All @@ -37,17 +40,41 @@ public TestBlockAccessorBuilder(BlockState defaultBlock) {
}

public void setBlockAt(int x, int y, int z, BlockType block) {
blocks.put(VectorHelper.asLong(x, y, z), BlockState.forDefaultBlockType(block));
blocks.put(Vector3i.from(x, y, z), BlockState.forDefaultBlockType(block));
minX = Math.min(minX, x);
minY = Math.min(minY, y);
minZ = Math.min(minZ, z);
maxX = Math.max(maxX, x);
maxY = Math.max(maxY, y);
maxZ = Math.max(maxZ, z);
}

public BlockAccessor build() {
return new TestBlockAccessor(blocks, defaultBlock);
int sizeX = maxX - minX + 1;
int sizeY = maxY - minY + 1;
int sizeZ = maxZ - minZ + 1;
BlockState[][][] arrayBlocks = new BlockState[sizeX][sizeY][sizeZ];

for (Map.Entry<Vector3i, BlockState> entry : blocks.entrySet()) {
Vector3i pos = entry.getKey();
arrayBlocks[pos.getX() - minX][pos.getY() - minY][pos.getZ() - minZ] = entry.getValue();
}

return new TestBlockAccessor(arrayBlocks, defaultBlock, -minX, -minY, -minZ);
}

private record TestBlockAccessor(Long2ObjectMap<BlockState> blocks, BlockState defaultBlock) implements BlockAccessor {
private record TestBlockAccessor(BlockState[][][] blocks, BlockState defaultBlock, int offsetX, int offsetY, int offsetZ) implements BlockAccessor {
@Override
public BlockState getBlockState(int x, int y, int z) {
return blocks.getOrDefault(VectorHelper.asLong(x, y, z), defaultBlock);
int adjX = x + offsetX;
int adjY = y + offsetY;
int adjZ = z + offsetZ;
if (adjX < 0 || adjX >= blocks.length || adjY < 0 || adjY >= blocks[0].length || adjZ < 0 || adjZ >= blocks[0][0].length) {
return defaultBlock;
}

BlockState value = blocks[adjX][adjY][adjZ];
return value != null ? value : defaultBlock;
}
}
}

0 comments on commit 429f929

Please sign in to comment.