Browse Source

Replace is_object/is_array with switch on gettype

When checking multiple types, this is ~10% slower in the worst case, and ~30-33% faster for the average and best case.

See #218
Justin Hileman 11 years ago
parent
commit
3d37a88f0b
2 changed files with 34 additions and 24 deletions
  1. 20 13
      src/Mustache/Context.php
  2. 14 11
      src/Mustache/Template.php

+ 20 - 13
src/Mustache/Context.php

@@ -172,19 +172,26 @@ class Mustache_Context
     private function findVariableInStack($id, array $stack)
     {
         for ($i = count($stack) - 1; $i >= 0; $i--) {
-            if (is_object($stack[$i]) && !($stack[$i] instanceof Closure)) {
-
-                // Note that is_callable() *will not work here*
-                // See https://github.com/bobthecow/mustache.php/wiki/Magic-Methods
-                if (method_exists($stack[$i], $id)) {
-                    return $stack[$i]->$id();
-                } elseif (isset($stack[$i]->$id)) {
-                    return $stack[$i]->$id;
-                } elseif ($stack[$i] instanceof ArrayAccess && isset($stack[$i][$id])) {
-                    return $stack[$i][$id];
-                }
-            } elseif (is_array($stack[$i]) && array_key_exists($id, $stack[$i])) {
-                return $stack[$i][$id];
+            switch (gettype($stack[$i])) {
+                case 'object':
+                    if (!($stack[$i] instanceof Closure)) {
+                        // Note that is_callable() *will not work here*
+                        // See https://github.com/bobthecow/mustache.php/wiki/Magic-Methods
+                        if (method_exists($stack[$i], $id)) {
+                            return $stack[$i]->$id();
+                        } elseif (isset($stack[$i]->$id)) {
+                            return $stack[$i]->$id;
+                        } elseif ($stack[$i] instanceof ArrayAccess && isset($stack[$i][$id])) {
+                            return $stack[$i][$id];
+                        }
+                    }
+                    break;
+
+                case 'array':
+                    if (array_key_exists($id, $stack[$i])) {
+                        return $stack[$i][$id];
+                    }
+                    break;
             }
         }
 

+ 14 - 11
src/Mustache/Template.php

@@ -113,19 +113,22 @@ abstract class Mustache_Template
      */
     protected function isIterable($value)
     {
-        if (is_object($value)) {
-            return $value instanceof Traversable;
-        } elseif (is_array($value)) {
-            $i = 0;
-            foreach ($value as $k => $v) {
-                if ($k !== $i++) {
-                    return false;
+        switch (gettype($value)) {
+            case 'object':
+                return $value instanceof Traversable;
+
+            case 'array':
+                $i = 0;
+                foreach ($value as $k => $v) {
+                    if ($k !== $i++) {
+                        return false;
+                    }
                 }
-            }
 
-            return true;
-        } else {
-            return false;
+                return true;
+
+            default:
+                return false;
         }
     }