Merge branch '5.1' into 5.x
* 5.1: Optimize circular collection by removing flattening
This commit is contained in:
commit
2994ef0e1a
@ -426,69 +426,42 @@ EOF;
|
|||||||
$this->singleUsePrivateIds[$id] = $id;
|
$this->singleUsePrivateIds[$id] = $id;
|
||||||
}
|
}
|
||||||
|
|
||||||
$newNodes = [];
|
$this->collectCircularReferences($id, $node->getOutEdges(), $checkedNodes);
|
||||||
if (!$this->collectCircularReferences($id, $node->getOutEdges(), $checkedNodes, $newNodes)) {
|
|
||||||
foreach ($newNodes as $newNodeId => $_) {
|
|
||||||
$checkedNodes[$newNodeId] = [];
|
|
||||||
}
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
$nodesToFlatten = $newNodes;
|
|
||||||
do {
|
|
||||||
$changedNodes = [];
|
|
||||||
foreach ($nodesToFlatten as $newNodeId => $_) {
|
|
||||||
$deps = &$checkedNodes[$newNodeId];
|
|
||||||
foreach ($deps as $id => [$path, $depsByConstructor]) {
|
|
||||||
foreach ($checkedNodes[$id] as $depsId => [$subPath, $subDepsByConstructor]) {
|
|
||||||
if (!isset($deps[$depsId]) || ($depsByConstructor && $subDepsByConstructor && !$deps[$depsId][1])) {
|
|
||||||
array_unshift($subPath, $id);
|
|
||||||
$deps[$depsId] = [$subPath, $depsByConstructor && $subDepsByConstructor];
|
|
||||||
$changedNodes += $newNodes[$newNodeId] ?? [];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} while ($nodesToFlatten = $changedNodes);
|
|
||||||
|
|
||||||
foreach ($newNodes as $newNodeId => $_) {
|
|
||||||
if (null !== $n = $checkedNodes[$newNodeId][$newNodeId] ?? null) {
|
|
||||||
$this->addCircularReferences($newNodeId, $n[0], $n[1]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->container->getCompiler()->getServiceReferenceGraph()->clear();
|
$this->container->getCompiler()->getServiceReferenceGraph()->clear();
|
||||||
$this->singleUsePrivateIds = array_diff_key($this->singleUsePrivateIds, $this->circularReferences);
|
$this->singleUsePrivateIds = array_diff_key($this->singleUsePrivateIds, $this->circularReferences);
|
||||||
}
|
}
|
||||||
|
|
||||||
private function collectCircularReferences(string $sourceId, array $edges, array &$checkedNodes, array &$newNodes, array $path = []): bool
|
private function collectCircularReferences(string $sourceId, array $edges, array &$checkedNodes, array $path = [], bool $byConstructor = true): void
|
||||||
{
|
{
|
||||||
$path[$sourceId] = true;
|
$path[$sourceId] = $byConstructor;
|
||||||
$checkedNodes[$sourceId] = [];
|
$checkedNodes[$sourceId] = true;
|
||||||
$newNodes[$sourceId] = [];
|
|
||||||
$circular = false;
|
|
||||||
foreach ($edges as $edge) {
|
foreach ($edges as $edge) {
|
||||||
$node = $edge->getDestNode();
|
$node = $edge->getDestNode();
|
||||||
$id = $node->getId();
|
$id = $node->getId();
|
||||||
if (!$node->getValue() instanceof Definition || $sourceId === $id || $edge->isWeak()) {
|
|
||||||
|
if (!($definition = $node->getValue()) instanceof Definition || $sourceId === $id || ($edge->isLazy() && ($this->proxyDumper ?? $this->getProxyDumper())->isProxyCandidate($definition)) || $edge->isWeak()) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isset($path[$id])) {
|
if (isset($path[$id])) {
|
||||||
$circular = true;
|
$loop = null;
|
||||||
} elseif (!isset($checkedNodes[$id])) {
|
$loopByConstructor = $edge->isReferencedByConstructor();
|
||||||
$circular = $this->collectCircularReferences($id, $node->getOutEdges(), $checkedNodes, $newNodes, $path) || $circular;
|
foreach ($path as $k => $pathByConstructor) {
|
||||||
|
if (null !== $loop) {
|
||||||
|
$loop[] = $k;
|
||||||
|
$loopByConstructor = $loopByConstructor && $pathByConstructor;
|
||||||
|
} elseif ($k === $id) {
|
||||||
|
$loop = [];
|
||||||
}
|
}
|
||||||
|
}
|
||||||
$checkedNodes[$sourceId][$id] = [[], $edge->isReferencedByConstructor()];
|
$this->addCircularReferences($id, $loop, $loopByConstructor);
|
||||||
if (isset($newNodes[$id])) {
|
} elseif (!isset($checkedNodes[$id])) {
|
||||||
$newNodes[$id][$sourceId] = true;
|
$this->collectCircularReferences($id, $node->getOutEdges(), $checkedNodes, $path, $edge->isReferencedByConstructor());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
unset($path[$sourceId]);
|
unset($path[$sourceId]);
|
||||||
|
|
||||||
return $circular;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private function addCircularReferences(string $sourceId, array $currentPath, bool $byConstructor)
|
private function addCircularReferences(string $sourceId, array $currentPath, bool $byConstructor)
|
||||||
|
Reference in New Issue
Block a user