Skip to content

Commit

Permalink
Reuse timings handlers for event handlers of the same events
Browse files Browse the repository at this point in the history
due to direct repeated usage of registerEvent() with closures, we've seen some libraries like muqsit/SimplePacketHandler generate very large timings reports, because a new timings handler gets created every time a plugin registers or unregisters a new packet handler callback.

This change fixes the problem by ensuring that any handlers derived from the same function, handling the same event class, will share the same timer.
  • Loading branch information
dktapps committed May 6, 2023
1 parent 633e77a commit d04da9b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/plugin/PluginManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
use pocketmine\permission\PermissionManager;
use pocketmine\permission\PermissionParser;
use pocketmine\Server;
use pocketmine\timings\TimingsHandler;
use pocketmine\timings\Timings;
use pocketmine\utils\AssumptionFailedError;
use pocketmine\utils\Utils;
use Symfony\Component\Filesystem\Path;
Expand Down Expand Up @@ -651,7 +651,7 @@ public function registerEvent(string $event, \Closure $handler, int $priority, P
throw new PluginException("Plugin attempted to register event handler " . $handlerName . "() to event " . $event . " while not enabled");
}

$timings = new TimingsHandler($handlerName . "(" . (new \ReflectionClass($event))->getShortName() . ")", group: $plugin->getDescription()->getFullName());
$timings = Timings::getEventHandlerTimings($event, $handlerName, $plugin->getDescription()->getFullName());

$registeredListener = new RegisteredListener($handler, $priority, $plugin, $handleCancelled, $timings);
HandlerListManager::global()->getListFor($event)->register($registeredListener);
Expand Down
14 changes: 14 additions & 0 deletions src/timings/Timings.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,8 @@ abstract class Timings{

/** @var TimingsHandler[] */
private static array $events = [];
/** @var TimingsHandler[][] */
private static array $eventHandlers = [];

public static function init() : void{
if(self::$initialized){
Expand Down Expand Up @@ -336,4 +338,16 @@ public static function getEventTimings(Event $event) : TimingsHandler{

return self::$events[$eventClass];
}

/**
* @phpstan-template TEvent of Event
* @phpstan-param class-string<TEvent> $event
*/
public static function getEventHandlerTimings(string $event, string $handlerName, string $group) : TimingsHandler{
if(!isset(self::$eventHandlers[$event][$handlerName])){
self::$eventHandlers[$event][$handlerName] = new TimingsHandler($handlerName . "(" . self::shortenCoreClassName($event, "pocketmine\\event\\") . ")", group: $group);
}

return self::$eventHandlers[$event][$handlerName];
}
}

0 comments on commit d04da9b

Please sign in to comment.