Paper/patches/server/0069-Optimize-isValidLocation-getType-and-getBlockData-fo.patch

194 lines
9.5 KiB
Diff
Raw Normal View History

2021-06-11 14:02:28 +02:00
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Aikar <aikar@aikar.co>
Date: Thu, 3 Mar 2016 02:07:55 -0600
Subject: [PATCH] Optimize isValidLocation, getType and getBlockData for
inlining
Hot methods, so reduce # of instructions for the method.
Move is valid location test to the BlockPosition class so that it can access local variables.
Replace all calls to the new place to the unnecessary forward.
Optimize getType and getBlockData to manually inline and optimize the calls
diff --git a/src/main/java/net/minecraft/core/Vec3i.java b/src/main/java/net/minecraft/core/Vec3i.java
2021-06-17 23:39:36 +02:00
index 964bec1d3fecfe43a43bd9968159fca069f00dfd..e188f130e47ef319477050981de53a7410452592 100644
2021-06-11 14:02:28 +02:00
--- a/src/main/java/net/minecraft/core/Vec3i.java
+++ b/src/main/java/net/minecraft/core/Vec3i.java
2021-06-12 04:24:43 +02:00
@@ -21,6 +21,15 @@ public class Vec3i implements Comparable<Vec3i> {
private int y;
private int z;
2021-06-11 14:02:28 +02:00
+ // Paper start
2021-06-14 17:10:25 +02:00
+ public boolean isValidLocation(net.minecraft.world.level.LevelHeightAccessor levelHeightAccessor) {
+ return getX() >= -30000000 && getZ() >= -30000000 && getX() < 30000000 && getZ() < 30000000 && !levelHeightAccessor.isOutsideBuildHeight(getY());
2021-06-11 14:02:28 +02:00
+ }
2021-06-14 17:10:25 +02:00
+ public boolean isInvalidYLocation(net.minecraft.world.level.LevelHeightAccessor levelHeightAccessor) {
+ return levelHeightAccessor.isOutsideBuildHeight(getY());
2021-06-11 14:02:28 +02:00
+ }
+ // Paper end
+
public Vec3i(int x, int y, int z) {
this.x = x;
this.y = y;
diff --git a/src/main/java/net/minecraft/world/level/Level.java b/src/main/java/net/minecraft/world/level/Level.java
2021-06-17 23:39:36 +02:00
index 8592de5a5541fbff8ec85bfce8635a780097c4d4..bbe2d5eab844880f2fde674aa1c78d60d6152231 100644
2021-06-11 14:02:28 +02:00
--- a/src/main/java/net/minecraft/world/level/Level.java
+++ b/src/main/java/net/minecraft/world/level/Level.java
2021-06-12 04:24:43 +02:00
@@ -259,7 +259,7 @@ public abstract class Level implements LevelAccessor, AutoCloseable {
2021-06-11 14:02:28 +02:00
}
2021-06-12 04:24:43 +02:00
public boolean isInWorldBounds(BlockPos pos) {
- return !this.isOutsideBuildHeight(pos) && Level.isInWorldBoundsHorizontal(pos);
+ return pos.isValidLocation(this); // Paper - use better/optimized check
2021-06-11 14:02:28 +02:00
}
public static boolean isInSpawnableBounds(BlockPos pos) {
diff --git a/src/main/java/net/minecraft/world/level/chunk/ChunkAccess.java b/src/main/java/net/minecraft/world/level/chunk/ChunkAccess.java
2021-06-12 04:24:43 +02:00
index 6a3f98edbc2b4056c5baf00277caee327e444a77..974ab04b08bbd3c27a394b37c1af112be5f28f43 100644
2021-06-11 14:02:28 +02:00
--- a/src/main/java/net/minecraft/world/level/chunk/ChunkAccess.java
+++ b/src/main/java/net/minecraft/world/level/chunk/ChunkAccess.java
2021-06-12 04:24:43 +02:00
@@ -29,6 +29,7 @@ public interface ChunkAccess extends BlockGetter, FeatureAccess {
return GameEventDispatcher.NOOP;
}
2021-06-11 14:02:28 +02:00
+ BlockState getType(final int x, final int y, final int z); // Paper
@Nullable
BlockState setBlockState(BlockPos pos, BlockState state, boolean moved);
diff --git a/src/main/java/net/minecraft/world/level/chunk/EmptyLevelChunk.java b/src/main/java/net/minecraft/world/level/chunk/EmptyLevelChunk.java
2021-06-12 04:24:43 +02:00
index 84ebfdfc4350fb57ca2959e000b33b8d5efa6e0b..69c2454533e6f21c70792b555ec02c6bc6d169b3 100644
2021-06-11 14:02:28 +02:00
--- a/src/main/java/net/minecraft/world/level/chunk/EmptyLevelChunk.java
+++ b/src/main/java/net/minecraft/world/level/chunk/EmptyLevelChunk.java
2021-06-12 04:24:43 +02:00
@@ -19,6 +19,11 @@ public class EmptyLevelChunk extends LevelChunk {
super(world, pos, new EmptyLevelChunk.EmptyChunkBiomeContainer(world));
2021-06-11 14:02:28 +02:00
}
+ // Paper start
+ @Override public BlockState getType(int x, int y, int z) {
+ return Blocks.VOID_AIR.defaultBlockState();
+ }
+ // Paper end
@Override
public BlockState getBlockState(BlockPos pos) {
return Blocks.VOID_AIR.defaultBlockState();
diff --git a/src/main/java/net/minecraft/world/level/chunk/ImposterProtoChunk.java b/src/main/java/net/minecraft/world/level/chunk/ImposterProtoChunk.java
2021-06-12 04:24:43 +02:00
index c1beb6d5fc3cabfeacf0ffbf563e53ff7984c5d3..452b513e8b89d865a396066adaf4feb1140e1c62 100644
2021-06-11 14:02:28 +02:00
--- a/src/main/java/net/minecraft/world/level/chunk/ImposterProtoChunk.java
+++ b/src/main/java/net/minecraft/world/level/chunk/ImposterProtoChunk.java
2021-06-12 04:24:43 +02:00
@@ -40,6 +40,11 @@ public class ImposterProtoChunk extends ProtoChunk {
2021-06-11 14:02:28 +02:00
public BlockState getBlockState(BlockPos pos) {
return this.wrapped.getBlockState(pos);
}
+ // Paper start
+ public final BlockState getType(final int x, final int y, final int z) {
+ return this.wrapped.getBlockData(x, y, z);
+ }
+ // Paper end
@Override
public FluidState getFluidState(BlockPos pos) {
diff --git a/src/main/java/net/minecraft/world/level/chunk/LevelChunk.java b/src/main/java/net/minecraft/world/level/chunk/LevelChunk.java
2021-06-17 23:39:36 +02:00
index 249f39bc0c5820e313cad7813bd1087cb2e09ed6..b737ffb8af88e7978c3bfc0b2e61639aca3376c0 100644
2021-06-11 14:02:28 +02:00
--- a/src/main/java/net/minecraft/world/level/chunk/LevelChunk.java
+++ b/src/main/java/net/minecraft/world/level/chunk/LevelChunk.java
2021-06-12 04:24:43 +02:00
@@ -337,12 +337,28 @@ public class LevelChunk implements ChunkAccess {
2021-06-11 14:02:28 +02:00
return this.sections;
}
+ // Paper start - Optimize getBlockData to reduce instructions
2021-06-12 04:24:43 +02:00
@Override
2021-06-11 14:02:28 +02:00
public BlockState getBlockState(BlockPos pos) {
- int i = pos.getX();
- int j = pos.getY();
- int k = pos.getZ();
+ return this.getBlockData(pos.getX(), pos.getY(), pos.getZ());
+ }
2021-06-12 04:24:43 +02:00
+
2021-06-11 14:02:28 +02:00
+ public BlockState getType(final int x, final int y, final int z) {
2021-06-12 04:24:43 +02:00
+ return this.getBlockData(x, y, z);
2021-06-11 14:02:28 +02:00
+ }
+ public final BlockState getBlockData(final int x, final int y, final int z) {
+ // Method body / logic copied from below
2021-06-12 04:24:43 +02:00
+ final int i = this.getSectionIndex(y);
+ if (i < 0 || i >= this.sections.length || this.sections[i] == null || this.sections[i].nonEmptyBlockCount == 0) {
2021-06-11 14:02:28 +02:00
+ return Blocks.AIR.defaultBlockState();
+ }
+ // Inlined ChunkSection.getType() and DataPaletteBlock.a(int,int,int)
+ return this.sections[i].states.get((y & 15) << 8 | (z & 15) << 4 | x & 15);
2021-06-12 04:24:43 +02:00
2021-06-11 14:02:28 +02:00
+ }
+
+ public BlockState getBlockData_unused(int i, int j, int k) {
+ // Paper end
2021-06-12 04:24:43 +02:00
if (this.level.isDebug()) {
2021-06-11 14:02:28 +02:00
BlockState iblockdata = null;
diff --git a/src/main/java/net/minecraft/world/level/chunk/LevelChunkSection.java b/src/main/java/net/minecraft/world/level/chunk/LevelChunkSection.java
2021-06-12 04:24:43 +02:00
index 03fd5684aec8fa0d87963f2adcd8244e92840917..5fd66020a937b641e2a060cf38df731a43f3bf55 100644
2021-06-11 14:02:28 +02:00
--- a/src/main/java/net/minecraft/world/level/chunk/LevelChunkSection.java
+++ b/src/main/java/net/minecraft/world/level/chunk/LevelChunkSection.java
2021-06-12 04:24:43 +02:00
@@ -15,10 +15,10 @@ public class LevelChunkSection {
public static final int SECTION_SIZE = 4096;
2021-06-11 14:02:28 +02:00
public static final Palette<BlockState> GLOBAL_BLOCKSTATE_PALETTE = new GlobalPalette<>(Block.BLOCK_STATE_REGISTRY, Blocks.AIR.defaultBlockState());
private final int bottomBlockY;
- private short nonEmptyBlockCount;
+ short nonEmptyBlockCount; // Paper - package-private
private short tickingBlockCount;
private short tickingFluidCount;
- private final PalettedContainer<BlockState> states;
+ final PalettedContainer<BlockState> states; // Paper - package-private
public LevelChunkSection(int yOffset) {
2021-06-12 04:24:43 +02:00
this(yOffset, (short)0, (short)0, (short)0);
@@ -37,7 +37,7 @@ public class LevelChunkSection {
2021-06-11 14:02:28 +02:00
}
2021-06-12 04:24:43 +02:00
public BlockState getBlockState(int x, int y, int z) {
- return this.states.get(x, y, z);
2021-06-11 14:02:28 +02:00
+ return this.states.get(y << 8 | z << 4 | x); // Paper - inline
}
public FluidState getFluidState(int x, int y, int z) {
diff --git a/src/main/java/net/minecraft/world/level/chunk/PalettedContainer.java b/src/main/java/net/minecraft/world/level/chunk/PalettedContainer.java
2021-06-17 23:39:36 +02:00
index 527238373b944a1e4a6e3a408534c72dd4c84035..bb8fd88aebb550edec8c679622a02a595cbc6694 100644
2021-06-11 14:02:28 +02:00
--- a/src/main/java/net/minecraft/world/level/chunk/PalettedContainer.java
+++ b/src/main/java/net/minecraft/world/level/chunk/PalettedContainer.java
2021-06-17 23:39:36 +02:00
@@ -135,7 +135,7 @@ public class PalettedContainer<T> implements PaletteResize<T> {
2021-06-11 14:02:28 +02:00
}
public T get(int x, int y, int z) {
- return this.get(getIndex(x, y, z));
+ return this.get(y << 8 | z << 4 | x); // Paper - inline
}
protected T get(int index) {
diff --git a/src/main/java/net/minecraft/world/level/chunk/ProtoChunk.java b/src/main/java/net/minecraft/world/level/chunk/ProtoChunk.java
2021-06-17 23:39:36 +02:00
index 39fe8f64528ad08594aaaa88e5c989c82e4e29d3..873fea54aecca411b6dee1ed3566f93c4fb9670f 100644
2021-06-11 14:02:28 +02:00
--- a/src/main/java/net/minecraft/world/level/chunk/ProtoChunk.java
+++ b/src/main/java/net/minecraft/world/level/chunk/ProtoChunk.java
2021-06-12 04:24:43 +02:00
@@ -104,14 +104,18 @@ public class ProtoChunk implements ChunkAccess {
2021-06-11 14:02:28 +02:00
@Override
public BlockState getBlockState(BlockPos pos) {
- int i = pos.getY();
2021-06-12 04:24:43 +02:00
- if (this.isOutsideBuildHeight(i)) {
+ // Paper start
2021-06-11 14:02:28 +02:00
+ return getType(pos.getX(), pos.getY(), pos.getZ());
+ }
+ public BlockState getType(final int x, final int y, final int z) {
2021-06-12 04:24:43 +02:00
+ if (this.isOutsideBuildHeight(y)) {
2021-06-11 14:02:28 +02:00
return Blocks.VOID_AIR.defaultBlockState();
} else {
2021-06-12 04:24:43 +02:00
- LevelChunkSection levelChunkSection = this.getSections()[this.getSectionIndex(i)];
- return LevelChunkSection.isEmpty(levelChunkSection) ? Blocks.AIR.defaultBlockState() : levelChunkSection.getBlockState(pos.getX() & 15, i & 15, pos.getZ() & 15);
+ LevelChunkSection chunksection = this.getSections()[this.getSectionIndex(y)];
2021-06-17 23:39:36 +02:00
+ return chunksection == LevelChunk.EMPTY_SECTION || chunksection.isEmpty() ? Blocks.AIR.defaultBlockState() : chunksection.getBlockState(x & 15, y & 15, z & 15);
2021-06-11 14:02:28 +02:00
}
}
+ // Paper end
@Override
public FluidState getFluidState(BlockPos pos) {