From f69c2b04953b9aceb35f9d0e74001169b4e48e59 Mon Sep 17 00:00:00 2001 From: Sean Harvey Date: Thu, 8 Nov 2012 10:38:16 +1300 Subject: [PATCH] Improve performance of DataObject::db() with caching In a usual CMS request, DataObject::db() is called potentially thousands of times, calling Config::get() constantly for the same uninherited statics, which is slow. This improves performance by caching those into DataObject::$_cache_db --- model/DataObject.php | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/model/DataObject.php b/model/DataObject.php index 29bb0a701..e3418aba8 100644 --- a/model/DataObject.php +++ b/model/DataObject.php @@ -145,6 +145,7 @@ class DataObject extends ViewableData implements DataObjectInterface, i18nEntity */ public static $cache_has_own_table = array(); public static $cache_has_own_table_field = array(); + protected static $_cache_db = array(); protected static $_cache_get_one; protected static $_cache_get_class_ancestry; protected static $_cache_composite_fields = array(); @@ -1578,23 +1579,28 @@ class DataObject extends ViewableData implements DataObjectInterface, i18nEntity continue; } + if(isset(self::$_cache_db[$class])) { + $dbItems = self::$_cache_db[$class]; + } else { + $dbItems = (array) Config::inst()->get($class, 'db', Config::UNINHERITED); + self::$_cache_db[$class] = $dbItems; + } + if($fieldName) { - $db = Config::inst()->get($class, 'db', Config::UNINHERITED); - - if(isset($db[$fieldName])) { - return $db[$fieldName]; + if(isset($dbItems[$fieldName])) { + return $dbItems[$fieldName]; } } else { - $newItems = (array)Config::inst()->get($class, 'db', Config::UNINHERITED); // Validate the data - foreach($newItems as $k => $v) { + foreach($dbItems as $k => $v) { if(!is_string($k) || is_numeric($k) || !is_string($v)) { user_error("$class::\$db has a bad entry: " . var_export($k,true). " => " . var_export($v,true) . ". Each map key should be a" . " property name, and the map value should be the property type.", E_USER_ERROR); } } - $items = isset($items) ? array_merge((array)$items, $newItems) : $newItems; + + $items = isset($items) ? array_merge((array) $items, $dbItems) : $dbItems; } } @@ -2906,6 +2912,7 @@ class DataObject extends ViewableData implements DataObjectInterface, i18nEntity public static function reset() { DataObject::$cache_has_own_table = array(); DataObject::$cache_has_own_table_field = array(); + DataObject::$_cache_db = array(); DataObject::$_cache_get_one = array(); DataObject::$_cache_composite_fields = array(); DataObject::$_cache_custom_database_fields = array();