In this code, we create a packet batch buffer, check if the selected compressor will compress it, and discard it if it's not.
This is wasteful, because willCompress() in the zlib/libdeflate case only cares if the data is above a certain length, which can be calculated without creating a throwaway batch buffer.
If the packets won't be compressed, the prepared batch buffer can't be used directly, since we pass the packets to each session to let them be buffered and compressed separately. This means that the buffer will be discarded.
Instead of doing this, we could have a Compressor API method which asks if a certain length of data will be compressed, so we know whether to bother creating the large batch buffer in the first place or not. However, this is BC-breaking for any plugins implementing custom compressors (although this is technically internal API anyway).
Description
https://github.com/pmmp/PocketMine-MP/blob/6ec778d0afea099fffeb67396a2cb56836f6cb4a/src/network/mcpe/StandardPacketBroadcaster.php#L55
In this code, we create a packet batch buffer, check if the selected compressor will compress it, and discard it if it's not. This is wasteful, because
willCompress()
in the zlib/libdeflate case only cares if the data is above a certain length, which can be calculated without creating a throwaway batch buffer.If the packets won't be compressed, the prepared batch buffer can't be used directly, since we pass the packets to each session to let them be buffered and compressed separately. This means that the buffer will be discarded.
Instead of doing this, we could have a
Compressor
API method which asks if a certain length of data will be compressed, so we know whether to bother creating the large batch buffer in the first place or not. However, this is BC-breaking for any plugins implementing custom compressors (although this is technically internal API anyway).