[wp-trac] [WordPress Trac] #24431: Boolean shorthand for add_filter

WordPress Trac noreply at wordpress.org
Mon May 27 05:20:02 UTC 2013


#24431: Boolean shorthand for add_filter
-------------------------+-----------------------------
 Reporter:  ryanve       |      Owner:
     Type:  enhancement  |     Status:  new
 Priority:  normal       |  Milestone:  Awaiting Review
Component:  Performance  |    Version:
 Severity:  normal       |   Keywords:
-------------------------+-----------------------------
 Callbacks like [http://core.trac.wordpress.org/browser/tags/3.5.1/wp-
 includes/functions.php#L3562 __return_false] or
 [http://core.trac.wordpress.org/browser/tags/3.5.1/wp-
 includes/functions.php#L3549 __return_true] are often passed to
 `add_filter` to negate a filter or flip an option. This creates uneeded
 function calls. A potential performance-saver is to enable `add_filter` to
 support bools being passed as callbacks. `add_filter` doesn't validate the
 callback anyway, so no change would be needed there. A check could be done
 as [http://core.trac.wordpress.org/browser/tags/3.5.1/wp-
 includes/plugin.php#L169 apply_filters] runs:

 {{{
 $value = is_bool($the_['function']) ? $the_['function'] :
 call_user_func_array(
     $the_['function'], array_slice($args, 1, (int) $the_['accepted_args'])
 );
 }}}

 3quals checks like `false === $fn || true === $fn` could be slightly
 faster. My hypothesis is that greater savings would be made from avoiding
 calls to `array_slice`, `call_user_func_array`, and the callback itself. A
 minor secondary benefit is that storing bools in the hash rather than
 strings like `__return_false` should reduce memory consumption.

 It of course would also add syntactic sugar like:
 {{{
 add_filter('use_default_gallery_style', false)
 }}}

--
Ticket URL: <http://core.trac.wordpress.org/ticket/24431>
WordPress Trac <http://core.trac.wordpress.org/>
WordPress blogging software


More information about the wp-trac mailing list