Override encoding to improve tokenizer performance

Speeds up tokenizing by ~15% for most users, and by 50% for users with mbstring.func_overload set!

See #144
This commit is contained in:
Justin Hileman
2014-02-20 09:53:06 -08:00
parent 6215e6b1b1
commit e00bebd141
+16 -3
View File
@@ -92,6 +92,14 @@ class Mustache_Tokenizer
*/ */
public function scan($text, $delimiters = null) public function scan($text, $delimiters = null)
{ {
// Setting mbstring.func_overload makes things *really* slow.
// Let's do everyone a favor and scan this string as ASCII instead.
$encoding = null;
if (function_exists('mb_internal_encoding') && ini_get('mbstring.func_overload') & 2) {
$encoding = mb_internal_encoding();
mb_internal_encoding('ASCII');
}
$this->reset(); $this->reset();
if ($delimiters = trim($delimiters)) { if ($delimiters = trim($delimiters)) {
@@ -107,7 +115,7 @@ class Mustache_Tokenizer
$this->flushBuffer(); $this->flushBuffer();
$this->state = self::IN_TAG_TYPE; $this->state = self::IN_TAG_TYPE;
} else { } else {
$char = substr($text, $i, 1); $char = $text[$i];
$this->buffer .= $char; $this->buffer .= $char;
if ($char == "\n") { if ($char == "\n") {
$this->flushBuffer(); $this->flushBuffer();
@@ -118,7 +126,7 @@ class Mustache_Tokenizer
case self::IN_TAG_TYPE: case self::IN_TAG_TYPE:
$i += $this->otagLen - 1; $i += $this->otagLen - 1;
$char = substr($text, $i + 1, 1); $char = $text[$i + 1];
if (isset(self::$tagTypes[$char])) { if (isset(self::$tagTypes[$char])) {
$tag = $char; $tag = $char;
$this->tagType = $tag; $this->tagType = $tag;
@@ -168,7 +176,7 @@ class Mustache_Tokenizer
} }
} }
} else { } else {
$this->buffer .= substr($text, $i, 1); $this->buffer .= $text[$i];
} }
break; break;
} }
@@ -176,6 +184,11 @@ class Mustache_Tokenizer
$this->flushBuffer(); $this->flushBuffer();
// Restore the user's encoding...
if ($encoding) {
mb_internal_encoding($encoding);
}
return $this->tokens; return $this->tokens;
} }