Skip to content
Snippets Groups Projects
Commit 97f33acf authored by catch's avatar catch
Browse files

Issue #2407187 by artem_sylchuk, longwave, Matroskeen, Berdir, Wim Leers,...

Issue #2407187 by artem_sylchuk, longwave, Matroskeen, Berdir, Wim Leers, andypost, abramm: Optimize LibraryDependencyResolver::getMinimalRepresentativeSubset() and win >=4%

(cherry picked from commit 3e64338b)
parent 859109f0
No related branches found
No related tags found
No related merge requests found
......@@ -14,6 +14,13 @@ class LibraryDependencyResolver implements LibraryDependencyResolverInterface {
*/
protected $libraryDiscovery;
/**
* The libraries dependencies.
*
* @var array
*/
protected $librariesDependencies = [];
/**
* Constructs a new LibraryDependencyResolver instance.
*
......@@ -28,7 +35,14 @@ public function __construct(LibraryDiscoveryInterface $library_discovery) {
* {@inheritdoc}
*/
public function getLibrariesWithDependencies(array $libraries) {
return $this->doGetDependencies($libraries);
$return = [];
foreach ($libraries as $library) {
if (!isset($this->librariesDependencies[$library])) {
$this->librariesDependencies[$library] = $this->doGetDependencies([$library]);
}
$return += $this->librariesDependencies[$library];
}
return array_values($return);
}
/**
......@@ -49,13 +63,13 @@ public function getLibrariesWithDependencies(array $libraries) {
*/
protected function doGetDependencies(array $libraries_with_unresolved_dependencies, array $final_libraries = []) {
foreach ($libraries_with_unresolved_dependencies as $library) {
if (!in_array($library, $final_libraries)) {
if (!isset($final_libraries[$library])) {
list($extension, $name) = explode('/', $library, 2);
$definition = $this->libraryDiscovery->getLibraryByName($extension, $name);
if (!empty($definition['dependencies'])) {
$final_libraries = $this->doGetDependencies($definition['dependencies'], $final_libraries);
}
$final_libraries[] = $library;
$final_libraries[$library] = $library;
}
}
return $final_libraries;
......@@ -67,31 +81,15 @@ protected function doGetDependencies(array $libraries_with_unresolved_dependenci
public function getMinimalRepresentativeSubset(array $libraries) {
assert(count($libraries) === count(array_unique($libraries)), '$libraries can\'t contain duplicate items.');
$minimal = [];
// Determine each library's dependencies.
$with_deps = [];
$all_dependencies = [];
foreach ($libraries as $library) {
$with_deps[$library] = $this->getLibrariesWithDependencies([$library]);
}
foreach ($libraries as $library) {
$exists = FALSE;
foreach ($with_deps as $other_library => $dependencies) {
if ($library == $other_library) {
continue;
}
if (in_array($library, $dependencies)) {
$exists = TRUE;
break;
}
}
if (!$exists) {
$minimal[] = $library;
}
$with_deps = $this->getLibrariesWithDependencies([$library]);
// We don't need library itself listed in the dependencies.
$all_dependencies += array_diff($with_deps, [$library]);
}
return $minimal;
return array_values(array_diff($libraries, array_intersect($all_dependencies, $libraries)));
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment