From: Alexander Ebert Date: Mon, 15 Apr 2024 13:35:47 +0000 (+0200) Subject: Reuse the `\IntlDateFormatter` X-Git-Tag: 6.1.0_Alpha_1~81^2~3 X-Git-Url: https://git.stricted.de/?a=commitdiff_plain;h=3abbce578639cd7fa3b09676e3698beb81d18588;p=GitHub%2FWoltLab%2FWCF.git Reuse the `\IntlDateFormatter` Constructing the object every time is a rather expensive process. The test for a page with 96 calls to the `|time` modifier showed an average of 8ms spent inside the `execute()` method. The optimized version showed a consistent runtime of around 1.3ms. --- diff --git a/wcfsetup/install/files/lib/system/template/plugin/TimeModifierTemplatePlugin.class.php b/wcfsetup/install/files/lib/system/template/plugin/TimeModifierTemplatePlugin.class.php index ee8597d1f6..cb59f8d35f 100644 --- a/wcfsetup/install/files/lib/system/template/plugin/TimeModifierTemplatePlugin.class.php +++ b/wcfsetup/install/files/lib/system/template/plugin/TimeModifierTemplatePlugin.class.php @@ -20,6 +20,9 @@ use wcf\system\WCF; */ class TimeModifierTemplatePlugin implements IModifierTemplatePlugin { + /** @var array */ + private array $dateFormatter = []; + /** * @inheritDoc */ @@ -34,12 +37,23 @@ class TimeModifierTemplatePlugin implements IModifierTemplatePlugin $isFutureDate = $dateTime->getTimestamp() > TIME_NOW; - $dateAndTime = \IntlDateFormatter::create( - WCF::getLanguage()->getLocale(), - \IntlDateFormatter::LONG, - \IntlDateFormatter::SHORT, - WCF::getUser()->getTimeZone() - )->format($dateTime); + $locale = WCF::getLanguage()->getLocale(); + $timeZone = WCF::getUser()->getTimeZone(); + + $key = $locale . '::' . $timeZone->getName(); + $dateFormatter = $this->dateFormatter[$key] ?? null; + if ($dateFormatter === null) { + $dateFormatter = \IntlDateFormatter::create( + WCF::getLanguage()->getLocale(), + \IntlDateFormatter::LONG, + \IntlDateFormatter::SHORT, + WCF::getUser()->getTimeZone() + ); + + $this->dateFormatter[$key] = $dateFormatter; + } + + $dateAndTime = $dateFormatter->format($dateTime); return \sprintf( '%s',