Skip to content

Commit 94073e2

Browse files
Optimize performance by using isset() instead of in_array()
I've run XDebug profiler to see why static content deploy takes so much time. Many in_array() calls grabbed my attention and I've fixed it. Co-Authored-By: Ihor Sviziev <[email protected]>
1 parent 68c48d5 commit 94073e2

File tree

2 files changed

+11
-9
lines changed

2 files changed

+11
-9
lines changed

lib/internal/Magento/Framework/Module/ModuleList/Loader.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -140,15 +140,15 @@ private function sortBySequence(array $origList): array
140140

141141
$expanded[] = [
142142
'name' => $moduleName,
143-
'sequence' => $sequence,
143+
'sequence_set' => array_flip($sequence),
144144
];
145145
}
146146

147147
// Use "bubble sorting" because usort does not check each pair of elements and in this case it is important
148148
$total = count($expanded);
149149
for ($i = 0; $i < $total - 1; $i++) {
150150
for ($j = $i; $j < $total; $j++) {
151-
if (in_array($expanded[$j]['name'], $expanded[$i]['sequence'], true)) {
151+
if (isset($expanded[$i]['sequence_set'][$expanded[$j]['name']])) {
152152
$temp = $expanded[$i];
153153
$expanded[$i] = $expanded[$j];
154154
$expanded[$j] = $temp;
@@ -196,18 +196,19 @@ private function prearrangeModules(array $modules): array
196196
*/
197197
private function expandSequence($list, $name, $accumulated = [])
198198
{
199-
$accumulated[] = $name;
199+
$accumulated[$name] = true;
200200
$result = $list[$name]['sequence'];
201+
$allResults = [];
201202
foreach ($result as $relatedName) {
202-
if (in_array($relatedName, $accumulated)) {
203-
throw new \Exception("Circular sequence reference from '{$name}' to '{$relatedName}'.");
203+
if (isset($accumulated[$relatedName])) {
204+
throw new \LogicException("Circular sequence reference from '{$name}' to '{$relatedName}'.");
204205
}
205206
if (!isset($list[$relatedName])) {
206207
continue;
207208
}
208-
$relatedResult = $this->expandSequence($list, $relatedName, $accumulated);
209-
$result = array_unique(array_merge($result, $relatedResult));
209+
$allResults[] = $this->expandSequence($list, $relatedName, $accumulated);
210210
}
211-
return $result;
211+
$allResults[] = $result;
212+
return array_unique(array_merge(...$allResults));
212213
}
213214
}

lib/internal/Magento/Framework/Setup/Lists.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,10 @@ public function getLocaleList()
9696
$languages = (new LanguageBundle())->get(Resolver::DEFAULT_LOCALE)['Languages'];
9797
$countries = (new RegionBundle())->get(Resolver::DEFAULT_LOCALE)['Countries'];
9898
$locales = \ResourceBundle::getLocales('') ?: [];
99+
$allowedLocales = array_flip($this->allowedLocales);
99100
$list = [];
100101
foreach ($locales as $locale) {
101-
if (!in_array($locale, $this->allowedLocales)) {
102+
if (!isset($allowedLocales[$locale])) {
102103
continue;
103104
}
104105
$language = \Locale::getPrimaryLanguage($locale);

0 commit comments

Comments
 (0)