From a42e724f506dc53acedc7a1e2b28871075e32401 Mon Sep 17 00:00:00 2001 From: Andrew Steinborn Date: Wed, 11 Dec 2019 23:51:39 -0500 Subject: [PATCH 1/4] Remove unneeded indirection in NBTType ByteBufInputStream implements DataInput, ByteBufOutputStream implements DataOutput. Use them to exploit native Netty ByteBuf calls where possible. --- .../ViaVersion/api/type/types/minecraft/NBTType.java | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/common/src/main/java/us/myles/ViaVersion/api/type/types/minecraft/NBTType.java b/common/src/main/java/us/myles/ViaVersion/api/type/types/minecraft/NBTType.java index f46d994f6..4e9ac52e2 100644 --- a/common/src/main/java/us/myles/ViaVersion/api/type/types/minecraft/NBTType.java +++ b/common/src/main/java/us/myles/ViaVersion/api/type/types/minecraft/NBTType.java @@ -36,10 +36,7 @@ public class NBTType extends Type { return null; } else { buffer.readerIndex(readerIndex); - ByteBufInputStream bytebufStream = new ByteBufInputStream(buffer); - try (DataInputStream dataInputStream = new DataInputStream(bytebufStream)) { - return (CompoundTag) NBTIO.readTag((DataInput) dataInputStream); - } + return (CompoundTag) NBTIO.readTag((DataInput) new ByteBufInputStream(buffer)); } } @@ -49,11 +46,7 @@ public class NBTType extends Type { buffer.writeByte(0); } else { ByteBufOutputStream bytebufStream = new ByteBufOutputStream(buffer); - DataOutputStream dataOutputStream = new DataOutputStream(bytebufStream); - - NBTIO.writeTag((DataOutput) dataOutputStream, object); - - dataOutputStream.close(); + NBTIO.writeTag((DataOutput) bytebufStream, object); } } } From 220c45b8001a13d9b1c274abd8926b3235010384 Mon Sep 17 00:00:00 2001 From: Andrew Steinborn Date: Wed, 11 Dec 2019 23:55:44 -0500 Subject: [PATCH 2/4] Remove indirection in StringType decoding --- .../myles/ViaVersion/api/type/types/StringType.java | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/common/src/main/java/us/myles/ViaVersion/api/type/types/StringType.java b/common/src/main/java/us/myles/ViaVersion/api/type/types/StringType.java index 7a0ba098d..fc3a9f340 100644 --- a/common/src/main/java/us/myles/ViaVersion/api/type/types/StringType.java +++ b/common/src/main/java/us/myles/ViaVersion/api/type/types/StringType.java @@ -1,14 +1,15 @@ package us.myles.ViaVersion.api.type.types; -import com.google.common.base.Charsets; import com.google.common.base.Preconditions; import io.netty.buffer.ByteBuf; import us.myles.ViaVersion.api.type.Type; +import java.nio.charset.StandardCharsets; + public class StringType extends Type { // String#length() (used to limit the string in Minecraft source code) uses char[]#length private static final int maxJavaCharUtf8Length = Character.toString(Character.MAX_VALUE) - .getBytes(Charsets.UTF_8).length; + .getBytes(StandardCharsets.UTF_8).length; public StringType() { super(String.class); @@ -21,9 +22,9 @@ public class StringType extends Type { Preconditions.checkArgument(len <= Short.MAX_VALUE * maxJavaCharUtf8Length, "Cannot receive string longer than Short.MAX_VALUE * " + maxJavaCharUtf8Length + " bytes (got %s bytes)", len); - byte[] b = new byte[len]; - buffer.readBytes(b); - String string = new String(b, Charsets.UTF_8); + String string = buffer.toString(buffer.readerIndex(), len, StandardCharsets.UTF_8); + buffer.skipBytes(len); + Preconditions.checkArgument(string.length() <= Short.MAX_VALUE, "Cannot receive string longer than Short.MAX_VALUE characters (got %s bytes)", string.length()); @@ -34,7 +35,7 @@ public class StringType extends Type { public void write(ByteBuf buffer, String object) throws Exception { Preconditions.checkArgument(object.length() <= Short.MAX_VALUE, "Cannot send string longer than Short.MAX_VALUE (got %s characters)", object.length()); - byte[] b = object.getBytes(Charsets.UTF_8); + byte[] b = object.getBytes(StandardCharsets.UTF_8); Type.VAR_INT.write(buffer, b.length); buffer.writeBytes(b); } From ba08c3ad2fa0ce17dd75964f0b6a37db280bfb6f Mon Sep 17 00:00:00 2001 From: Andrew Steinborn Date: Thu, 12 Dec 2019 00:02:09 -0500 Subject: [PATCH 3/4] Remove indirection in byte array writing This saves us from the cost of (un)boxing bytes on the fly. --- .../us/myles/ViaVersion/api/type/Type.java | 2 +- .../api/type/types/ByteArrayType.java | 27 +++++++++++++++++++ .../packets/WorldPackets.java | 14 +++------- 3 files changed, 31 insertions(+), 12 deletions(-) create mode 100644 common/src/main/java/us/myles/ViaVersion/api/type/types/ByteArrayType.java diff --git a/common/src/main/java/us/myles/ViaVersion/api/type/Type.java b/common/src/main/java/us/myles/ViaVersion/api/type/Type.java index 25c6cd0ab..ca0dceb9d 100644 --- a/common/src/main/java/us/myles/ViaVersion/api/type/Type.java +++ b/common/src/main/java/us/myles/ViaVersion/api/type/Type.java @@ -14,7 +14,7 @@ import java.util.UUID; public abstract class Type implements ByteBufReader, ByteBufWriter { /* Defined Types */ public static final Type BYTE = new ByteType(); - public static final Type BYTE_ARRAY = new ArrayType<>(Type.BYTE); + public static final Type BYTE_ARRAY = new ByteArrayType(); public static final Type REMAINING_BYTES = new RemainingBytesType(); diff --git a/common/src/main/java/us/myles/ViaVersion/api/type/types/ByteArrayType.java b/common/src/main/java/us/myles/ViaVersion/api/type/types/ByteArrayType.java new file mode 100644 index 000000000..429e37062 --- /dev/null +++ b/common/src/main/java/us/myles/ViaVersion/api/type/types/ByteArrayType.java @@ -0,0 +1,27 @@ +package us.myles.ViaVersion.api.type.types; + +import com.google.common.base.Preconditions; +import io.netty.buffer.ByteBuf; +import us.myles.ViaVersion.api.type.Type; + +public class ByteArrayType extends Type { + public ByteArrayType() { + super("Byte Array", byte[].class); + } + + @Override + public byte[] read(ByteBuf buffer) throws Exception { + int len = Type.VAR_INT.read(buffer); + + Preconditions.checkArgument(buffer.isReadable(len), "Could not read %s bytes from buffer (have %s)", len, buffer.readableBytes()); + byte[] data = new byte[len]; + buffer.readBytes(data); + return data; + } + + @Override + public void write(ByteBuf buffer, byte[] object) throws Exception { + Type.VAR_INT.write(buffer, object.length); + buffer.writeBytes(object); + } +} diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14to1_13_2/packets/WorldPackets.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14to1_13_2/packets/WorldPackets.java index 3f10c4326..4bddfd9bd 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14to1_13_2/packets/WorldPackets.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14to1_13_2/packets/WorldPackets.java @@ -32,7 +32,7 @@ public class WorldPackets { private static final int VOID_AIR = MappingData.blockStateMappings.getNewBlock(8591); private static final int CAVE_AIR = MappingData.blockStateMappings.getNewBlock(8592); public static final int SERVERSIDE_VIEW_DISTANCE = 64; - private static final Byte[] FULL_LIGHT = new Byte[2048]; + private static final byte[] FULL_LIGHT = new byte[2048]; static { Arrays.fill(FULL_LIGHT, (byte) 0xff); @@ -242,14 +242,14 @@ public class WorldPackets { } continue; } - lightPacket.write(Type.BYTE_ARRAY, fromPrimitiveArray(section.getSkyLight())); + lightPacket.write(Type.BYTE_ARRAY, section.getSkyLight()); } if (chunk.isGroundUp()) lightPacket.write(Type.BYTE_ARRAY, FULL_LIGHT); // chunk above 255 for (ChunkSection section : chunk.getSections()) { if (section == null) continue; - lightPacket.write(Type.BYTE_ARRAY, fromPrimitiveArray(section.getBlockLight())); + lightPacket.write(Type.BYTE_ARRAY, section.getBlockLight()); } EntityTracker entityTracker = wrapper.user().get(EntityTracker.class); @@ -268,14 +268,6 @@ public class WorldPackets { lightPacket.send(Protocol1_14To1_13_2.class, true, true); } - - private Byte[] fromPrimitiveArray(byte[] bytes) { - Byte[] newArray = new Byte[bytes.length]; - for (int i = 0; i < bytes.length; i++) { - newArray[i] = bytes[i]; - } - return newArray; - } }); } }); From 437463f1b4082382d42e3b179199b17caba6c0df Mon Sep 17 00:00:00 2001 From: Andrew Steinborn Date: Thu, 12 Dec 2019 20:45:03 -0500 Subject: [PATCH 4/4] Revert "Remove indirection in byte array writing" This reverts commit ba08c3ad2fa0ce17dd75964f0b6a37db280bfb6f. Breaks the API that ViaRewind and ViaBackwards depend on. --- .../us/myles/ViaVersion/api/type/Type.java | 2 +- .../api/type/types/ByteArrayType.java | 27 ------------------- .../packets/WorldPackets.java | 14 +++++++--- 3 files changed, 12 insertions(+), 31 deletions(-) delete mode 100644 common/src/main/java/us/myles/ViaVersion/api/type/types/ByteArrayType.java diff --git a/common/src/main/java/us/myles/ViaVersion/api/type/Type.java b/common/src/main/java/us/myles/ViaVersion/api/type/Type.java index ca0dceb9d..25c6cd0ab 100644 --- a/common/src/main/java/us/myles/ViaVersion/api/type/Type.java +++ b/common/src/main/java/us/myles/ViaVersion/api/type/Type.java @@ -14,7 +14,7 @@ import java.util.UUID; public abstract class Type implements ByteBufReader, ByteBufWriter { /* Defined Types */ public static final Type BYTE = new ByteType(); - public static final Type BYTE_ARRAY = new ByteArrayType(); + public static final Type BYTE_ARRAY = new ArrayType<>(Type.BYTE); public static final Type REMAINING_BYTES = new RemainingBytesType(); diff --git a/common/src/main/java/us/myles/ViaVersion/api/type/types/ByteArrayType.java b/common/src/main/java/us/myles/ViaVersion/api/type/types/ByteArrayType.java deleted file mode 100644 index 429e37062..000000000 --- a/common/src/main/java/us/myles/ViaVersion/api/type/types/ByteArrayType.java +++ /dev/null @@ -1,27 +0,0 @@ -package us.myles.ViaVersion.api.type.types; - -import com.google.common.base.Preconditions; -import io.netty.buffer.ByteBuf; -import us.myles.ViaVersion.api.type.Type; - -public class ByteArrayType extends Type { - public ByteArrayType() { - super("Byte Array", byte[].class); - } - - @Override - public byte[] read(ByteBuf buffer) throws Exception { - int len = Type.VAR_INT.read(buffer); - - Preconditions.checkArgument(buffer.isReadable(len), "Could not read %s bytes from buffer (have %s)", len, buffer.readableBytes()); - byte[] data = new byte[len]; - buffer.readBytes(data); - return data; - } - - @Override - public void write(ByteBuf buffer, byte[] object) throws Exception { - Type.VAR_INT.write(buffer, object.length); - buffer.writeBytes(object); - } -} diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14to1_13_2/packets/WorldPackets.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14to1_13_2/packets/WorldPackets.java index 4bddfd9bd..3f10c4326 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14to1_13_2/packets/WorldPackets.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14to1_13_2/packets/WorldPackets.java @@ -32,7 +32,7 @@ public class WorldPackets { private static final int VOID_AIR = MappingData.blockStateMappings.getNewBlock(8591); private static final int CAVE_AIR = MappingData.blockStateMappings.getNewBlock(8592); public static final int SERVERSIDE_VIEW_DISTANCE = 64; - private static final byte[] FULL_LIGHT = new byte[2048]; + private static final Byte[] FULL_LIGHT = new Byte[2048]; static { Arrays.fill(FULL_LIGHT, (byte) 0xff); @@ -242,14 +242,14 @@ public class WorldPackets { } continue; } - lightPacket.write(Type.BYTE_ARRAY, section.getSkyLight()); + lightPacket.write(Type.BYTE_ARRAY, fromPrimitiveArray(section.getSkyLight())); } if (chunk.isGroundUp()) lightPacket.write(Type.BYTE_ARRAY, FULL_LIGHT); // chunk above 255 for (ChunkSection section : chunk.getSections()) { if (section == null) continue; - lightPacket.write(Type.BYTE_ARRAY, section.getBlockLight()); + lightPacket.write(Type.BYTE_ARRAY, fromPrimitiveArray(section.getBlockLight())); } EntityTracker entityTracker = wrapper.user().get(EntityTracker.class); @@ -268,6 +268,14 @@ public class WorldPackets { lightPacket.send(Protocol1_14To1_13_2.class, true, true); } + + private Byte[] fromPrimitiveArray(byte[] bytes) { + Byte[] newArray = new Byte[bytes.length]; + for (int i = 0; i < bytes.length; i++) { + newArray[i] = bytes[i]; + } + return newArray; + } }); } });