Skip to content

Commit d562df9

Browse files
Static Content Deploy - Optimize files scanning performance
According to XDebug profiling Magento2 calls preg_match() and preg_quote() unreasonable amount of times during static content deploy. I've debugged it and found root cause. This is current algorithm: 1. Get list of all modules 2. Get array of all files in all modules 3. Now try to guess which file belongs to which module by using dynamic regex: preg_quote(), preg_match() I've refactored it to avoid mixing all files in one array and then splitting again. Quite simple fix.
1 parent 50a085d commit d562df9

File tree

1 file changed

+16
-17
lines changed
  • lib/internal/Magento/Framework/App/Utility

1 file changed

+16
-17
lines changed

lib/internal/Magento/Framework/App/Utility/Files.php

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -925,14 +925,12 @@ public function getStaticPreProcessingFiles($filePattern = '*')
925925
$area = '*';
926926
$locale = '*';
927927
$result = [];
928-
$moduleWebPath = [];
929928
$moduleLocalePath = [];
930929
foreach ($this->componentRegistrar->getPaths(ComponentRegistrar::MODULE) as $moduleDir) {
931-
$moduleWebPath[] = $moduleDir . "/view/{$area}/web";
932930
$moduleLocalePath[] = $moduleDir . "/view/{$area}/web/i18n/{$locale}";
933931
}
934932

935-
$this->_accumulateFilesByPatterns($moduleWebPath, $filePattern, $result, '_parseModuleStatic');
933+
$this->_accumulateStaticFiles($area, $filePattern, $result);
936934
$this->_accumulateFilesByPatterns($moduleLocalePath, $filePattern, $result, '_parseModuleLocaleStatic');
937935
$this->accumulateThemeStaticFiles($area, $locale, $filePattern, $result);
938936
self::$_cache[$key] = $result;
@@ -1041,25 +1039,26 @@ protected function _accumulateFilesByPatterns(array $patterns, $filePattern, arr
10411039
}
10421040

10431041
/**
1044-
* Parse meta-info of a static file in module
1042+
* Search static files from all modules by the specified pattern and accumulate meta-info
10451043
*
1046-
* @param string $file
1047-
* @return array
1044+
* @param string $area
1045+
* @param string $filePattern
1046+
* @param array $result
1047+
* @return void
10481048
*/
1049-
protected function _parseModuleStatic($file)
1049+
protected function _accumulateStaticFiles($area, $filePattern, array &$result)
10501050
{
1051-
foreach ($this->componentRegistrar->getPaths(ComponentRegistrar::MODULE) as $moduleName => $modulePath) {
1052-
if (preg_match(
1053-
'/^' . preg_quote("{$modulePath}/", '/') . 'view\/([a-z]+)\/web\/(.+)$/i',
1054-
$file,
1055-
$matches
1056-
) === 1
1057-
) {
1058-
list(, $area, $filePath) = $matches;
1059-
return [$area, '', '', $moduleName, $filePath, $file];
1051+
foreach ($this->componentRegistrar->getPaths(ComponentRegistrar::MODULE) as $moduleName => $moduleDir) {
1052+
$moduleWebPath = $moduleDir . "/view/{$area}/web";
1053+
1054+
foreach (self::getFiles([$moduleWebPath], $filePattern) as $absolutePath) {
1055+
$localPath = substr($absolutePath, strlen($moduleDir) + 1);
1056+
if (preg_match('/^view\/([a-z]+)\/web\/(.+)$/i', $localPath, $matches) === 1) {
1057+
list(, $parsedArea, $parsedPath) = $matches;
1058+
$result[] = [$parsedArea, '', '', $moduleName, $parsedPath, $absolutePath];
1059+
}
10601060
}
10611061
}
1062-
return [];
10631062
}
10641063

10651064
/**

0 commit comments

Comments
 (0)