From 3a2e904098a05ab06671be595c6d9afd854fb488 Mon Sep 17 00:00:00 2001 From: sam marshall Date: Thu, 2 May 2013 15:28:17 +0100 Subject: [PATCH] MDL-39472 MUC: Optimise cache::get_from_persist_cache --- cache/classes/helper.php | 5 +++++ cache/classes/loaders.php | 15 +++++++++++---- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/cache/classes/helper.php b/cache/classes/helper.php index 873dbbe6279..6f30b4b2bf9 100644 --- a/cache/classes/helper.php +++ b/cache/classes/helper.php @@ -350,6 +350,11 @@ class cache_helper { * @param string $definition */ protected static function ensure_ready_for_stats($store, $definition) { + // This function is performance-sensitive, so exit as quickly as possible + // if we do not need to do anything. + if (isset(self::$stats[$definition][$store])) { + return; + } if (!array_key_exists($definition, self::$stats)) { self::$stats[$definition] = array( $store => array( diff --git a/cache/classes/loaders.php b/cache/classes/loaders.php index 9f7dc79bcca..6f4e680e29a 100644 --- a/cache/classes/loaders.php +++ b/cache/classes/loaders.php @@ -912,7 +912,8 @@ class cache implements cache_loader { * @return bool */ protected function is_in_persist_cache($key) { - if (is_array($key)) { + // This method of checking if an array was supplied is faster than is_array. + if ($key === (array)$key) { $key = $key['key']; } // This could be written as a single line, however it has been split because the ttl check is faster than the instanceof @@ -933,10 +934,15 @@ class cache implements cache_loader { * @return mixed|false The data from the persist cache or false if it wasn't there. */ protected function get_from_persist_cache($key) { - if (is_array($key)) { + // This method of checking if an array was supplied is faster than is_array. + if ($key === (array)$key) { $key = $key['key']; } - if (!$this->persist || !array_key_exists($key, $this->persistcache)) { + // This isset check is faster than array_key_exists but will return false + // for null values, meaning null values will come from backing store not + // the persist cache. We think this okay because null usage should be + // very rare (see comment in MDL-39472). + if (!$this->persist || !isset($this->persistcache[$key])) { $result = false; } else { $data = $this->persistcache[$key]; @@ -976,7 +982,8 @@ class cache implements cache_loader { * @return bool */ protected function set_in_persist_cache($key, $data) { - if (is_array($key)) { + // This method of checking if an array was supplied is faster than is_array. + if ($key === (array)$key) { $key = $key['key']; } $this->persistcache[$key] = $data;