1
0
Fork 0

Optimize PoolBuilder to not load replaced targets if not required

pull/11449/head
Yanick Witschi 2023-05-02 18:08:46 +02:00
parent 010bad5428
commit 995b806dfe
2 changed files with 34 additions and 5 deletions

View File

@ -94,6 +94,11 @@ class PoolBuilder
* @phpstan-var array<int, array<string, array<string, PackageInterface>>> * @phpstan-var array<int, array<string, array<string, PackageInterface>>>
*/ */
private $loadedPerRepo = []; private $loadedPerRepo = [];
/**
* @var array[]
* @phpstan-var array<string, bool>
*/
private $optionalPackages = [];
/** /**
* @var BasePackage[] * @var BasePackage[]
*/ */
@ -235,8 +240,9 @@ class PoolBuilder
} }
} }
while (!empty($this->packagesToLoad)) { while ([] !== $this->packagesToLoad || [] !== $this->optionalPackages) {
$this->loadPackagesMarkedForLoading($request, $repositories); $this->loadPackagesMarkedForLoading($request, $repositories);
$this->loadOptionalPackages($request);
} }
if (\count($this->temporaryConstraints) > 0) { if (\count($this->temporaryConstraints) > 0) {
@ -483,7 +489,8 @@ class PoolBuilder
if ($request->getUpdateAllowTransitiveRootDependencies() || !$skippedRootRequires) { if ($request->getUpdateAllowTransitiveRootDependencies() || !$skippedRootRequires) {
$this->unlockPackage($request, $repositories, $replace); $this->unlockPackage($request, $repositories, $replace);
$this->markPackageNameForLoading($request, $replace, $link->getConstraint()); // Mark as optional - if no other package requires it, we don't need to load it
$this->markPackageNameForOptionalLoading($replace);
} else { } else {
foreach ($skippedRootRequires as $rootRequire) { foreach ($skippedRootRequires as $rootRequire) {
if (!isset($this->updateAllowWarned[$rootRequire])) { if (!isset($this->updateAllowWarned[$rootRequire])) {
@ -651,10 +658,12 @@ class PoolBuilder
if (isset($requires[$lockedPackage->getName()])) { if (isset($requires[$lockedPackage->getName()])) {
$this->markPackageNameForLoading($request, $lockedPackage->getName(), $requires[$lockedPackage->getName()]->getConstraint()); $this->markPackageNameForLoading($request, $lockedPackage->getName(), $requires[$lockedPackage->getName()]->getConstraint());
} }
foreach ($lockedPackage->getReplaces() as $replace) { foreach ($lockedPackage->getReplaces() as $replace) {
if (isset($requires[$replace->getTarget()], $this->skippedLoad[$replace->getTarget()])) { if (isset($requires[$replace->getTarget()], $this->skippedLoad[$replace->getTarget()])) {
$this->unlockPackage($request, $repositories, $replace->getTarget()); $this->unlockPackage($request, $repositories, $replace->getTarget());
$this->markPackageNameForLoading($request, $replace->getTarget(), $requires[$replace->getTarget()]->getConstraint()); // Mark as optional - if no other package requires it, we don't need to load it
$this->markPackageNameForOptionalLoading($replace->getTarget());
} }
} }
} }
@ -664,6 +673,28 @@ class PoolBuilder
} }
} }
private function markPackageNameForOptionalLoading(string $name): void
{
$this->optionalPackages[$name] = true;
}
private function loadOptionalPackages(Request $request): void
{
if ([] === $this->optionalPackages) {
return;
}
foreach ($this->packages as $package) {
foreach ($package->getRequires() as $link) {
if (isset($this->optionalPackages[$link->getTarget()])) {
$this->markPackageNameForLoading($request, $link->getTarget(), $link->getConstraint());
}
}
}
$this->optionalPackages = [];
}
/** /**
* @param RepositoryInterface[] $repositories * @param RepositoryInterface[] $repositories
*/ */

View File

@ -101,7 +101,6 @@ Check that replacers from additional repositories are loaded when doing a partia
"indirect/replacer-1.0.0.0", "indirect/replacer-1.0.0.0",
"replacer/package-1.2.0.0", "replacer/package-1.2.0.0",
"replacer/package-1.0.0.0", "replacer/package-1.0.0.0",
"base/package-1.0.0.0",
"shared/dep-1.0.0.0", "shared/dep-1.0.0.0",
"shared/dep-1.2.0.0" "shared/dep-1.2.0.0"
] ]
@ -112,6 +111,5 @@ Check that replacers from additional repositories are loaded when doing a partia
"indirect/replacer-1.0.0.0", "indirect/replacer-1.0.0.0",
"replacer/package-1.2.0.0", "replacer/package-1.2.0.0",
"replacer/package-1.0.0.0", "replacer/package-1.0.0.0",
"base/package-1.0.0.0",
"shared/dep-1.2.0.0" "shared/dep-1.2.0.0"
] ]