mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-07-10 11:57:47 +00:00
Avoid creating batch buffers just to determine whether a batch should be globally compressed
Instead, sum together the lengths of encoded packet buffers and use that to decide whether to build the buffer or not.
This commit is contained in:
parent
6a64486f55
commit
8234360c8d
@ -891,6 +891,9 @@ class Server{
|
|||||||
if($this->configGroup->getPropertyInt("network.batch-threshold", 256) >= 0){
|
if($this->configGroup->getPropertyInt("network.batch-threshold", 256) >= 0){
|
||||||
$netCompressionThreshold = $this->configGroup->getPropertyInt("network.batch-threshold", 256);
|
$netCompressionThreshold = $this->configGroup->getPropertyInt("network.batch-threshold", 256);
|
||||||
}
|
}
|
||||||
|
if($netCompressionThreshold < 0){
|
||||||
|
$netCompressionThreshold = null;
|
||||||
|
}
|
||||||
|
|
||||||
$netCompressionLevel = $this->configGroup->getPropertyInt("network.compression-level", 6);
|
$netCompressionLevel = $this->configGroup->getPropertyInt("network.compression-level", 6);
|
||||||
if($netCompressionLevel < 1 || $netCompressionLevel > 9){
|
if($netCompressionLevel < 1 || $netCompressionLevel > 9){
|
||||||
|
@ -23,21 +23,19 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace pocketmine\network\mcpe;
|
namespace pocketmine\network\mcpe;
|
||||||
|
|
||||||
use pocketmine\network\mcpe\protocol\ClientboundPacket;
|
use pocketmine\network\mcpe\compression\ThresholdCompressor;
|
||||||
use pocketmine\network\mcpe\protocol\serializer\PacketBatch;
|
use pocketmine\network\mcpe\protocol\serializer\PacketBatch;
|
||||||
use pocketmine\network\mcpe\protocol\serializer\PacketSerializer;
|
use pocketmine\network\mcpe\protocol\serializer\PacketSerializer;
|
||||||
use pocketmine\Server;
|
use pocketmine\Server;
|
||||||
use pocketmine\utils\BinaryStream;
|
use pocketmine\utils\BinaryStream;
|
||||||
use function array_map;
|
|
||||||
use function spl_object_id;
|
use function spl_object_id;
|
||||||
|
use function strlen;
|
||||||
|
|
||||||
final class StandardPacketBroadcaster implements PacketBroadcaster{
|
final class StandardPacketBroadcaster implements PacketBroadcaster{
|
||||||
public function __construct(private Server $server){}
|
public function __construct(private Server $server){}
|
||||||
|
|
||||||
public function broadcastPackets(array $recipients, array $packets) : void{
|
public function broadcastPackets(array $recipients, array $packets) : void{
|
||||||
$batchBuffers = [];
|
$packetBufferTotalLengths = [];
|
||||||
|
|
||||||
/** @var string[][] $packetBuffers */
|
|
||||||
$packetBuffers = [];
|
$packetBuffers = [];
|
||||||
$compressors = [];
|
$compressors = [];
|
||||||
/** @var NetworkSession[][][] $targetMap */
|
/** @var NetworkSession[][][] $targetMap */
|
||||||
@ -45,13 +43,14 @@ final class StandardPacketBroadcaster implements PacketBroadcaster{
|
|||||||
foreach($recipients as $recipient){
|
foreach($recipients as $recipient){
|
||||||
$serializerContext = $recipient->getPacketSerializerContext();
|
$serializerContext = $recipient->getPacketSerializerContext();
|
||||||
$bufferId = spl_object_id($serializerContext);
|
$bufferId = spl_object_id($serializerContext);
|
||||||
if(!isset($batchBuffers[$bufferId])){
|
if(!isset($packetBuffers[$bufferId])){
|
||||||
$packetBuffers[$bufferId] = array_map(function(ClientboundPacket $packet) use ($serializerContext) : string{
|
$packetBufferTotalLengths[$bufferId] = 0;
|
||||||
return NetworkSession::encodePacketTimed(PacketSerializer::encoder($serializerContext), $packet);
|
$packetBuffers[$bufferId] = [];
|
||||||
}, $packets);
|
foreach($packets as $packet){
|
||||||
$stream = new BinaryStream();
|
$buffer = NetworkSession::encodePacketTimed(PacketSerializer::encoder($serializerContext), $packet);
|
||||||
PacketBatch::encodeRaw($stream, $packetBuffers[$bufferId]);
|
$packetBufferTotalLengths[$bufferId] += strlen($buffer);
|
||||||
$batchBuffers[$bufferId] = $stream->getBuffer();
|
$packetBuffers[$bufferId][] = $buffer;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//TODO: different compressors might be compatible, it might not be necessary to split them up by object
|
//TODO: different compressors might be compatible, it might not be necessary to split them up by object
|
||||||
@ -62,10 +61,29 @@ final class StandardPacketBroadcaster implements PacketBroadcaster{
|
|||||||
}
|
}
|
||||||
|
|
||||||
foreach($targetMap as $bufferId => $compressorMap){
|
foreach($targetMap as $bufferId => $compressorMap){
|
||||||
$batchBuffer = $batchBuffers[$bufferId];
|
|
||||||
foreach($compressorMap as $compressorId => $compressorTargets){
|
foreach($compressorMap as $compressorId => $compressorTargets){
|
||||||
$compressor = $compressors[$compressorId];
|
$compressor = $compressors[$compressorId];
|
||||||
if(!$compressor->willCompress($batchBuffer)){
|
|
||||||
|
$batchBuffer = null;
|
||||||
|
if($compressor instanceof ThresholdCompressor){
|
||||||
|
$threshold = $compressor->getCompressionThreshold();
|
||||||
|
if($threshold !== null && $packetBufferTotalLengths[$bufferId] >= $threshold){
|
||||||
|
//do not prepare shared batch unless we're sure it will be compressed
|
||||||
|
$stream = new BinaryStream();
|
||||||
|
PacketBatch::encodeRaw($stream, $packetBuffers[$bufferId]);
|
||||||
|
$batchBuffer = $stream->getBuffer();
|
||||||
|
}
|
||||||
|
}else{
|
||||||
|
//this is a legacy compressor, so we have to encode the batch and check if it will compress
|
||||||
|
$stream = new BinaryStream();
|
||||||
|
PacketBatch::encodeRaw($stream, $packetBuffers[$bufferId]);
|
||||||
|
$tempBatchBuffer = $stream->getBuffer();
|
||||||
|
if($compressor->willCompress($tempBatchBuffer)){
|
||||||
|
$batchBuffer = $tempBatchBuffer;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if($batchBuffer === null){
|
||||||
foreach($compressorTargets as $target){
|
foreach($compressorTargets as $target){
|
||||||
foreach($packetBuffers[$bufferId] as $packetBuffer){
|
foreach($packetBuffers[$bufferId] as $packetBuffer){
|
||||||
$target->addToSendBuffer($packetBuffer);
|
$target->addToSendBuffer($packetBuffer);
|
||||||
|
31
src/network/mcpe/compression/ThresholdCompressor.php
Normal file
31
src/network/mcpe/compression/ThresholdCompressor.php
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/*
|
||||||
|
*
|
||||||
|
* ____ _ _ __ __ _ __ __ ____
|
||||||
|
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
|
||||||
|
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
|
||||||
|
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
|
||||||
|
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU Lesser General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* @author PocketMine Team
|
||||||
|
* @link http://www.pocketmine.net/
|
||||||
|
*
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace pocketmine\network\mcpe\compression;
|
||||||
|
|
||||||
|
interface ThresholdCompressor extends Compressor{
|
||||||
|
/**
|
||||||
|
* Returns the minimum size of packet batch that will be compressed. Null means that no packets will be compressed.
|
||||||
|
*/
|
||||||
|
public function getCompressionThreshold() : ?int;
|
||||||
|
}
|
@ -32,7 +32,7 @@ use function zlib_decode;
|
|||||||
use function zlib_encode;
|
use function zlib_encode;
|
||||||
use const ZLIB_ENCODING_RAW;
|
use const ZLIB_ENCODING_RAW;
|
||||||
|
|
||||||
final class ZlibCompressor implements Compressor{
|
final class ZlibCompressor implements ThresholdCompressor{
|
||||||
use SingletonTrait;
|
use SingletonTrait;
|
||||||
|
|
||||||
public const DEFAULT_LEVEL = 7;
|
public const DEFAULT_LEVEL = 7;
|
||||||
@ -48,12 +48,16 @@ final class ZlibCompressor implements Compressor{
|
|||||||
|
|
||||||
public function __construct(
|
public function __construct(
|
||||||
private int $level,
|
private int $level,
|
||||||
private int $minCompressionSize,
|
private ?int $minCompressionSize,
|
||||||
private int $maxDecompressionSize
|
private int $maxDecompressionSize
|
||||||
){}
|
){}
|
||||||
|
|
||||||
|
public function getCompressionThreshold() : ?int{
|
||||||
|
return $this->minCompressionSize;
|
||||||
|
}
|
||||||
|
|
||||||
public function willCompress(string $data) : bool{
|
public function willCompress(string $data) : bool{
|
||||||
return $this->minCompressionSize > -1 && strlen($data) >= $this->minCompressionSize;
|
return $this->minCompressionSize !== null && strlen($data) >= $this->minCompressionSize;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
x
Reference in New Issue
Block a user