mirror of
https://github.com/SpigotMC/BungeeCord.git
synced 2025-02-07 23:41:19 +01:00
Preallocate compression output buffer to remove unnecessary resizing
This commit is contained in:
parent
0aa2871b26
commit
60a3bf082f
@ -52,7 +52,13 @@ public class NativeZlib implements BungeeZlib
|
||||
|
||||
while ( !nativeCompress.finished && ( compress || in.isReadable() ) )
|
||||
{
|
||||
out.ensureWritable( OUTPUT_BUFFER_SIZE );
|
||||
if ( compress )
|
||||
{
|
||||
out.ensureWritable( OUTPUT_BUFFER_SIZE );
|
||||
} else
|
||||
{
|
||||
Preconditions.checkArgument( out.isWritable(), "Output buffer overrun" );
|
||||
}
|
||||
|
||||
int processed;
|
||||
try
|
||||
|
@ -64,7 +64,7 @@ public class NativeZlibTest
|
||||
|
||||
zlib.process( originalBuf, compressed );
|
||||
|
||||
ByteBuf uncompressed = Unpooled.directBuffer();
|
||||
ByteBuf uncompressed = Unpooled.directBuffer( dataBuf.length, dataBuf.length );
|
||||
|
||||
zlib.init( false, 0 );
|
||||
zlib.process( compressed, uncompressed );
|
||||
|
@ -7,10 +7,12 @@ import io.netty.handler.codec.MessageToMessageDecoder;
|
||||
import java.util.List;
|
||||
import net.md_5.bungee.jni.zlib.BungeeZlib;
|
||||
import net.md_5.bungee.protocol.DefinedPacket;
|
||||
import net.md_5.bungee.protocol.OverflowPacketException;
|
||||
|
||||
public class PacketDecompressor extends MessageToMessageDecoder<ByteBuf>
|
||||
{
|
||||
|
||||
private static final int MAX_DECOMPRESSED_LEN = 1 << 23;
|
||||
private final BungeeZlib zlib = CompressFactory.zlib.newInstance();
|
||||
|
||||
@Override
|
||||
@ -34,8 +36,12 @@ public class PacketDecompressor extends MessageToMessageDecoder<ByteBuf>
|
||||
out.add( in.retain() );
|
||||
} else
|
||||
{
|
||||
ByteBuf decompressed = ctx.alloc().directBuffer();
|
||||
if ( size > MAX_DECOMPRESSED_LEN )
|
||||
{
|
||||
throw new OverflowPacketException( "Packet may not be larger than " + MAX_DECOMPRESSED_LEN + " bytes" );
|
||||
}
|
||||
|
||||
ByteBuf decompressed = ctx.alloc().directBuffer( size, size );
|
||||
try
|
||||
{
|
||||
zlib.process( in, decompressed );
|
||||
|
Loading…
Reference in New Issue
Block a user