Prechádzať zdrojové kódy

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.
rubenrua 8 rokov pred
rodič
commit
8fccbaa4a4
1 zmenil súbory, kde vykonal 14 pridanie a 5 odobranie
  1. 14 5
      src/Composer/DependencyResolver/RuleSet.php

+ 14 - 5
src/Composer/DependencyResolver/RuleSet.php

@@ -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);
         }
     }