From fde70e7623ee9936f6d2e3cdcb35f93805cc5d08 Mon Sep 17 00:00:00 2001 From: matt Date: Mon, 2 Apr 2007 07:03:38 +0000 Subject: [PATCH] Fixes bottlenecks found in filter system using xdebug + kcachegrind. merge_filters was being called 1000+ times. git-svn-id: http://svn.automattic.com/wordpress/trunk@5169 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/plugin.php | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/wp-includes/plugin.php b/wp-includes/plugin.php index ead9710506..cebc9f128f 100644 --- a/wp-includes/plugin.php +++ b/wp-includes/plugin.php @@ -16,10 +16,11 @@ * @return boolean true if the $function_to_add is added succesfully to filter $tag. How many arguments your function takes. In WordPress 1.5.1+, hooked functions can take extra arguments that are set when the matching do_action() or apply_filters() call is run. For example, the action comment_id_not_found will pass any functions that hook onto it the ID of the requested comment. */ function add_filter($tag, $function_to_add, $priority = 10, $accepted_args = 1) { - global $wp_filter; + global $wp_filter, $merged_filters; // So the format is wp_filter['tag']['array of priorities']['array of functions serialized']['array of ['array (functions, accepted_args)]'] $wp_filter[$tag][$priority][serialize($function_to_add)] = array('function' => $function_to_add, 'accepted_args' => $accepted_args); + unset( $merged_filters[ $tag ] ); return true; } @@ -37,9 +38,10 @@ function add_filter($tag, $function_to_add, $priority = 10, $accepted_args = 1) * @return string The text in $string after all hooked functions are applied to it. */ function apply_filters($tag, $string) { - global $wp_filter; + global $wp_filter, $merged_filters; - merge_filters($tag); + if ( !isset( $merged_filters[ $tag ] ) ) + merge_filters($tag); if ( !isset($wp_filter[$tag]) ) return $string; @@ -68,15 +70,16 @@ function apply_filters($tag, $string) { * @param string $tag The filter hook of which the functions should be merged. */ function merge_filters($tag) { - global $wp_filter; + global $wp_filter, $merged_filters; - if ( isset($wp_filter['all']) ) + if ( is_array($wp_filter['all']) ) $wp_filter[$tag] = array_merge($wp_filter['all'], (array) $wp_filter[$tag]); if ( isset($wp_filter[$tag]) ){ reset($wp_filter[$tag]); uksort($wp_filter[$tag], "strnatcasecmp"); } + $merged_filters[ $tag ] = true; } /** @@ -92,9 +95,10 @@ function merge_filters($tag) { * @return boolean Whether the function is removed. */ function remove_filter($tag, $function_to_remove, $priority = 10, $accepted_args = 1) { - global $wp_filter; + global $wp_filter, $merged_filters; unset($GLOBALS['wp_filter'][$tag][$priority][serialize($function_to_remove)]); + unset( $merged_filters[ $tag ] ); return true; }