Merge pull request #223 from bobthecow/feature/faster-type-checking

Faster context lookups
This commit is contained in:
Justin Hileman
2014-08-18 17:08:40 -07:00
3 changed files with 48 additions and 30 deletions
+8 -6
View File
@@ -609,9 +609,9 @@ class Mustache_Compiler
{ {
if ($this->customEscape) { if ($this->customEscape) {
return sprintf(self::CUSTOM_ESCAPE, $value); return sprintf(self::CUSTOM_ESCAPE, $value);
} else {
return sprintf(self::DEFAULT_ESCAPE, $value, var_export($this->entityFlags, true), var_export($this->charset, true));
} }
return sprintf(self::DEFAULT_ESCAPE, $value, var_export($this->entityFlags, true), var_export($this->charset, true));
} }
/** /**
@@ -631,11 +631,13 @@ class Mustache_Compiler
{ {
if ($id === '.') { if ($id === '.') {
return 'last'; return 'last';
} elseif (strpos($id, '.') === false) {
return 'find';
} else {
return 'findDot';
} }
if (strpos($id, '.') === false) {
return 'find';
}
return 'findDot';
} }
const IS_CALLABLE = '!is_string(%s) && is_callable(%s)'; const IS_CALLABLE = '!is_string(%s) && is_callable(%s)';
+25 -12
View File
@@ -172,19 +172,32 @@ class Mustache_Context
private function findVariableInStack($id, array $stack) private function findVariableInStack($id, array $stack)
{ {
for ($i = count($stack) - 1; $i >= 0; $i--) { for ($i = count($stack) - 1; $i >= 0; $i--) {
if (is_object($stack[$i]) && !($stack[$i] instanceof Closure)) { $frame = &$stack[$i];
// Note that is_callable() *will not work here* switch (gettype($frame)) {
// See https://github.com/bobthecow/mustache.php/wiki/Magic-Methods case 'object':
if (method_exists($stack[$i], $id)) { if (!($frame instanceof Closure)) {
return $stack[$i]->$id(); // Note that is_callable() *will not work here*
} elseif (isset($stack[$i]->$id)) { // See https://github.com/bobthecow/mustache.php/wiki/Magic-Methods
return $stack[$i]->$id; if (method_exists($frame, $id)) {
} elseif ($stack[$i] instanceof ArrayAccess && isset($stack[$i][$id])) { return $frame->$id();
return $stack[$i][$id]; }
}
} elseif (is_array($stack[$i]) && array_key_exists($id, $stack[$i])) { if (isset($frame->$id)) {
return $stack[$i][$id]; return $frame->$id;
}
if ($frame instanceof ArrayAccess && isset($frame[$id])) {
return $frame[$id];
}
}
break;
case 'array':
if (array_key_exists($id, $frame)) {
return $frame[$id];
}
break;
} }
} }
+15 -12
View File
@@ -113,19 +113,22 @@ abstract class Mustache_Template
*/ */
protected function isIterable($value) protected function isIterable($value)
{ {
if (is_object($value)) { switch (gettype($value)) {
return $value instanceof Traversable; case 'object':
} elseif (is_array($value)) { return $value instanceof Traversable;
$i = 0;
foreach ($value as $k => $v) {
if ($k !== $i++) {
return false;
}
}
return true; case 'array':
} else { $i = 0;
return false; foreach ($value as $k => $v) {
if ($k !== $i++) {
return false;
}
}
return true;
default:
return false;
} }
} }