Make WordPress Core

Opened 11 years ago

Closed 10 years ago

#24431 closed enhancement (wontfix)

Boolean shorthand for add_filter

Reported by: ryanve's profile ryanve Owned by:
Milestone: Priority: normal
Severity: normal Version:
Component: Plugins Keywords:
Focuses: performance Cc:


Callbacks like __return_false or __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 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)

Change History (3)

#1 @toscho
11 years ago

  • Cc info@… added

How would priorities be handled?

#2 @ryanve
11 years ago

@toscho Priorities could work in one of two ways. I'm open to either. 1 is simpler. 2 is more capable:

  1. Subsequent (later priority) callbacks run as normal. false would work just like __return_false does now.
  2. Booleans provide a way to break from the loop. Subsequent (later priority) callbacks would not fire.

#3 @nacin
10 years ago

  • Component changed from Performance to Plugins
  • Focuses performance added
  • Milestone Awaiting Review deleted
  • Resolution set to wontfix
  • Status changed from new to closed

I don't think this is needed for now.

Note: See TracTickets for help on using tickets.