pmmp / PocketMine-MP

A server software for Minecraft: Bedrock Edition in PHP
https://pmmp.io
GNU Lesser General Public License v3.0
3.25k stars 1.53k forks source link

Wasteful decision making in StandardPacketBroadcaster #5589

Closed dktapps closed 1 year ago

dktapps commented 1 year ago

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).

dktapps commented 1 year ago

Fixed by aaec21f544ae09c248396a4529c0d82b489a9a0a.