Ver código fonte

Performance improvement on Router to avoid array_*

Changes on the Router to avoid array_diff, array_keys and other inneficient array_ functions.

This is a "recommit" to respect Symfony Contribution rules
Xavier De Cock 14 anos atrás
pai
commit
865ee54558
1 arquivos alterados com 15 adições e 5 exclusões
  1. 15 5
      src/Symfony/Component/Routing/Router.php

+ 15 - 5
src/Symfony/Component/Routing/Router.php

@@ -68,12 +68,22 @@ class Router implements RouterInterface
             'resource_type'          => null,
         );
 
-        // check option names
-        if ($diff = array_diff(array_keys($options), array_keys($this->options))) {
-            throw new \InvalidArgumentException(sprintf('The Router does not support the following options: \'%s\'.', implode('\', \'', $diff)));
+        // check option names and live merge, if errors are encountered Exception will be thrown
+        $invalid = array();
+        $isInvalid = false;
+        // This allows to avoid innefficients array_diff, array_keys, and so on, we only walks one the overriden options
+        // With array_keys, array_diff and array_merge there is 3 full walk of the $this->options array and 2 of $options.
+        foreach ($options as $key => $value) {
+            if (array_key_exists($key, $this->options)) {
+                $this->options[$key] = $value;
+            } else {
+                $isInvalid = true;
+                $invalid[] = $key;
+            }
+        }
+        if ($isInvalid) {
+            throw new \InvalidArgumentException(sprintf('The Router does not support the following options: \'%s\'.', implode('\', \'', $invalid)));
         }
-
-        $this->options = array_merge($this->options, $options);
     }
 
     /**