NetworkSession: store Packet[] for buffering instead of PacketBatch

this reduces memory allocations for buffering (in theory).
This commit is contained in:
Dylan K. Taylor 2020-07-22 15:18:01 +01:00
parent ed144a1709
commit 2fcee432c1

View File

@ -151,8 +151,8 @@ class NetworkSession{
/** @var EncryptionContext */
private $cipher;
/** @var PacketBatch|null */
private $sendBuffer;
/** @var Packet[] */
private $sendBuffer = [];
/**
* @var \SplQueue|CompressBatchPromise[]
@ -416,10 +416,7 @@ class NetworkSession{
$timings = Timings::getSendDataPacketTimings($packet);
$timings->startTiming();
try{
if($this->sendBuffer === null){
$this->sendBuffer = new PacketBatch();
}
$this->sendBuffer->putPacket($packet);
$this->sendBuffer[] = $packet;
$this->manager->scheduleUpdate($this); //schedule flush at end of tick
}finally{
$timings->stopTiming();
@ -427,9 +424,9 @@ class NetworkSession{
}
private function flushSendBuffer(bool $immediate = false) : void{
if($this->sendBuffer !== null){
$promise = $this->server->prepareBatch($this->sendBuffer, $this->compressor, $immediate);
$this->sendBuffer = null;
if(count($this->sendBuffer) > 0){
$promise = $this->server->prepareBatch(PacketBatch::fromPackets(...$this->sendBuffer), $this->compressor, $immediate);
$this->sendBuffer = [];
$this->queueCompressed($promise, $immediate);
}
}
@ -944,9 +941,7 @@ class NetworkSession{
return true; //keep ticking until timeout
}
if($this->sendBuffer !== null){
$this->flushSendBuffer();
}
$this->flushSendBuffer();
return false;
}