From cb57c3912aef6bed394a4693f94e9c97001aff34 Mon Sep 17 00:00:00 2001 From: Andy Staudacher Date: Sun, 21 Feb 2010 13:05:48 -0800 Subject: Fix random queries (such as for the random image block) for locales that don't use a period as decimal separator of floating point numbers (such as de_DE). (Integrating a fix Kohana's SQL generation for floating point numbers. See http://dev.kohanaframework.org/issues/2636.) --- system/libraries/Database.php | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'system') diff --git a/system/libraries/Database.php b/system/libraries/Database.php index 98e33fa0..253bb152 100644 --- a/system/libraries/Database.php +++ b/system/libraries/Database.php @@ -420,6 +420,11 @@ abstract class Database_Core { { return (string) $value; } + elseif (is_float($value)) + { + // Convert to non-locale aware float to prevent possible commas + return sprintf('%F', $value); + } return '\''.$this->escape($value).'\''; } -- cgit v1.2.3 From 5ddd7c9677b644396981de7df8176a3b168ffe21 Mon Sep 17 00:00:00 2001 From: Andy Staudacher Date: Sun, 21 Feb 2010 20:04:06 -0800 Subject: Fix Kohana's internal cache for Gallery's usage pattern. Adds a core.internal_cache_read_only config variable to Kohana's internals. Kohana's internal_cache for find_file wasn't working in Gallery because the cache would be emptied on each request after reading it from disk and before most lookups would run. 1. Bootstrap sets initial core.modules (= include path): forge, kohana23_compat, gallery. 2. Kohana::setup() loads find_file cache from disk. 3. Gallery loads list of active modules and themes, and updates the core.modules value (=include path), which forces the internal find_file cache to be empties (which makes sense). 4. Request processing starts, and thus 80% of all Kohana::find_file() triggered is_file() invocations start off with an empty find_file cache. In the case of my small Gallery installation, we're talking about 3100 is_file() invocations per request with or without internal_cache enabled. With this fix, this number is down to 800 invocations. The basic idea is that we treat the cache as read only and don't write any (possibly dirty) values to it in memory until we're sure that the include path won't change later on in the request processing. Once we know the list of active modules and themes, we can update core.modules and finally flip the read-only state of the cache and start writing to it. --- application/config/config.php | 6 ++++ modules/gallery/helpers/gallery_event.php | 1 + system/core/Kohana.php | 47 ++++++++++++++++++++----------- 3 files changed, 37 insertions(+), 17 deletions(-) (limited to 'system') diff --git a/application/config/config.php b/application/config/config.php index aecc400c..1c5bccb7 100644 --- a/application/config/config.php +++ b/application/config/config.php @@ -67,6 +67,12 @@ $config["url_suffix"] = ""; * can give significant speed improvements at the expense of delayed updating. */ $config["internal_cache"] = FALSE; +/** + * Enable or disable writing to the internal cache. Used by Gallery to treat + * the cache as read-only until all active modules and themes are in the + * include path. + */ +$config["internal_cache_read_only"] = TRUE; $config["internal_cache_path"] = VARPATH . "tmp/"; /** diff --git a/modules/gallery/helpers/gallery_event.php b/modules/gallery/helpers/gallery_event.php index 36f91142..a6783bc6 100644 --- a/modules/gallery/helpers/gallery_event.php +++ b/modules/gallery/helpers/gallery_event.php @@ -25,6 +25,7 @@ class gallery_event_Core { static function gallery_ready() { identity::load_user(); theme::load_themes(); + Kohana_Config::instance()->set('core.internal_cache_read_only', false); locales::set_request_locale(); } diff --git a/system/core/Kohana.php b/system/core/Kohana.php index ae056d0e..1ed6d725 100644 --- a/system/core/Kohana.php +++ b/system/core/Kohana.php @@ -365,13 +365,17 @@ abstract class Kohana_Core { // Add SYSPATH as the last path Kohana::$include_paths[] = SYSPATH; - // Clear cached include paths - self::$internal_cache['find_file_paths'] = array(); - if ( ! isset(self::$write_cache['find_file_paths'])) + if ( ! Kohana::config('core.internal_cache_read_only')) { - // Write cache at shutdown - self::$write_cache['find_file_paths'] = TRUE; - } + // Clear cached include paths + self::$internal_cache['find_file_paths'] = array(); + + if ( ! isset(self::$write_cache['find_file_paths'])) + { + // Write cache at shutdown + self::$write_cache['find_file_paths'] = TRUE; + } + } } @@ -818,13 +822,17 @@ abstract class Kohana_Core { } } - if ( ! isset(Kohana::$write_cache['find_file_paths'])) + if ( ! Kohana::config('core.internal_cache_read_only')) { - // Write cache at shutdown - Kohana::$write_cache['find_file_paths'] = TRUE; - } + Kohana::$internal_cache['find_file_paths'][$search] = $found; - return Kohana::$internal_cache['find_file_paths'][$search] = $found; + if ( ! isset(Kohana::$write_cache['find_file_paths'])) + { + // Write cache at shutdown + Kohana::$write_cache['find_file_paths'] = TRUE; + } + } + return $found; } /** @@ -900,8 +908,7 @@ abstract class Kohana_Core { public static function message($key, $args = array()) { // Extract the main group from the key - $group = explode('.', $key, 2); - $group = $group[0]; + list ($group, $subkey) = explode('.', $key, 2); if ( ! isset(Kohana::$internal_cache['messages'][$group])) { @@ -913,17 +920,23 @@ abstract class Kohana_Core { include $file[0]; } - if ( ! isset(Kohana::$write_cache['messages'])) + if ( ! isset(Kohana::$write_cache['messages']) && ! Kohana::config('core.internal_cache_read_only')) { // Write language cache Kohana::$write_cache['messages'] = TRUE; } - - Kohana::$internal_cache['messages'][$group] = $messages; + if ( ! Kohana::config('core.internal_cache_read_only')) + { + Kohana::$internal_cache['messages'][$group] = $messages; + } + } + else + { + $messages = Kohana::$internal_cache['messages'][$group]; } // Get the line from cache - $line = Kohana::key_string(Kohana::$internal_cache['messages'], $key); + $line = Kohana::key_string($messages, $subkey); if ($line === NULL) { -- cgit v1.2.3 From 6cbe0f78aa80a2810908a76ca163f1dfff2f0726 Mon Sep 17 00:00:00 2001 From: Andy Staudacher Date: Sun, 21 Feb 2010 21:07:55 -0800 Subject: Revert "Fix Kohana's internal cache for Gallery's usage pattern." This reverts commit 5ddd7c9677b644396981de7df8176a3b168ffe21. --- application/config/config.php | 6 ---- modules/gallery/helpers/gallery_event.php | 1 - system/core/Kohana.php | 47 +++++++++++-------------------- 3 files changed, 17 insertions(+), 37 deletions(-) (limited to 'system') diff --git a/application/config/config.php b/application/config/config.php index 1c5bccb7..aecc400c 100644 --- a/application/config/config.php +++ b/application/config/config.php @@ -67,12 +67,6 @@ $config["url_suffix"] = ""; * can give significant speed improvements at the expense of delayed updating. */ $config["internal_cache"] = FALSE; -/** - * Enable or disable writing to the internal cache. Used by Gallery to treat - * the cache as read-only until all active modules and themes are in the - * include path. - */ -$config["internal_cache_read_only"] = TRUE; $config["internal_cache_path"] = VARPATH . "tmp/"; /** diff --git a/modules/gallery/helpers/gallery_event.php b/modules/gallery/helpers/gallery_event.php index a6783bc6..36f91142 100644 --- a/modules/gallery/helpers/gallery_event.php +++ b/modules/gallery/helpers/gallery_event.php @@ -25,7 +25,6 @@ class gallery_event_Core { static function gallery_ready() { identity::load_user(); theme::load_themes(); - Kohana_Config::instance()->set('core.internal_cache_read_only', false); locales::set_request_locale(); } diff --git a/system/core/Kohana.php b/system/core/Kohana.php index 1ed6d725..ae056d0e 100644 --- a/system/core/Kohana.php +++ b/system/core/Kohana.php @@ -365,17 +365,13 @@ abstract class Kohana_Core { // Add SYSPATH as the last path Kohana::$include_paths[] = SYSPATH; - if ( ! Kohana::config('core.internal_cache_read_only')) + // Clear cached include paths + self::$internal_cache['find_file_paths'] = array(); + if ( ! isset(self::$write_cache['find_file_paths'])) { - // Clear cached include paths - self::$internal_cache['find_file_paths'] = array(); - - if ( ! isset(self::$write_cache['find_file_paths'])) - { - // Write cache at shutdown - self::$write_cache['find_file_paths'] = TRUE; - } - } + // Write cache at shutdown + self::$write_cache['find_file_paths'] = TRUE; + } } @@ -822,17 +818,13 @@ abstract class Kohana_Core { } } - if ( ! Kohana::config('core.internal_cache_read_only')) + if ( ! isset(Kohana::$write_cache['find_file_paths'])) { - Kohana::$internal_cache['find_file_paths'][$search] = $found; - - if ( ! isset(Kohana::$write_cache['find_file_paths'])) - { - // Write cache at shutdown - Kohana::$write_cache['find_file_paths'] = TRUE; - } + // Write cache at shutdown + Kohana::$write_cache['find_file_paths'] = TRUE; } - return $found; + + return Kohana::$internal_cache['find_file_paths'][$search] = $found; } /** @@ -908,7 +900,8 @@ abstract class Kohana_Core { public static function message($key, $args = array()) { // Extract the main group from the key - list ($group, $subkey) = explode('.', $key, 2); + $group = explode('.', $key, 2); + $group = $group[0]; if ( ! isset(Kohana::$internal_cache['messages'][$group])) { @@ -920,23 +913,17 @@ abstract class Kohana_Core { include $file[0]; } - if ( ! isset(Kohana::$write_cache['messages']) && ! Kohana::config('core.internal_cache_read_only')) + if ( ! isset(Kohana::$write_cache['messages'])) { // Write language cache Kohana::$write_cache['messages'] = TRUE; } - if ( ! Kohana::config('core.internal_cache_read_only')) - { - Kohana::$internal_cache['messages'][$group] = $messages; - } - } - else - { - $messages = Kohana::$internal_cache['messages'][$group]; + + Kohana::$internal_cache['messages'][$group] = $messages; } // Get the line from cache - $line = Kohana::key_string($messages, $subkey); + $line = Kohana::key_string(Kohana::$internal_cache['messages'], $key); if ($line === NULL) { -- cgit v1.2.3 From 336c3bd264b4af2ab74fe1262366ad6f2e705451 Mon Sep 17 00:00:00 2001 From: Andy Staudacher Date: Sun, 21 Feb 2010 21:29:24 -0800 Subject: Fix Kohana's internal cache for Gallery's usage pattern. Instead of deleting the whole find_files cache when ever include_paths (=core.modules) change, keep a separate find_files cache for each set of include_paths. Benefits for Gallery: - There are about 3000 is_file() invocations for a photo / album page in a vanilla Gallery installation. These are mostly triggered by Kohana::find_file(). - Enabling internal_cache doesn't help at all (see explanation below). The number of is_file() invocations is about the same with or without this cache. - With this patch, more than 95% of these invocations are gone. The cache works as intended. Kohana's internal_cache for find_file wasn't working in Gallery because the cache would be emptied on each request after reading it from disk and before most lookups would run. 1. Bootstrap sets initial core.modules (= include path): forge, kohana23_compat, gallery. 2. Kohana::setup() loads find_file cache from disk. 3. Gallery loads list of active modules and themes, and updates the core.modules value (=include path), which forces the internal find_file cache to be empties (which makes sense). 4. Request processing starts, and thus 80+% of all Kohana::find_file() triggered is_file() invocations start off with an empty find_file cache. The patch doesn't have a significant impact on performance for Kohana applications which don't change their include paths at runtime (after Kohana::setup). And the patch should benefit all Kohana applications which have modules / extensions, i.e. which first need to bootstrap Kohana before they can load a list of all active modules from the database. --- system/core/Kohana.php | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) (limited to 'system') diff --git a/system/core/Kohana.php b/system/core/Kohana.php index ae056d0e..f7f6b326 100644 --- a/system/core/Kohana.php +++ b/system/core/Kohana.php @@ -32,6 +32,7 @@ abstract class Kohana_Core { // Include paths protected static $include_paths; + protected static $include_paths_hash = ''; // Cache lifetime protected static $cache_lifetime; @@ -365,14 +366,7 @@ abstract class Kohana_Core { // Add SYSPATH as the last path Kohana::$include_paths[] = SYSPATH; - // Clear cached include paths - self::$internal_cache['find_file_paths'] = array(); - if ( ! isset(self::$write_cache['find_file_paths'])) - { - // Write cache at shutdown - self::$write_cache['find_file_paths'] = TRUE; - } - + Kohana::$include_paths_hash = md5(serialize(Kohana::$include_paths)); } return Kohana::$include_paths; @@ -766,8 +760,8 @@ abstract class Kohana_Core { // Search path $search = $directory.'/'.$filename.$ext; - if (isset(Kohana::$internal_cache['find_file_paths'][$search])) - return Kohana::$internal_cache['find_file_paths'][$search]; + if (isset(Kohana::$internal_cache['find_file_paths'][Kohana::$include_paths_hash][$search])) + return Kohana::$internal_cache['find_file_paths'][Kohana::$include_paths_hash][$search]; // Load include paths $paths = Kohana::$include_paths; @@ -824,7 +818,7 @@ abstract class Kohana_Core { Kohana::$write_cache['find_file_paths'] = TRUE; } - return Kohana::$internal_cache['find_file_paths'][$search] = $found; + return Kohana::$internal_cache['find_file_paths'][Kohana::$include_paths_hash][$search] = $found; } /** -- cgit v1.2.3 From ff9129c59850277c3079edd19b8a0f7ac4d70056 Mon Sep 17 00:00:00 2001 From: Andy Staudacher Date: Sun, 21 Feb 2010 23:57:29 -0800 Subject: Revert "Fix Kohana's internal cache for Gallery's usage pattern." Waiting for feedback from Kohana devevelopers before applying the patch to Gallery 3. This reverts commit 336c3bd264b4af2ab74fe1262366ad6f2e705451. --- system/core/Kohana.php | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) (limited to 'system') diff --git a/system/core/Kohana.php b/system/core/Kohana.php index f7f6b326..ae056d0e 100644 --- a/system/core/Kohana.php +++ b/system/core/Kohana.php @@ -32,7 +32,6 @@ abstract class Kohana_Core { // Include paths protected static $include_paths; - protected static $include_paths_hash = ''; // Cache lifetime protected static $cache_lifetime; @@ -366,7 +365,14 @@ abstract class Kohana_Core { // Add SYSPATH as the last path Kohana::$include_paths[] = SYSPATH; - Kohana::$include_paths_hash = md5(serialize(Kohana::$include_paths)); + // Clear cached include paths + self::$internal_cache['find_file_paths'] = array(); + if ( ! isset(self::$write_cache['find_file_paths'])) + { + // Write cache at shutdown + self::$write_cache['find_file_paths'] = TRUE; + } + } return Kohana::$include_paths; @@ -760,8 +766,8 @@ abstract class Kohana_Core { // Search path $search = $directory.'/'.$filename.$ext; - if (isset(Kohana::$internal_cache['find_file_paths'][Kohana::$include_paths_hash][$search])) - return Kohana::$internal_cache['find_file_paths'][Kohana::$include_paths_hash][$search]; + if (isset(Kohana::$internal_cache['find_file_paths'][$search])) + return Kohana::$internal_cache['find_file_paths'][$search]; // Load include paths $paths = Kohana::$include_paths; @@ -818,7 +824,7 @@ abstract class Kohana_Core { Kohana::$write_cache['find_file_paths'] = TRUE; } - return Kohana::$internal_cache['find_file_paths'][Kohana::$include_paths_hash][$search] = $found; + return Kohana::$internal_cache['find_file_paths'][$search] = $found; } /** -- cgit v1.2.3