mirror of
https://github.com/ViaVersion/ViaVersion.git
synced 2024-11-21 17:45:36 +01:00
Pre-size most allocated byte buffers
Also avoids the default allocation size of 256 bytes for bufs that are much smaller than that
This commit is contained in:
parent
e9985adafc
commit
f0eab71644
@ -22,6 +22,7 @@ import com.viaversion.viaversion.bukkit.util.NMSUtil;
|
|||||||
import com.viaversion.viaversion.exception.CancelCodecException;
|
import com.viaversion.viaversion.exception.CancelCodecException;
|
||||||
import com.viaversion.viaversion.exception.CancelDecoderException;
|
import com.viaversion.viaversion.exception.CancelDecoderException;
|
||||||
import com.viaversion.viaversion.exception.InformativeException;
|
import com.viaversion.viaversion.exception.InformativeException;
|
||||||
|
import com.viaversion.viaversion.util.ByteBufUtil;
|
||||||
import com.viaversion.viaversion.util.PipelineUtil;
|
import com.viaversion.viaversion.util.PipelineUtil;
|
||||||
import io.netty.buffer.ByteBuf;
|
import io.netty.buffer.ByteBuf;
|
||||||
import io.netty.channel.ChannelHandler;
|
import io.netty.channel.ChannelHandler;
|
||||||
@ -39,7 +40,7 @@ public final class BukkitDecodeHandler extends MessageToMessageDecoder<ByteBuf>
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void decode(final ChannelHandlerContext ctx, final ByteBuf bytebuf, final List<Object> out) throws Exception {
|
protected void decode(final ChannelHandlerContext ctx, final ByteBuf bytebuf, final List<Object> out) {
|
||||||
if (!connection.checkServerboundPacket()) {
|
if (!connection.checkServerboundPacket()) {
|
||||||
throw CancelDecoderException.generate(null);
|
throw CancelDecoderException.generate(null);
|
||||||
}
|
}
|
||||||
@ -48,7 +49,7 @@ public final class BukkitDecodeHandler extends MessageToMessageDecoder<ByteBuf>
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
final ByteBuf transformedBuf = ctx.alloc().buffer().writeBytes(bytebuf);
|
final ByteBuf transformedBuf = ByteBufUtil.copy(ctx.alloc(), bytebuf);
|
||||||
try {
|
try {
|
||||||
connection.transformIncoming(transformedBuf, CancelDecoderException::generate);
|
connection.transformIncoming(transformedBuf, CancelDecoderException::generate);
|
||||||
out.add(transformedBuf.retain());
|
out.add(transformedBuf.retain());
|
||||||
|
@ -22,6 +22,7 @@ import com.viaversion.viaversion.bukkit.util.NMSUtil;
|
|||||||
import com.viaversion.viaversion.exception.CancelCodecException;
|
import com.viaversion.viaversion.exception.CancelCodecException;
|
||||||
import com.viaversion.viaversion.exception.CancelEncoderException;
|
import com.viaversion.viaversion.exception.CancelEncoderException;
|
||||||
import com.viaversion.viaversion.exception.InformativeException;
|
import com.viaversion.viaversion.exception.InformativeException;
|
||||||
|
import com.viaversion.viaversion.util.ByteBufUtil;
|
||||||
import com.viaversion.viaversion.util.PipelineUtil;
|
import com.viaversion.viaversion.util.PipelineUtil;
|
||||||
import io.netty.buffer.ByteBuf;
|
import io.netty.buffer.ByteBuf;
|
||||||
import io.netty.channel.ChannelHandler;
|
import io.netty.channel.ChannelHandler;
|
||||||
@ -52,7 +53,7 @@ public final class BukkitEncodeHandler extends MessageToMessageEncoder<ByteBuf>
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
final ByteBuf transformedBuf = ctx.alloc().buffer().writeBytes(bytebuf);
|
final ByteBuf transformedBuf = ByteBufUtil.copy(ctx.alloc(), bytebuf);
|
||||||
try {
|
try {
|
||||||
final boolean needsCompression = !handledCompression && handleCompressionOrder(ctx, transformedBuf);
|
final boolean needsCompression = !handledCompression && handleCompressionOrder(ctx, transformedBuf);
|
||||||
connection.transformClientbound(transformedBuf, CancelEncoderException::generate);
|
connection.transformClientbound(transformedBuf, CancelEncoderException::generate);
|
||||||
|
@ -55,6 +55,7 @@ import java.util.function.Function;
|
|||||||
import org.checkerframework.checker.nullness.qual.Nullable;
|
import org.checkerframework.checker.nullness.qual.Nullable;
|
||||||
|
|
||||||
public class UserConnectionImpl implements UserConnection {
|
public class UserConnectionImpl implements UserConnection {
|
||||||
|
private static final int PASSTHROUGH_DATA_BYTES = Long.BYTES * 2 + 2;
|
||||||
private static final AtomicLong IDS = new AtomicLong();
|
private static final AtomicLong IDS = new AtomicLong();
|
||||||
private final long id = IDS.incrementAndGet();
|
private final long id = IDS.incrementAndGet();
|
||||||
private final Map<Class<?>, StorableObject> storedObjects = new ConcurrentHashMap<>();
|
private final Map<Class<?>, StorableObject> storedObjects = new ConcurrentHashMap<>();
|
||||||
@ -244,7 +245,8 @@ public class UserConnectionImpl implements UserConnection {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void sendRawPacketToServerServerSide(final ByteBuf packet, final boolean currentThread) {
|
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 {
|
try {
|
||||||
// We'll use passing through because there are some encoder wrappers
|
// We'll use passing through because there are some encoder wrappers
|
||||||
ChannelHandlerContext context = PipelineUtil
|
ChannelHandlerContext context = PipelineUtil
|
||||||
@ -341,7 +343,7 @@ public class UserConnectionImpl implements UserConnection {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
PacketWrapper wrapper = new PacketWrapperImpl(id, buf, this);
|
PacketWrapperImpl wrapper = new PacketWrapperImpl(id, buf, this);
|
||||||
State state = protocolInfo.getState(direction);
|
State state = protocolInfo.getState(direction);
|
||||||
try {
|
try {
|
||||||
protocolInfo.getPipeline().transform(direction, state, wrapper);
|
protocolInfo.getPipeline().transform(direction, state, wrapper);
|
||||||
@ -349,7 +351,7 @@ public class UserConnectionImpl implements UserConnection {
|
|||||||
throw cancelSupplier.apply(ex);
|
throw cancelSupplier.apply(ex);
|
||||||
}
|
}
|
||||||
|
|
||||||
ByteBuf transformed = buf.alloc().buffer();
|
ByteBuf transformed = wrapper.allocateOutputBuffer();
|
||||||
try {
|
try {
|
||||||
wrapper.writeToBuffer(transformed);
|
wrapper.writeToBuffer(transformed);
|
||||||
buf.clear().writeBytes(transformed);
|
buf.clear().writeBytes(transformed);
|
||||||
|
@ -320,7 +320,7 @@ public class PacketWrapperImpl implements PacketWrapper {
|
|||||||
final ProtocolInfo protocolInfo = user().getProtocolInfo();
|
final ProtocolInfo protocolInfo = user().getProtocolInfo();
|
||||||
final List<Protocol> protocols = protocolInfo.getPipeline().pipes(protocolClass, skipCurrentPipeline, direction);
|
final List<Protocol> protocols = protocolInfo.getPipeline().pipes(protocolClass, skipCurrentPipeline, direction);
|
||||||
apply(direction, protocolInfo.getState(direction), protocols);
|
apply(direction, protocolInfo.getState(direction), protocols);
|
||||||
final ByteBuf output = inputBuffer == null ? user().getChannel().alloc().buffer() : inputBuffer.alloc().buffer();
|
final ByteBuf output = allocateOutputBuffer();
|
||||||
try {
|
try {
|
||||||
writeToBuffer(output);
|
writeToBuffer(output);
|
||||||
return output.retain();
|
return output.retain();
|
||||||
@ -354,7 +354,7 @@ public class PacketWrapperImpl implements PacketWrapper {
|
|||||||
return cancelledFuture();
|
return cancelledFuture();
|
||||||
}
|
}
|
||||||
|
|
||||||
ByteBuf output = inputBuffer == null ? user().getChannel().alloc().buffer() : inputBuffer.alloc().buffer();
|
ByteBuf output = allocateOutputBuffer();
|
||||||
try {
|
try {
|
||||||
writeToBuffer(output);
|
writeToBuffer(output);
|
||||||
return user().sendRawPacketFuture(output.retain());
|
return user().sendRawPacketFuture(output.retain());
|
||||||
@ -373,7 +373,7 @@ public class PacketWrapperImpl implements PacketWrapper {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
ByteBuf output = inputBuffer == null ? user().getChannel().alloc().buffer() : inputBuffer.alloc().buffer();
|
ByteBuf output = allocateOutputBuffer();
|
||||||
try {
|
try {
|
||||||
writeToBuffer(output);
|
writeToBuffer(output);
|
||||||
if (currentThread) {
|
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() {
|
private ChannelFuture cancelledFuture() {
|
||||||
return user().getChannel().newFailedFuture(new RuntimeException("Tried to send cancelled packet"));
|
return user().getChannel().newFailedFuture(new RuntimeException("Tried to send cancelled packet"));
|
||||||
}
|
}
|
||||||
@ -454,7 +462,7 @@ public class PacketWrapperImpl implements PacketWrapper {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
ByteBuf output = inputBuffer == null ? user().getChannel().alloc().buffer() : inputBuffer.alloc().buffer();
|
ByteBuf output = allocateOutputBuffer();
|
||||||
try {
|
try {
|
||||||
writeToBuffer(output);
|
writeToBuffer(output);
|
||||||
if (currentThread) {
|
if (currentThread) {
|
||||||
|
@ -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 <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
@ -48,7 +48,8 @@ public enum SerializerVersion {
|
|||||||
V1_18(TextComponentSerializer.V1_18, SNbtSerializer.V1_14),
|
V1_18(TextComponentSerializer.V1_18, SNbtSerializer.V1_14),
|
||||||
V1_19_4(TextComponentSerializer.V1_19_4, 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_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 TextComponentSerializer jsonSerializer;
|
||||||
final SNbtSerializer<? extends Tag> snbtSerializer;
|
final SNbtSerializer<? extends Tag> snbtSerializer;
|
||||||
|
@ -20,6 +20,7 @@ package com.viaversion.viaversion.velocity.handlers;
|
|||||||
import com.viaversion.viaversion.api.connection.UserConnection;
|
import com.viaversion.viaversion.api.connection.UserConnection;
|
||||||
import com.viaversion.viaversion.exception.CancelCodecException;
|
import com.viaversion.viaversion.exception.CancelCodecException;
|
||||||
import com.viaversion.viaversion.exception.CancelDecoderException;
|
import com.viaversion.viaversion.exception.CancelDecoderException;
|
||||||
|
import com.viaversion.viaversion.util.ByteBufUtil;
|
||||||
import io.netty.buffer.ByteBuf;
|
import io.netty.buffer.ByteBuf;
|
||||||
import io.netty.channel.ChannelHandler;
|
import io.netty.channel.ChannelHandler;
|
||||||
import io.netty.channel.ChannelHandlerContext;
|
import io.netty.channel.ChannelHandlerContext;
|
||||||
@ -43,7 +44,7 @@ public class VelocityDecodeHandler extends MessageToMessageDecoder<ByteBuf> {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
ByteBuf transformedBuf = ctx.alloc().buffer().writeBytes(bytebuf);
|
ByteBuf transformedBuf = ByteBufUtil.copy(ctx.alloc(), bytebuf);
|
||||||
try {
|
try {
|
||||||
info.transformIncoming(transformedBuf, CancelDecoderException::generate);
|
info.transformIncoming(transformedBuf, CancelDecoderException::generate);
|
||||||
out.add(transformedBuf.retain());
|
out.add(transformedBuf.retain());
|
||||||
|
@ -20,6 +20,7 @@ package com.viaversion.viaversion.velocity.handlers;
|
|||||||
import com.viaversion.viaversion.api.connection.UserConnection;
|
import com.viaversion.viaversion.api.connection.UserConnection;
|
||||||
import com.viaversion.viaversion.exception.CancelCodecException;
|
import com.viaversion.viaversion.exception.CancelCodecException;
|
||||||
import com.viaversion.viaversion.exception.CancelEncoderException;
|
import com.viaversion.viaversion.exception.CancelEncoderException;
|
||||||
|
import com.viaversion.viaversion.util.ByteBufUtil;
|
||||||
import io.netty.buffer.ByteBuf;
|
import io.netty.buffer.ByteBuf;
|
||||||
import io.netty.channel.ChannelHandler;
|
import io.netty.channel.ChannelHandler;
|
||||||
import io.netty.channel.ChannelHandlerContext;
|
import io.netty.channel.ChannelHandlerContext;
|
||||||
@ -42,7 +43,7 @@ public class VelocityEncodeHandler extends MessageToMessageEncoder<ByteBuf> {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
ByteBuf transformedBuf = ctx.alloc().buffer().writeBytes(bytebuf);
|
ByteBuf transformedBuf = ByteBufUtil.copy(ctx.alloc(), bytebuf);
|
||||||
try {
|
try {
|
||||||
info.transformOutgoing(transformedBuf, CancelEncoderException::generate);
|
info.transformOutgoing(transformedBuf, CancelEncoderException::generate);
|
||||||
out.add(transformedBuf.retain());
|
out.add(transformedBuf.retain());
|
||||||
|
Loading…
Reference in New Issue
Block a user