From f0eab716443601860b08f0fc53805209e28ca640 Mon Sep 17 00:00:00 2001 From: Nassim Jahnke Date: Mon, 4 Nov 2024 21:18:27 +0100 Subject: [PATCH] Pre-size most allocated byte buffers Also avoids the default allocation size of 256 bytes for bufs that are much smaller than that --- .../bukkit/handlers/BukkitDecodeHandler.java | 5 ++-- .../bukkit/handlers/BukkitEncodeHandler.java | 3 +- .../connection/UserConnectionImpl.java | 8 +++-- .../protocol/packet/PacketWrapperImpl.java | 16 +++++++--- .../viaversion/util/ByteBufUtil.java | 29 +++++++++++++++++++ .../viaversion/util/SerializerVersion.java | 3 +- .../handlers/VelocityDecodeHandler.java | 3 +- .../handlers/VelocityEncodeHandler.java | 3 +- 8 files changed, 57 insertions(+), 13 deletions(-) create mode 100644 common/src/main/java/com/viaversion/viaversion/util/ByteBufUtil.java diff --git a/bukkit/src/main/java/com/viaversion/viaversion/bukkit/handlers/BukkitDecodeHandler.java b/bukkit/src/main/java/com/viaversion/viaversion/bukkit/handlers/BukkitDecodeHandler.java index 2d73afc73..2a0f94b06 100644 --- a/bukkit/src/main/java/com/viaversion/viaversion/bukkit/handlers/BukkitDecodeHandler.java +++ b/bukkit/src/main/java/com/viaversion/viaversion/bukkit/handlers/BukkitDecodeHandler.java @@ -22,6 +22,7 @@ import com.viaversion.viaversion.bukkit.util.NMSUtil; import com.viaversion.viaversion.exception.CancelCodecException; import com.viaversion.viaversion.exception.CancelDecoderException; import com.viaversion.viaversion.exception.InformativeException; +import com.viaversion.viaversion.util.ByteBufUtil; import com.viaversion.viaversion.util.PipelineUtil; import io.netty.buffer.ByteBuf; import io.netty.channel.ChannelHandler; @@ -39,7 +40,7 @@ public final class BukkitDecodeHandler extends MessageToMessageDecoder } @Override - protected void decode(final ChannelHandlerContext ctx, final ByteBuf bytebuf, final List out) throws Exception { + protected void decode(final ChannelHandlerContext ctx, final ByteBuf bytebuf, final List out) { if (!connection.checkServerboundPacket()) { throw CancelDecoderException.generate(null); } @@ -48,7 +49,7 @@ public final class BukkitDecodeHandler extends MessageToMessageDecoder return; } - final ByteBuf transformedBuf = ctx.alloc().buffer().writeBytes(bytebuf); + final ByteBuf transformedBuf = ByteBufUtil.copy(ctx.alloc(), bytebuf); try { connection.transformIncoming(transformedBuf, CancelDecoderException::generate); out.add(transformedBuf.retain()); diff --git a/bukkit/src/main/java/com/viaversion/viaversion/bukkit/handlers/BukkitEncodeHandler.java b/bukkit/src/main/java/com/viaversion/viaversion/bukkit/handlers/BukkitEncodeHandler.java index 1147ad407..4784cb2c4 100644 --- a/bukkit/src/main/java/com/viaversion/viaversion/bukkit/handlers/BukkitEncodeHandler.java +++ b/bukkit/src/main/java/com/viaversion/viaversion/bukkit/handlers/BukkitEncodeHandler.java @@ -22,6 +22,7 @@ import com.viaversion.viaversion.bukkit.util.NMSUtil; import com.viaversion.viaversion.exception.CancelCodecException; import com.viaversion.viaversion.exception.CancelEncoderException; import com.viaversion.viaversion.exception.InformativeException; +import com.viaversion.viaversion.util.ByteBufUtil; import com.viaversion.viaversion.util.PipelineUtil; import io.netty.buffer.ByteBuf; import io.netty.channel.ChannelHandler; @@ -52,7 +53,7 @@ public final class BukkitEncodeHandler extends MessageToMessageEncoder return; } - final ByteBuf transformedBuf = ctx.alloc().buffer().writeBytes(bytebuf); + final ByteBuf transformedBuf = ByteBufUtil.copy(ctx.alloc(), bytebuf); try { final boolean needsCompression = !handledCompression && handleCompressionOrder(ctx, transformedBuf); connection.transformClientbound(transformedBuf, CancelEncoderException::generate); diff --git a/common/src/main/java/com/viaversion/viaversion/connection/UserConnectionImpl.java b/common/src/main/java/com/viaversion/viaversion/connection/UserConnectionImpl.java index 4a7f82dc4..ac7a9aaca 100644 --- a/common/src/main/java/com/viaversion/viaversion/connection/UserConnectionImpl.java +++ b/common/src/main/java/com/viaversion/viaversion/connection/UserConnectionImpl.java @@ -55,6 +55,7 @@ import java.util.function.Function; import org.checkerframework.checker.nullness.qual.Nullable; public class UserConnectionImpl implements UserConnection { + private static final int PASSTHROUGH_DATA_BYTES = Long.BYTES * 2 + 2; private static final AtomicLong IDS = new AtomicLong(); private final long id = IDS.incrementAndGet(); private final Map, StorableObject> storedObjects = new ConcurrentHashMap<>(); @@ -244,7 +245,8 @@ public class UserConnectionImpl implements UserConnection { } private void sendRawPacketToServerServerSide(final ByteBuf packet, final boolean currentThread) { - final ByteBuf buf = packet.alloc().buffer(); + final int initialCapacity = active ? packet.readableBytes() + PASSTHROUGH_DATA_BYTES : packet.readableBytes(); + final ByteBuf buf = packet.alloc().buffer(initialCapacity); try { // We'll use passing through because there are some encoder wrappers ChannelHandlerContext context = PipelineUtil @@ -341,7 +343,7 @@ public class UserConnectionImpl implements UserConnection { return; } - PacketWrapper wrapper = new PacketWrapperImpl(id, buf, this); + PacketWrapperImpl wrapper = new PacketWrapperImpl(id, buf, this); State state = protocolInfo.getState(direction); try { protocolInfo.getPipeline().transform(direction, state, wrapper); @@ -349,7 +351,7 @@ public class UserConnectionImpl implements UserConnection { throw cancelSupplier.apply(ex); } - ByteBuf transformed = buf.alloc().buffer(); + ByteBuf transformed = wrapper.allocateOutputBuffer(); try { wrapper.writeToBuffer(transformed); buf.clear().writeBytes(transformed); diff --git a/common/src/main/java/com/viaversion/viaversion/protocol/packet/PacketWrapperImpl.java b/common/src/main/java/com/viaversion/viaversion/protocol/packet/PacketWrapperImpl.java index 9f05ef28c..971640a41 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocol/packet/PacketWrapperImpl.java +++ b/common/src/main/java/com/viaversion/viaversion/protocol/packet/PacketWrapperImpl.java @@ -320,7 +320,7 @@ public class PacketWrapperImpl implements PacketWrapper { final ProtocolInfo protocolInfo = user().getProtocolInfo(); final List protocols = protocolInfo.getPipeline().pipes(protocolClass, skipCurrentPipeline, direction); apply(direction, protocolInfo.getState(direction), protocols); - final ByteBuf output = inputBuffer == null ? user().getChannel().alloc().buffer() : inputBuffer.alloc().buffer(); + final ByteBuf output = allocateOutputBuffer(); try { writeToBuffer(output); return output.retain(); @@ -354,7 +354,7 @@ public class PacketWrapperImpl implements PacketWrapper { return cancelledFuture(); } - ByteBuf output = inputBuffer == null ? user().getChannel().alloc().buffer() : inputBuffer.alloc().buffer(); + ByteBuf output = allocateOutputBuffer(); try { writeToBuffer(output); return user().sendRawPacketFuture(output.retain()); @@ -373,7 +373,7 @@ public class PacketWrapperImpl implements PacketWrapper { return; } - ByteBuf output = inputBuffer == null ? user().getChannel().alloc().buffer() : inputBuffer.alloc().buffer(); + ByteBuf output = allocateOutputBuffer(); try { writeToBuffer(output); if (currentThread) { @@ -386,6 +386,14 @@ public class PacketWrapperImpl implements PacketWrapper { } } + public ByteBuf allocateOutputBuffer() { + if (inputBuffer == null) { + return user().getChannel().alloc().buffer(); + } + // May have already been partially or fully read + return inputBuffer.alloc().buffer(Math.max(inputBuffer.readableBytes(), 256)); + } + private ChannelFuture cancelledFuture() { return user().getChannel().newFailedFuture(new RuntimeException("Tried to send cancelled packet")); } @@ -454,7 +462,7 @@ public class PacketWrapperImpl implements PacketWrapper { return; } - ByteBuf output = inputBuffer == null ? user().getChannel().alloc().buffer() : inputBuffer.alloc().buffer(); + ByteBuf output = allocateOutputBuffer(); try { writeToBuffer(output); if (currentThread) { diff --git a/common/src/main/java/com/viaversion/viaversion/util/ByteBufUtil.java b/common/src/main/java/com/viaversion/viaversion/util/ByteBufUtil.java new file mode 100644 index 000000000..beaa41a30 --- /dev/null +++ b/common/src/main/java/com/viaversion/viaversion/util/ByteBufUtil.java @@ -0,0 +1,29 @@ +/* + * This file is part of ViaVersion - https://github.com/ViaVersion/ViaVersion + * Copyright (C) 2016-2024 ViaVersion and contributors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package com.viaversion.viaversion.util; + +import io.netty.buffer.ByteBuf; +import io.netty.buffer.ByteBufAllocator; + +public final class ByteBufUtil { + + public static ByteBuf copy(final ByteBufAllocator allocator, final ByteBuf from) { + final int bytes = from.readableBytes(); + return allocator.buffer(bytes).writeBytes(from, bytes); + } +} diff --git a/common/src/main/java/com/viaversion/viaversion/util/SerializerVersion.java b/common/src/main/java/com/viaversion/viaversion/util/SerializerVersion.java index 0ab0f7c84..6b19d2c27 100644 --- a/common/src/main/java/com/viaversion/viaversion/util/SerializerVersion.java +++ b/common/src/main/java/com/viaversion/viaversion/util/SerializerVersion.java @@ -48,7 +48,8 @@ public enum SerializerVersion { V1_18(TextComponentSerializer.V1_18, SNbtSerializer.V1_14), V1_19_4(TextComponentSerializer.V1_19_4, SNbtSerializer.V1_14), V1_20_3(TextComponentCodec.V1_20_3, SNbtSerializer.V1_14), - V1_20_5(TextComponentCodec.V1_20_5, SNbtSerializer.V1_14); + V1_20_5(TextComponentCodec.V1_20_5, SNbtSerializer.V1_14), + V1_21_4(TextComponentCodec.V1_21_4, SNbtSerializer.V1_14); final TextComponentSerializer jsonSerializer; final SNbtSerializer snbtSerializer; diff --git a/velocity/src/main/java/com/viaversion/viaversion/velocity/handlers/VelocityDecodeHandler.java b/velocity/src/main/java/com/viaversion/viaversion/velocity/handlers/VelocityDecodeHandler.java index e92648efe..0df6ff9c6 100644 --- a/velocity/src/main/java/com/viaversion/viaversion/velocity/handlers/VelocityDecodeHandler.java +++ b/velocity/src/main/java/com/viaversion/viaversion/velocity/handlers/VelocityDecodeHandler.java @@ -20,6 +20,7 @@ package com.viaversion.viaversion.velocity.handlers; import com.viaversion.viaversion.api.connection.UserConnection; import com.viaversion.viaversion.exception.CancelCodecException; import com.viaversion.viaversion.exception.CancelDecoderException; +import com.viaversion.viaversion.util.ByteBufUtil; import io.netty.buffer.ByteBuf; import io.netty.channel.ChannelHandler; import io.netty.channel.ChannelHandlerContext; @@ -43,7 +44,7 @@ public class VelocityDecodeHandler extends MessageToMessageDecoder { return; } - ByteBuf transformedBuf = ctx.alloc().buffer().writeBytes(bytebuf); + ByteBuf transformedBuf = ByteBufUtil.copy(ctx.alloc(), bytebuf); try { info.transformIncoming(transformedBuf, CancelDecoderException::generate); out.add(transformedBuf.retain()); diff --git a/velocity/src/main/java/com/viaversion/viaversion/velocity/handlers/VelocityEncodeHandler.java b/velocity/src/main/java/com/viaversion/viaversion/velocity/handlers/VelocityEncodeHandler.java index ef1911777..71f0b500c 100644 --- a/velocity/src/main/java/com/viaversion/viaversion/velocity/handlers/VelocityEncodeHandler.java +++ b/velocity/src/main/java/com/viaversion/viaversion/velocity/handlers/VelocityEncodeHandler.java @@ -20,6 +20,7 @@ package com.viaversion.viaversion.velocity.handlers; import com.viaversion.viaversion.api.connection.UserConnection; import com.viaversion.viaversion.exception.CancelCodecException; import com.viaversion.viaversion.exception.CancelEncoderException; +import com.viaversion.viaversion.util.ByteBufUtil; import io.netty.buffer.ByteBuf; import io.netty.channel.ChannelHandler; import io.netty.channel.ChannelHandlerContext; @@ -42,7 +43,7 @@ public class VelocityEncodeHandler extends MessageToMessageEncoder { return; } - ByteBuf transformedBuf = ctx.alloc().buffer().writeBytes(bytebuf); + ByteBuf transformedBuf = ByteBufUtil.copy(ctx.alloc(), bytebuf); try { info.transformOutgoing(transformedBuf, CancelEncoderException::generate); out.add(transformedBuf.retain());