[wp-trac] [WordPress Trac] #53218: Make apply_filters() variadic

WordPress Trac noreply at wordpress.org
Thu Sep 16 22:49:10 UTC 2021


#53218: Make apply_filters() variadic
-------------------------+-----------------------------
 Reporter:  johnbillion  |       Owner:  (none)
     Type:  enhancement  |      Status:  new
 Priority:  normal       |   Milestone:  Future Release
Component:  Plugins      |     Version:  5.3
 Severity:  normal       |  Resolution:
 Keywords:  has-patch    |     Focuses:
-------------------------+-----------------------------

Comment (by johnbillion):

 In [https://github.com/WordPress/wordpress-develop/pull/1259 PR 1259] I
 decided to revisit this with a view to removing as much of the args array
 processing in `apply_filters()` as possible in order to introduce the
 variadic `$args` parameter without negatively affecting performance.

 As Juliette noted above, using `array_merge()` in place of
 `func_get_args()` and `array_shift()` results in a slight reduction in
 performance. What we can do instead is move the `func_get_args()` call
 inside the `all` hook handling block so it's not called when `all` isn't
 in use, switch to a variadic `$args`, and replace `array_pop( $args )`
 with `array_unshift()` to push the `$value` variable onto the front of
 `$args` before it's passed to `WP_Hook::apply_filters()`. This results in
 no performance change down to the microsecond.

 The `all` hook handling now uses the same handling as in `do_action()`.
 `func_get_args()` is more performant than `array_unshift( $args,
 $hook_name )`.

 What's the overall gain of this change? A more correct signature for
 `apply_filters()` with no negative performance impact.

 == Benchmarking

 The standard deviations of the benchmarks for this change are quite high
 because the processing time is so low. With 100,000 iterations the change
 averages to +/- less than 1/10th of a microsecond.

 Benchmark results can be seen in the action here:
 https://github.com/WordPress/wordpress-develop/actions/runs/1243401754

 Example:

 || old with callback    || 2.240μs ||
 || new with callback    || 2.230μs ||
 || old without callback || 0.190μs ||
 || new without callback || 0.182μs ||

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/53218#comment:7>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list