From fdcd7a2e60d4e06fca5b09efd9eaa6af9ec04912 Mon Sep 17 00:00:00 2001 From: Sean Harvey Date: Wed, 7 Nov 2012 17:23:36 +1300 Subject: [PATCH] Fixing performance of DataObject::custom_database_fields() On sites with lots of modules, and pages with plenty of database queries, DataObject::custom_database_fields() can be called thousands of times, and slow down page render times. This fixes it so the fields are cached by class in a static variable, and are cleared when reset() is called on the DataObject. --- model/DataObject.php | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/model/DataObject.php b/model/DataObject.php index 114808240..29bb0a701 100644 --- a/model/DataObject.php +++ b/model/DataObject.php @@ -147,7 +147,8 @@ class DataObject extends ViewableData implements DataObjectInterface, i18nEntity public static $cache_has_own_table_field = array(); protected static $_cache_get_one; protected static $_cache_get_class_ancestry; - protected static $_cache_composite_fields = array(); + protected static $_cache_composite_fields = array(); + protected static $_cache_custom_database_fields = array(); protected static $_cache_field_labels = array(); /** @@ -226,8 +227,12 @@ class DataObject extends ViewableData implements DataObjectInterface, i18nEntity * @return array Map of fieldname to specification, similiar to {@link DataObject::$db}. */ public static function custom_database_fields($class) { + if(isset(self::$_cache_custom_database_fields[$class])) { + return self::$_cache_custom_database_fields[$class]; + } + $fields = Config::inst()->get($class, 'db', Config::UNINHERITED); - + foreach(self::composite_fields($class, false) as $fieldName => $fieldClass) { // Remove the original fieldname, it's not an actual database column unset($fields[$fieldName]); @@ -244,8 +249,12 @@ class DataObject extends ViewableData implements DataObjectInterface, i18nEntity if($hasOne) foreach(array_keys($hasOne) as $field) { $fields[$field . 'ID'] = 'ForeignKey'; } - - return (array)$fields; + + $output = (array) $fields; + + self::$_cache_custom_database_fields[$class] = $output; + + return $output; } /** @@ -2899,6 +2908,7 @@ class DataObject extends ViewableData implements DataObjectInterface, i18nEntity DataObject::$cache_has_own_table_field = array(); DataObject::$_cache_get_one = array(); DataObject::$_cache_composite_fields = array(); + DataObject::$_cache_custom_database_fields = array(); DataObject::$_cache_get_class_ancestry = array(); DataObject::$_cache_field_labels = array(); }