From 6e44b4080a6356fbf7c27e0d5e7acfcad5ebf577 Mon Sep 17 00:00:00 2001 From: thybag Date: Tue, 4 Sep 2012 16:09:56 +0100 Subject: [PATCH] Cache application.encoding within HTML class to avoid unnecessary calls to Config::get(); Calling the "Config::get('application.encoding')" is expensive and within a large form (using the form builder) having it requested multiple times can result in a significant performance drag. Caching this value reduced calls to Config:get within our project from 1200+ to 125. All core tests appear to pass with this change in place. --- laravel/html.php | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/laravel/html.php b/laravel/html.php index fd5bf82e..71a89080 100644 --- a/laravel/html.php +++ b/laravel/html.php @@ -9,6 +9,13 @@ class HTML { */ public static $macros = array(); + /** + * Cache application encoding locally to save expensive calls to config::get(). + * + * @var string + */ + public static $encoding = null; + /** * Registers a custom macro. * @@ -31,7 +38,8 @@ public static function macro($name, $macro) */ public static function entities($value) { - return htmlentities($value, ENT_QUOTES, Config::get('application.encoding'), false); + if(static::$encoding===null) static::$encoding = Config::get('application.encoding'); + return htmlentities($value, ENT_QUOTES, static::$encoding, false); } /** @@ -42,7 +50,8 @@ public static function entities($value) */ public static function decode($value) { - return html_entity_decode($value, ENT_QUOTES, Config::get('application.encoding')); + if(static::$encoding===null) static::$encoding = Config::get('application.encoding'); + return html_entity_decode($value, ENT_QUOTES, static::$encoding); } /** @@ -55,7 +64,8 @@ public static function decode($value) */ public static function specialchars($value) { - return htmlspecialchars($value, ENT_QUOTES, Config::get('application.encoding'), false); + if(static::$encoding===null) static::$encoding = Config::get('application.encoding'); + return htmlspecialchars($value, ENT_QUOTES, static::$encoding, false); } /**