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