Skip to content

Commit 4a42eea

Browse files
ACPT-1929
Cache result of isObjectInClassList because now that its time is O(n) when search for matching classes, when same class is checked twice, it can be O(1) instead when using the cache
1 parent b9cb9d1 commit 4a42eea

File tree

1 file changed

+17
-5
lines changed
  • lib/internal/Magento/Framework/ObjectManager/Resetter

1 file changed

+17
-5
lines changed

lib/internal/Magento/Framework/ObjectManager/Resetter/Resetter.php

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,15 @@ class Resetter implements ResetterInterface
3131
/** @var WeakMapSorter|null Note: We use temporal coupling here because of chicken/egg during bootstrapping */
3232
private ?WeakMapSorter $weakMapSorter = null;
3333

34-
/**
35-
* @var array
36-
*/
34+
/** @var array */
3735
private array $reflectionCache = [];
3836

37+
/** @var array */
38+
private array $isObjectInClassListCache = [];
39+
40+
/** @var array */
41+
private readonly array $classList;
42+
3943
/**
4044
* @param ComponentRegistrarInterface|null $componentRegistrar
4145
* @param array $classList
@@ -44,7 +48,7 @@ class Resetter implements ResetterInterface
4448
*/
4549
public function __construct(
4650
private ?ComponentRegistrarInterface $componentRegistrar = null,
47-
private array $classList = [],
51+
array $classList = [],
4852
) {
4953
if (null === $this->componentRegistrar) {
5054
$this->componentRegistrar = new ComponentRegistrar();
@@ -57,8 +61,9 @@ public function __construct(
5761
if (!$resetData) {
5862
throw new LocalizedException(__('Error parsing %1', $resetPath));
5963
}
60-
$this->classList += $resetData;
64+
$classList += $resetData;
6165
}
66+
$this->classList = $classList;
6267
$this->resetAfterWeakMap = new WeakMap;
6368
}
6469

@@ -138,11 +143,18 @@ public function setObjectManager(ObjectManagerInterface $objectManager) : void
138143
*/
139144
public function isObjectInClassList(object $object)
140145
{
146+
$className = \get_class($object);
147+
$isObjectInClassListCachedValue = $this->isObjectInClassListCache[$className] ?? null;
148+
if (null !== $isObjectInClassListCachedValue) {
149+
return $isObjectInClassListCachedValue;
150+
}
141151
foreach ($this->classList as $key => $value) {
142152
if ($object instanceof $key) {
153+
$this->isObjectInClassListCache[$className] = true;
143154
return true;
144155
}
145156
}
157+
$this->isObjectInClassListCache[$className] = false;
146158
return false;
147159
}
148160

0 commit comments

Comments
 (0)