From 6f082b6d3bbdb9a41f4a1d386cd93bb2b2177b20 Mon Sep 17 00:00:00 2001 From: Alexander Ebert Date: Wed, 13 Mar 2019 13:15:37 +0100 Subject: [PATCH] `SingletonFactory::getInstance()` now uses `isset` instead of `array_key_exists` The behavior remains unchanged for both valid calls, as well as for detecting infinite loops due to cross references. --- .../files/lib/system/SingletonFactory.class.php | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/wcfsetup/install/files/lib/system/SingletonFactory.class.php b/wcfsetup/install/files/lib/system/SingletonFactory.class.php index 95d335b900..8e0a8ee5b1 100644 --- a/wcfsetup/install/files/lib/system/SingletonFactory.class.php +++ b/wcfsetup/install/files/lib/system/SingletonFactory.class.php @@ -49,13 +49,15 @@ abstract class SingletonFactory { * @throws SystemException */ public static final function getInstance() { - $className = get_called_class(); - if (!array_key_exists($className, self::$__singletonObjects)) { - self::$__singletonObjects[$className] = null; + $className = static::class; + if (!isset(self::$__singletonObjects[$className])) { + // The previously used `null` value forced us into using `array_key_exists()` which has a bad performance, + // especially with the growing list of derived classes that are used today. Saves a few ms on every request. + self::$__singletonObjects[$className] = false; self::$__singletonObjects[$className] = new $className(); } - else if (self::$__singletonObjects[$className] === null) { - throw new SystemException("Infinite loop detected while trying to retrieve object for '".$className."'"); + else if (self::$__singletonObjects[$className] === false) { + throw new SystemException("Infinite loop detected while trying to retrieve object for '" . $className . "'"); } return self::$__singletonObjects[$className]; -- 2.20.1