Skip to content

Commit acb4ca9

Browse files
committed
Cache the result of a rendered reaction
This greatly reduces the time spent when the same reaction appears multiple times. It does increase the static memory usage but at the same time cuts the GC'ed memory by a lot. In a test with 32 calls this reduced the time by ~75% and reduced memory usage by ~71%.
1 parent e40d2c4 commit acb4ca9

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

wcfsetup/install/files/lib/data/reaction/type/ReactionType.class.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@ class ReactionType extends DatabaseObject implements ITitledObject
2727
*/
2828
protected static $databaseTableIndexName = 'reactionTypeID';
2929

30+
/**
31+
* Cache for the rendered icons.
32+
* @var array<int, string>
33+
*/
34+
private array $renderedIcons = [];
35+
3036
/**
3137
* @inheritDoc
3238
*/
@@ -42,9 +48,13 @@ public function getTitle(): string
4248
*/
4349
public function renderIcon()
4450
{
45-
return WCF::getTPL()->render('wcf', 'reactionTypeImage', [
46-
'reactionType' => $this,
47-
]);
51+
if (!isset($this->renderedIcons[$this->reactionTypeID])) {
52+
$this->renderedIcons[$this->reactionTypeID] = WCF::getTPL()->render('wcf', 'reactionTypeImage', [
53+
'reactionType' => $this,
54+
]);
55+
}
56+
57+
return $this->renderedIcons[$this->reactionTypeID];
4858
}
4959

5060
/**

0 commit comments

Comments
 (0)