Make WordPress Core

#58290 closed enhancement (fixed)

Save a few processing cycles in `WP_Hook`

Reported by: bor0's profile bor0 Owned by: sergeybiryukov's profile SergeyBiryukov
Milestone: 6.3 Priority: normal
Severity: normal Version:
Component: Plugins Keywords: has-patch 2nd-opinion
Focuses: performance Cc:

Description

I found two low-hanging fruits that could improve the performance of hooks:

  1. Use count only when really needed (sometimes it may not be needed)
  2. is_object is redundant in is_object( $x ) && $x instance of X

Attachments (2)

58290.patch (1.3 KB) - added by bor0 13 months ago.
58290.2.patch (528 bytes) - added by bor0 13 months ago.

Download all attachments as: .zip

Change History (10)

@bor0
13 months ago

#1 @davidbaumwald
13 months ago

  • Component changed from General to Plugins
  • Focuses performance added

Thanks @bor0! I am adding the performance keyword so that this gets picked up during one of their scrubs.

#2 @bor0
13 months ago

Thanks @davidbaumwald.

Some more context for the second change, as the first change (count) seems straightforward.

In case we pass an object, the performance will be better as the call to is_object will be redundant. In the other case where we pass a non-object, the instanceof operator (a variant of is_a()) will first check if it is an object before doing any further processing. Therefore, no additional cycles should be wasted in both cases.

#3 @SergeyBiryukov
13 months ago

  • Milestone changed from Awaiting Review to 6.3

#4 @johnbillion
13 months ago

  • Keywords 2nd-opinion added
  • Milestone changed from 6.3 to Awaiting Review
  • Version trunk deleted

Thanks for the patch @bor0. Removing the is_object() call makes sense.

The change to the count( $args ) call could actually increase the number of times it gets counted. Currently the count happens exactly once for every call to apply_filters(). With this change in place the number of times the count happens is equal to the number of callbacks attached to the filter where accepted_args is greater than or equal to the number of arguments passed to the filter. Worth some more thought.

#5 @johnbillion
13 months ago

  • Milestone changed from Awaiting Review to 6.3

#6 @johnbillion
13 months ago

Correction: With this change in place the count actually happens once for every callback where accepted_args is not set to 0. Seems like therefore this will most often increase the number of counts that happen.

#7 @bor0
13 months ago

@johnbillion, great catch! What an oversight. I totally missed that the processing is happening within a loop for all callbacks.

@bor0
13 months ago

#8 @SergeyBiryukov
13 months ago

  • Owner set to SergeyBiryukov
  • Resolution set to fixed
  • Status changed from new to closed

In 55748:

Plugins: Remove is_object() check in WP_Hook:build_preinitialized_hooks().

This is a minor performance enhancement:

  • If an object is passed, the call to is_object() will be redundant.
  • If a non-object is passed, the instanceof operator (a variant of is_a()) will first check if it is an object before doing any further processing.

Therefore, no additional processing cycles should be wasted in both cases.

Follow-up to [38571].

Props bor0, johnbillion, davidbaumwald.
Fixes #58290.

Note: See TracTickets for help on using tickets.