Opened 17 months ago
Closed 17 months ago
#58290 closed enhancement (fixed)
Save a few processing cycles in `WP_Hook`
Reported by: | bor0 | Owned by: | 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:
- Use
count
only when really needed (sometimes it may not be needed) is_object
is redundant inis_object( $x ) && $x instance of X
Attachments (2)
Change History (10)
#2
@
17 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.
#4
@
17 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.
#6
@
17 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.
Thanks @bor0! I am adding the
performance
keyword so that this gets picked up during one of their scrubs.