Skip to content

Commit 74b9d9b

Browse files
ENGCOM-5923: Optimize performance by using isset() instead of in_array() #24677
- Merge Pull Request #24677 from ven-com/magento2:2.3-in-array-fixes - Merged commits: 1. 94073e2 2. d875236
2 parents 68c48d5 + d875236 commit 74b9d9b

File tree

2 files changed

+14
-10
lines changed

2 files changed

+14
-10
lines changed

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

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ public function __construct(
7878
public function load(array $exclude = [])
7979
{
8080
$result = [];
81+
$excludeSet = array_flip($exclude);
82+
8183
foreach ($this->getModuleConfigs() as list($file, $contents)) {
8284
try {
8385
$this->parser->loadXML($contents);
@@ -93,7 +95,7 @@ public function load(array $exclude = [])
9395

9496
$data = $this->converter->convert($this->parser->getDom());
9597
$name = key($data);
96-
if (!in_array($name, $exclude)) {
98+
if (!isset($excludeSet[$name])) {
9799
$result[$name] = $data[$name];
98100
}
99101
}
@@ -140,15 +142,15 @@ private function sortBySequence(array $origList): array
140142

141143
$expanded[] = [
142144
'name' => $moduleName,
143-
'sequence' => $sequence,
145+
'sequence_set' => array_flip($sequence),
144146
];
145147
}
146148

147149
// Use "bubble sorting" because usort does not check each pair of elements and in this case it is important
148150
$total = count($expanded);
149151
for ($i = 0; $i < $total - 1; $i++) {
150152
for ($j = $i; $j < $total; $j++) {
151-
if (in_array($expanded[$j]['name'], $expanded[$i]['sequence'], true)) {
153+
if (isset($expanded[$i]['sequence_set'][$expanded[$j]['name']])) {
152154
$temp = $expanded[$i];
153155
$expanded[$i] = $expanded[$j];
154156
$expanded[$j] = $temp;
@@ -196,18 +198,19 @@ private function prearrangeModules(array $modules): array
196198
*/
197199
private function expandSequence($list, $name, $accumulated = [])
198200
{
199-
$accumulated[] = $name;
201+
$accumulated[$name] = true;
200202
$result = $list[$name]['sequence'];
203+
$allResults = [];
201204
foreach ($result as $relatedName) {
202-
if (in_array($relatedName, $accumulated)) {
203-
throw new \Exception("Circular sequence reference from '{$name}' to '{$relatedName}'.");
205+
if (isset($accumulated[$relatedName])) {
206+
throw new \LogicException("Circular sequence reference from '{$name}' to '{$relatedName}'.");
204207
}
205208
if (!isset($list[$relatedName])) {
206209
continue;
207210
}
208-
$relatedResult = $this->expandSequence($list, $relatedName, $accumulated);
209-
$result = array_unique(array_merge($result, $relatedResult));
211+
$allResults[] = $this->expandSequence($list, $relatedName, $accumulated);
210212
}
211-
return $result;
213+
$allResults[] = $result;
214+
return array_unique(array_merge(...$allResults));
212215
}
213216
}

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)