RakLib: split PthreadsChannelWriter into two implementations

this gains a very small performance improvement by avoiding unnecessary !== null checks on every packet written in either direction. It's insignificant for sure, but I just found this code in an old stash, so what the heck.
This commit is contained in:
Dylan K. Taylor 2021-07-22 18:52:58 +01:00
parent 5c609cc1c1
commit 832a156fc7
No known key found for this signature in database
GPG Key ID: 8927471A91CAFD3D
3 changed files with 44 additions and 8 deletions

View File

@ -23,18 +23,12 @@ use raklib\server\ipc\InterThreadChannelWriter;
final class PthreadsChannelWriter implements InterThreadChannelWriter{
/** @var \Threaded */
private $buffer;
/** @var SleeperNotifier|null */
private $notifier;
public function __construct(\Threaded $buffer, ?SleeperNotifier $notifier = null){
public function __construct(\Threaded $buffer){
$this->buffer = $buffer;
$this->notifier = $notifier;
}
public function write(string $str) : void{
$this->buffer[] = $str;
if($this->notifier !== null){
$this->notifier->wakeupSleeper();
}
}
}

View File

@ -153,7 +153,7 @@ class RakLibServer extends Thread{
$this->maxMtuSize,
new SimpleProtocolAcceptor($this->protocolVersion),
new UserToRakLibThreadMessageReceiver(new PthreadsChannelReader($this->mainToThreadBuffer)),
new RakLibToUserThreadMessageSender(new PthreadsChannelWriter($this->threadToMainBuffer, $this->mainThreadNotifier)),
new RakLibToUserThreadMessageSender(new SnoozeAwarePthreadsChannelWriter($this->threadToMainBuffer, $this->mainThreadNotifier)),
new ExceptionTraceCleaner($this->mainPath)
);
$this->synchronized(function() : void{

View File

@ -0,0 +1,42 @@
<?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\raklib;
use pocketmine\snooze\SleeperNotifier;
use raklib\server\ipc\InterThreadChannelWriter;
final class SnoozeAwarePthreadsChannelWriter implements InterThreadChannelWriter{
private \Threaded $buffer;
private SleeperNotifier $notifier;
public function __construct(\Threaded $buffer, SleeperNotifier $notifier){
$this->buffer = $buffer;
$this->notifier = $notifier;
}
public function write(string $str) : void{
$this->buffer[] = $str;
$this->notifier->wakeupSleeper();
}
}