13
0
geforkt von Mirrors/Paper
Paper/Spigot-Server-Patches/0088-Optimize-isValidLocation-getType-and-getBlockData-fo.patch

261 Zeilen
12 KiB
Diff

2017-05-31 10:04:52 +02:00
From 0417cbd32cabe9da9575c5a0e01ba1f49bda5d91 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 inling
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/server/BaseBlockPosition.java b/src/main/java/net/minecraft/server/BaseBlockPosition.java
2017-05-14 20:05:01 +02:00
index d55e180d7..679d5504e 100644
--- a/src/main/java/net/minecraft/server/BaseBlockPosition.java
+++ b/src/main/java/net/minecraft/server/BaseBlockPosition.java
@@ -10,6 +10,14 @@ public class BaseBlockPosition implements Comparable<BaseBlockPosition> {
private final int a;
private final int b;
private final int c;
+ // Paper start
+ public boolean isValidLocation() {
+ return a >= -30000000 && c >= -30000000 && a < 30000000 && c < 30000000 && b >= 0 && b < 256;
+ }
+ public boolean isInvalidYLocation() {
+ return b < 0 || b >= 256;
+ }
+ // Paper end
public BaseBlockPosition(int i, int j, int k) {
this.a = i;
diff --git a/src/main/java/net/minecraft/server/BlockPosition.java b/src/main/java/net/minecraft/server/BlockPosition.java
index 67b476b22..008ed206d 100644
--- a/src/main/java/net/minecraft/server/BlockPosition.java
+++ b/src/main/java/net/minecraft/server/BlockPosition.java
2016-12-20 23:34:27 +01:00
@@ -339,6 +339,16 @@ public class BlockPosition extends BaseBlockPosition {
protected int b;
protected int c;
protected int d;
+ // Paper start
+ @Override
+ public boolean isValidLocation() {
+ return b >= -30000000 && d >= -30000000 && b < 30000000 && d < 30000000 && c >= 0 && c < 256;
+ }
+ @Override
+ public boolean isInvalidYLocation() {
+ return c < 0 || c >= 256;
+ }
+ // Paper end
public MutableBlockPosition() {
this(0, 0, 0);
diff --git a/src/main/java/net/minecraft/server/Chunk.java b/src/main/java/net/minecraft/server/Chunk.java
2017-05-14 20:05:01 +02:00
index c0d6e056d..88f5ce9e2 100644
--- a/src/main/java/net/minecraft/server/Chunk.java
+++ b/src/main/java/net/minecraft/server/Chunk.java
2017-05-14 20:05:01 +02:00
@@ -406,11 +406,27 @@ public class Chunk {
return this.a(i, j, k).c();
}
- public IBlockData getBlockData(BlockPosition blockposition) {
- return this.a(blockposition.getX(), blockposition.getY(), blockposition.getZ());
+ // Paper start - Optimize getBlockData to reduce instructions
+ public final IBlockData getBlockData(final BlockPosition pos) {
+ return getBlockData(pos.getX(), pos.getY(), pos.getZ());
+ }
+
+ public final IBlockData getBlockData(final int x, final int y, final int z) {
+ // Method body / logic copied from below
+ final int i = y >> 4;
+ if (y >= 0 && i < this.sections.length && this.sections[i] != null) {
+ // Inlined ChunkSection.getType() and DataPaletteBlock.a(int,int,int)
+ return this.sections[i].blockIds.a((y & 15) << 8 | (z & 15) << 4 | x & 15);
+ }
+ return Blocks.AIR.getBlockData();
}
public IBlockData a(final int i, final int j, final int k) {
+ return getBlockData(i, j, k);
+ }
2017-05-14 20:05:01 +02:00
+
+ public IBlockData unused(final int i, final int j, final int k) {
+ // Paper end
2017-05-14 20:05:01 +02:00
if (this.world.N() == WorldType.DEBUG_ALL_BLOCK_STATES) {
IBlockData iblockdata = null;
diff --git a/src/main/java/net/minecraft/server/ChunkSection.java b/src/main/java/net/minecraft/server/ChunkSection.java
index 3d784d0dc..afdc4a779 100644
--- a/src/main/java/net/minecraft/server/ChunkSection.java
+++ b/src/main/java/net/minecraft/server/ChunkSection.java
@@ -5,7 +5,7 @@ public class ChunkSection {
private final int yPos;
private int nonEmptyBlockCount;
private int tickingBlockCount;
- private final DataPaletteBlock blockIds;
+ final DataPaletteBlock blockIds; // Paper - package
private NibbleArray emittedLight;
private NibbleArray skyLight;
diff --git a/src/main/java/net/minecraft/server/World.java b/src/main/java/net/minecraft/server/World.java
2017-05-31 10:04:52 +02:00
index b34af0ffc..c3fbbab09 100644
--- a/src/main/java/net/minecraft/server/World.java
+++ b/src/main/java/net/minecraft/server/World.java
2017-05-14 20:05:01 +02:00
@@ -275,12 +275,12 @@ public abstract class World implements IBlockAccess {
return this.getType(blockposition1);
}
- private boolean isValidLocation(BlockPosition blockposition) {
- return !this.E(blockposition) && blockposition.getX() >= -30000000 && blockposition.getZ() >= -30000000 && blockposition.getX() < 30000000 && blockposition.getZ() < 30000000;
+ private static boolean isValidLocation(BlockPosition blockposition) { // Paper - unused but incase reflection / future uses
+ return blockposition.isValidLocation(); // Paper
}
- private boolean E(BlockPosition blockposition) {
- return blockposition.getY() < 0 || blockposition.getY() >= 256;
+ private static boolean E(BlockPosition blockposition) { // Paper - unused but incase reflection / future uses
+ return blockposition.isInvalidYLocation(); // Paper
}
public boolean isEmpty(BlockPosition blockposition) {
2017-05-14 20:05:01 +02:00
@@ -376,7 +376,7 @@ public abstract class World implements IBlockAccess {
return true;
}
// CraftBukkit end
- if (this.E(blockposition)) {
+ if (blockposition.isInvalidYLocation()) { // Paper
return false;
} else if (!this.isClientSide && this.worldData.getType() == WorldType.DEBUG_ALL_BLOCK_STATES) {
return false;
2017-05-14 20:05:01 +02:00
@@ -693,7 +693,7 @@ public abstract class World implements IBlockAccess {
// Paper start - test if meets light level, return faster
// logic copied from below
public boolean isLightLevel(BlockPosition blockposition, int level) {
- if (isValidLocation(blockposition)) {
+ if (blockposition.isValidLocation()) {
if (this.getType(blockposition).f()) {
if (this.c(blockposition.up(), false) >= level) {
return true;
2017-05-14 20:05:01 +02:00
@@ -812,7 +812,7 @@ public abstract class World implements IBlockAccess {
blockposition = new BlockPosition(blockposition.getX(), 0, blockposition.getZ());
}
- if (!this.isValidLocation(blockposition)) {
+ if (!blockposition.isValidLocation()) { // Paper
return enumskyblock.c;
} else if (!this.isLoaded(blockposition)) {
return enumskyblock.c;
2017-05-14 20:05:01 +02:00
@@ -824,7 +824,7 @@ public abstract class World implements IBlockAccess {
}
public void a(EnumSkyBlock enumskyblock, BlockPosition blockposition, int i) {
- if (this.isValidLocation(blockposition)) {
+ if (blockposition.isValidLocation()) { // Paper
if (this.isLoaded(blockposition)) {
Chunk chunk = this.getChunkAtWorldCoords(blockposition);
2017-05-14 20:05:01 +02:00
@@ -848,19 +848,19 @@ public abstract class World implements IBlockAccess {
// Paper start - reduces need to do isLoaded before getType
public IBlockData getTypeIfLoaded(BlockPosition blockposition) {
// CraftBukkit start - tree generation
+ final int x = blockposition.getX();
+ final int y = blockposition.getY();
+ final int z = blockposition.getZ();
if (captureTreeGeneration) {
- Iterator<BlockState> it = capturedBlockStates.iterator();
- while (it.hasNext()) {
- BlockState previous = it.next();
- if (previous.getX() == blockposition.getX() && previous.getY() == blockposition.getY() && previous.getZ() == blockposition.getZ()) {
- return CraftMagicNumbers.getBlock(previous.getTypeId()).fromLegacyData(previous.getRawData());
- }
+ final IBlockData previous = getCapturedBlockType(x, y, z);
+ if (previous != null) {
+ return previous;
}
}
// CraftBukkit end
- Chunk chunk = this.getChunkIfLoaded(blockposition);
+ Chunk chunk = ((ChunkProviderServer) this.chunkProvider).getChunkIfLoaded(x >> 4, z >> 4);
if (chunk != null) {
- return this.isValidLocation(blockposition) ? chunk.getBlockData(blockposition) : Blocks.AIR.getBlockData();
+ return chunk.getBlockData(x, y, z);
}
return null;
}
2017-05-14 20:05:01 +02:00
@@ -868,24 +868,33 @@ public abstract class World implements IBlockAccess {
public IBlockData getType(BlockPosition blockposition) {
// CraftBukkit start - tree generation
+ // Paper start - optimize getType lookup to reduce instructions - getBlockData already enforces valid Y, move tree out
+ final int x = blockposition.getX();
+ final int y = blockposition.getY();
+ final int z = blockposition.getZ();
if (captureTreeGeneration) {
- Iterator<BlockState> it = capturedBlockStates.iterator();
- while (it.hasNext()) {
- BlockState previous = it.next();
- if (previous.getX() == blockposition.getX() && previous.getY() == blockposition.getY() && previous.getZ() == blockposition.getZ()) {
- return CraftMagicNumbers.getBlock(previous.getTypeId()).fromLegacyData(previous.getRawData());
- }
+ final IBlockData previous = getCapturedBlockType(x, y, z);
+ if (previous != null) {
+ return previous;
}
}
// CraftBukkit end
- if (this.E(blockposition)) {
- return Blocks.AIR.getBlockData();
- } else {
- Chunk chunk = this.getChunkAtWorldCoords(blockposition);
+ return this.chunkProvider.getChunkAt(x >> 4, z >> 4).getBlockData(x, y, z);
+ // Paper end
+ }
- return chunk.getBlockData(blockposition);
+ // Paper start
+ private IBlockData getCapturedBlockType(int x, int y, int z) {
+ Iterator<BlockState> it = capturedBlockStates.iterator();
+ while (it.hasNext()) {
+ BlockState previous = it.next();
+ if (previous.getX() == x && previous.getY() == y && previous.getZ() == z) {
+ return CraftMagicNumbers.getBlock(previous.getTypeId()).fromLegacyData(previous.getRawData());
+ }
}
+ return null;
}
+ // Paper end
2017-05-14 20:05:01 +02:00
public boolean D() {
return this.L < 4;
2017-05-31 10:04:52 +02:00
@@ -2024,7 +2033,7 @@ public abstract class World implements IBlockAccess {
public Map<BlockPosition, TileEntity> capturedTileEntities = Maps.newHashMap();
@Nullable
public TileEntity getTileEntity(BlockPosition blockposition) {
- if (this.E(blockposition)) {
+ if (blockposition.isInvalidYLocation()) { // Paper
return null;
} else {
// CraftBukkit start
2017-05-31 10:04:52 +02:00
@@ -2065,7 +2074,7 @@ public abstract class World implements IBlockAccess {
}
public void setTileEntity(BlockPosition blockposition, @Nullable TileEntity tileentity) {
- if (!this.E(blockposition)) {
2016-11-17 03:23:38 +01:00
+ if (!blockposition.isInvalidYLocation()) {
if (tileentity != null && !tileentity.y()) {
// CraftBukkit start
if (captureBlockStates) {
2017-05-31 10:04:52 +02:00
@@ -2128,7 +2137,7 @@ public abstract class World implements IBlockAccess {
}
public boolean d(BlockPosition blockposition, boolean flag) {
- if (this.E(blockposition)) {
+ if (blockposition.isInvalidYLocation()) { // Paper
return false;
} else {
Chunk chunk = this.chunkProvider.getLoadedChunkAt(blockposition.getX() >> 4, blockposition.getZ() >> 4);
--
2017-05-14 20:05:01 +02:00
2.13.0