NetworkSession: Force use of async compression during the login sequence when latency doesn't matter

closes #3907
this reduces the impact of compression on the login sequence by about 90%; however, since compression only accounted for about 30% of said overhead at most, it's not really a massive difference.
This commit is contained in:
Dylan K. Taylor 2020-11-26 23:39:19 +00:00
parent 4ea5401d72
commit 0be60fe1eb
2 changed files with 17 additions and 5 deletions

View File

@ -1236,18 +1236,21 @@ class Server{
/**
* Broadcasts a list of packets in a batch to a list of players
*
* @param bool|null $sync Compression on the main thread (true) or workers (false). Default is automatic (null).
*/
public function prepareBatch(PacketBatch $stream, Compressor $compressor, bool $forceSync = false) : CompressBatchPromise{
public function prepareBatch(PacketBatch $stream, Compressor $compressor, ?bool $sync = null) : CompressBatchPromise{
try{
Timings::$playerNetworkSendCompressTimer->startTiming();
$buffer = $stream->getBuffer();
if(!$compressor->willCompress($buffer)){
$forceSync = true;
if($sync === null){
$sync = !($this->networkCompressionAsync && $compressor->willCompress($buffer));
}
$promise = new CompressBatchPromise();
if(!$forceSync and $this->networkCompressionAsync){
if(!$sync){
$task = new CompressBatchTask($buffer, $promise, $compressor);
$this->asyncPool->submitTask($task);
}else{

View File

@ -164,6 +164,8 @@ class NetworkSession{
private $compressedQueue;
/** @var Compressor */
private $compressor;
/** @var bool */
private $forceAsyncCompression = true;
/** @var PacketPool */
private $packetPool;
@ -424,7 +426,13 @@ class NetworkSession{
private function flushSendBuffer(bool $immediate = false) : void{
if(count($this->sendBuffer) > 0){
$promise = $this->server->prepareBatch(PacketBatch::fromPackets(...$this->sendBuffer), $this->compressor, $immediate);
$syncMode = null; //automatic
if($immediate){
$syncMode = true;
}elseif($this->forceAsyncCompression){
$syncMode = false;
}
$promise = $this->server->prepareBatch(PacketBatch::fromPackets(...$this->sendBuffer), $this->compressor, $syncMode);
$this->sendBuffer = [];
$this->queueCompressedNoBufferFlush($promise, $immediate);
}
@ -641,6 +649,7 @@ class NetworkSession{
$this->logger->debug("Received spawn response, entering in-game phase");
$this->player->setImmobile(false); //TODO: HACK: we set this during the spawn sequence to prevent the client sending junk movements
$this->player->doFirstSpawn();
$this->forceAsyncCompression = false;
$this->setHandler(new InGamePacketHandler($this->player, $this));
}