1
0
Fork 0

Improve memory usage resolving dependencies II

Optimize `rulesByHash` in `Composer\DependencyResolver\RuleSet` to only
use an array with the second element, event with very low probability.
pull/6168/head
rubenrua 2017-02-20 14:40:31 +01:00
parent 4e1887a721
commit 8fccbaa4a4
1 changed files with 14 additions and 5 deletions

View File

@ -63,8 +63,14 @@ class RuleSet implements \IteratorAggregate, \Countable
// Do not add if rule already exists
if (isset($this->rulesByHash[$hash])) {
$potentialDuplicates = $this->rulesByHash[$hash];
foreach ($potentialDuplicates as $potentialDuplicate) {
if ($rule->equals($potentialDuplicate)) {
if (is_array($potentialDuplicates)) {
foreach ($potentialDuplicates as $potentialDuplicate) {
if ($rule->equals($potentialDuplicate)) {
return;
}
}
} else {
if ($rule->equals($potentialDuplicates)) {
return;
}
}
@ -73,7 +79,7 @@ class RuleSet implements \IteratorAggregate, \Countable
if (!isset($this->rules[$type])) {
$this->rules[$type] = array();
}
$this->rules[$type][] = $rule;
$this->ruleById[$this->nextRuleId] = $rule;
$rule->setType($type);
@ -81,9 +87,12 @@ class RuleSet implements \IteratorAggregate, \Countable
$this->nextRuleId++;
if (!isset($this->rulesByHash[$hash])) {
$this->rulesByHash[$hash] = array($rule);
} else {
$this->rulesByHash[$hash] = $rule;
} elseif (is_array($this->rulesByHash[$hash])) {
$this->rulesByHash[$hash][] = $rule;
} else {
$originalRule = $this->rulesByHash[$hash];
$this->rulesByHash[$hash] = array($originalRule, $rule);
}
}