mirror of
https://github.com/twigphp/Twig.git
synced 2026-09-17 12:57:05 +00:00
merged branch Seldaek/optim (PR #1077)
This PR was merged into the master branch. Discussion ---------- Some more optimizations Please look at commits one by one. Overall this makes the twig_escape_filter almost 20% faster here for happy path (escaping string for html with UTF-8) on 1000 calls. Still pretty darn slow, but faster. Commits -------d886b12Make happy path (strings) faster by avoiding the cast, same perfs for the other cases97bf5e5Avoid unnecessary is_object callc1c0dccUse uppercased charsets consistently (as in js/css/html_attr strategies)a23fa62Add UTF-8 in uppercase since it is so commonc891c53Avoid strtolower call at every twig_escape_filter call61f2b2aUse plain string comparison which is twice as fastd4eec01Move html case up since it is the most likely to be hit
This commit is contained in:
@@ -53,7 +53,7 @@ class Twig_Environment
|
||||
* * debug: When set to true, it automatically set "auto_reload" to true as
|
||||
* well (default to false).
|
||||
*
|
||||
* * charset: The charset used by the templates (default to utf-8).
|
||||
* * charset: The charset used by the templates (default to UTF-8).
|
||||
*
|
||||
* * base_template_class: The base template class to use for generated
|
||||
* templates (default to Twig_Template).
|
||||
@@ -99,7 +99,7 @@ class Twig_Environment
|
||||
), $options);
|
||||
|
||||
$this->debug = (bool) $options['debug'];
|
||||
$this->charset = $options['charset'];
|
||||
$this->charset = strtoupper($options['charset']);
|
||||
$this->baseTemplateClass = $options['base_template_class'];
|
||||
$this->autoReload = null === $options['auto_reload'] ? $this->debug : (bool) $options['auto_reload'];
|
||||
$this->strictVariables = (bool) $options['strict_variables'];
|
||||
@@ -566,7 +566,7 @@ class Twig_Environment
|
||||
*/
|
||||
public function setCharset($charset)
|
||||
{
|
||||
$this->charset = $charset;
|
||||
$this->charset = strtoupper($charset);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
+49
-38
@@ -847,21 +847,62 @@ function twig_in_filter($value, $compare)
|
||||
*/
|
||||
function twig_escape_filter(Twig_Environment $env, $string, $strategy = 'html', $charset = null, $autoescape = false)
|
||||
{
|
||||
if ($autoescape && is_object($string) && $string instanceof Twig_Markup) {
|
||||
if ($autoescape && $string instanceof Twig_Markup) {
|
||||
return $string;
|
||||
}
|
||||
|
||||
if (!is_string($string) && !(is_object($string) && method_exists($string, '__toString'))) {
|
||||
return $string;
|
||||
if (!is_string($string)) {
|
||||
if (is_object($string) && method_exists($string, '__toString')) {
|
||||
$string = (string) $string;
|
||||
} else {
|
||||
return $string;
|
||||
}
|
||||
}
|
||||
|
||||
if (null === $charset) {
|
||||
$charset = $env->getCharset();
|
||||
}
|
||||
|
||||
$string = (string) $string;
|
||||
|
||||
switch ($strategy) {
|
||||
case 'html':
|
||||
// see http://php.net/htmlspecialchars
|
||||
|
||||
// Using a static variable to avoid initializing the array
|
||||
// each time the function is called. Moving the declaration on the
|
||||
// top of the function slow downs other escaping strategies.
|
||||
static $htmlspecialcharsCharsets = array(
|
||||
'ISO-8859-1' => true, 'ISO8859-1' => true,
|
||||
'ISO-8859-15' => true, 'ISO8859-15' => true,
|
||||
'utf-8' => true, 'UTF-8' => true,
|
||||
'CP866' => true, 'IBM866' => true, '866' => true,
|
||||
'CP1251' => true, 'WINDOWS-1251' => true, 'WIN-1251' => true,
|
||||
'1251' => true,
|
||||
'CP1252' => true, 'WINDOWS-1252' => true, '1252' => true,
|
||||
'KOI8-R' => true, 'KOI8-RU' => true, 'KOI8R' => true,
|
||||
'BIG5' => true, '950' => true,
|
||||
'GB2312' => true, '936' => true,
|
||||
'BIG5-HKSCS' => true,
|
||||
'SHIFT_JIS' => true, 'SJIS' => true, '932' => true,
|
||||
'EUC-JP' => true, 'EUCJP' => true,
|
||||
'ISO8859-5' => true, 'ISO-8859-5' => true, 'MACROMAN' => true,
|
||||
);
|
||||
|
||||
if (isset($htmlspecialcharsCharsets[$charset])) {
|
||||
return htmlspecialchars($string, ENT_QUOTES | ENT_SUBSTITUTE, $charset);
|
||||
}
|
||||
|
||||
if (isset($htmlspecialcharsCharsets[strtoupper($charset)])) {
|
||||
// cache the lowercase variant for future iterations
|
||||
$htmlspecialcharsCharsets[$charset] = true;
|
||||
|
||||
return htmlspecialchars($string, ENT_QUOTES | ENT_SUBSTITUTE, $charset);
|
||||
}
|
||||
|
||||
$string = twig_convert_encoding($string, 'UTF-8', $charset);
|
||||
$string = htmlspecialchars($string, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8');
|
||||
|
||||
return twig_convert_encoding($string, $charset, 'UTF-8');
|
||||
|
||||
case 'js':
|
||||
// escape all non-alphanumeric characters
|
||||
// into their \xHH or \uHHHH representations
|
||||
@@ -915,40 +956,10 @@ function twig_escape_filter(Twig_Environment $env, $string, $strategy = 'html',
|
||||
|
||||
return $string;
|
||||
|
||||
case 'html':
|
||||
// see http://php.net/htmlspecialchars
|
||||
|
||||
// Using a static variable to avoid initializing the array
|
||||
// each time the function is called. Moving the declaration on the
|
||||
// top of the function slow downs other escaping strategies.
|
||||
static $htmlspecialcharsCharsets = array(
|
||||
'iso-8859-1' => true, 'iso8859-1' => true,
|
||||
'iso-8859-15' => true, 'iso8859-15' => true,
|
||||
'utf-8' => true,
|
||||
'cp866' => true, 'ibm866' => true, '866' => true,
|
||||
'cp1251' => true, 'windows-1251' => true, 'win-1251' => true,
|
||||
'1251' => true,
|
||||
'cp1252' => true, 'windows-1252' => true, '1252' => true,
|
||||
'koi8-r' => true, 'koi8-ru' => true, 'koi8r' => true,
|
||||
'big5' => true, '950' => true,
|
||||
'gb2312' => true, '936' => true,
|
||||
'big5-hkscs' => true,
|
||||
'shift_jis' => true, 'sjis' => true, '932' => true,
|
||||
'euc-jp' => true, 'eucjp' => true,
|
||||
'iso8859-5' => true, 'iso-8859-5' => true, 'macroman' => true,
|
||||
);
|
||||
|
||||
if (isset($htmlspecialcharsCharsets[strtolower($charset)])) {
|
||||
return htmlspecialchars($string, ENT_QUOTES | ENT_SUBSTITUTE, $charset);
|
||||
}
|
||||
|
||||
$string = twig_convert_encoding($string, 'UTF-8', $charset);
|
||||
$string = htmlspecialchars($string, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8');
|
||||
|
||||
return twig_convert_encoding($string, $charset, 'UTF-8');
|
||||
|
||||
case 'url':
|
||||
if (version_compare(PHP_VERSION, '5.3.0', '<')) {
|
||||
// hackish test to avoid version_compare that is much slower, this works unless PHP releases a 5.10.*
|
||||
// at that point however PHP 5.2.* support can be removed
|
||||
if (PHP_VERSION < '5.3.0') {
|
||||
return str_replace('%7E', '~', rawurlencode($string));
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user