Hot path optimisation for DataPacketSendEvent

This commit is contained in:
Dylan K. Taylor 2023-08-01 17:41:53 +01:00
parent 2608637210
commit 0b86fafafb
No known key found for this signature in database
GPG Key ID: 8927471A91CAFD3D
2 changed files with 16 additions and 10 deletions

View File

@ -464,12 +464,16 @@ class NetworkSession{
$timings = Timings::getSendDataPacketTimings($packet); $timings = Timings::getSendDataPacketTimings($packet);
$timings->startTiming(); $timings->startTiming();
try{ try{
$ev = new DataPacketSendEvent([$this], [$packet]); if(DataPacketSendEvent::hasHandlers()){
$ev->call(); $ev = new DataPacketSendEvent([$this], [$packet]);
if($ev->isCancelled()){ $ev->call();
return false; if($ev->isCancelled()){
return false;
}
$packets = $ev->getPackets();
}else{
$packets = [$packet];
} }
$packets = $ev->getPackets();
foreach($packets as $evPacket){ foreach($packets as $evPacket){
$this->addToSendBuffer(self::encodePacketTimed(PacketSerializer::encoder($this->packetSerializerContext), $evPacket)); $this->addToSendBuffer(self::encodePacketTimed(PacketSerializer::encoder($this->packetSerializerContext), $evPacket));

View File

@ -44,12 +44,14 @@ final class StandardPacketBroadcaster implements PacketBroadcaster{
public function broadcastPackets(array $recipients, array $packets) : void{ public function broadcastPackets(array $recipients, array $packets) : void{
//TODO: this shouldn't really be called here, since the broadcaster might be replaced by an alternative //TODO: this shouldn't really be called here, since the broadcaster might be replaced by an alternative
//implementation that doesn't fire events //implementation that doesn't fire events
$ev = new DataPacketSendEvent($recipients, $packets); if(DataPacketSendEvent::hasHandlers()){
$ev->call(); $ev = new DataPacketSendEvent($recipients, $packets);
if($ev->isCancelled()){ $ev->call();
return; if($ev->isCancelled()){
return;
}
$packets = $ev->getPackets();
} }
$packets = $ev->getPackets();
$compressors = []; $compressors = [];