[wp-trac] [WordPress Trac] #58291: Speed up WordPress hooks addition/removal by ~20%

WordPress Trac noreply at wordpress.org
Thu May 11 01:02:21 UTC 2023


#58291: Speed up WordPress hooks addition/removal by ~20%
--------------------------+-----------------------------
 Reporter:  bor0          |      Owner:  (none)
     Type:  defect (bug)  |     Status:  new
 Priority:  normal        |  Milestone:  Awaiting Review
Component:  General       |    Version:  trunk
 Severity:  normal        |   Keywords:  has-patch
  Focuses:  performance   |
--------------------------+-----------------------------
 I present a patch where `add_filter`/`remove_filter`/`has_filter` are sped
 up by ~20%.

 Here's what's changed in the proposed patch:
 - Calling a static method than a function is faster - check `static-
 faster.php` for proof
 - Removed unused arguments, passing less data to the stack - speed should
 be obvious here
 - [https://github.com/php/php-src/blob/7c44baa/ext/spl/php_spl.c#L661-L672
 spl_object_id] is faster than [https://github.com/php/php-
 src/blob/7c44baa/ext/spl/php_spl.c#L649-L659 spl_object_hash], as we are
 not calling [https://github.com/php/php-
 src/blob/7c44baa/ext/spl/php_spl.c#L674-L677 strpprintf].
   - Sometimes `WP_Hook::build_unique_id` will return a string (when
 concatenated), and sometimes an integer, but this should be okay.
 - No need to concatenate `'::'` - speed should be obvious here, fewer
 strings to concatenate
 - If the argument is an object, return `spl_object_id` right away rather
 than constructing a pair where the second element is `''` and then
 concatenate that empty string - speed should be obvious here
 - Bail early on `add_filter` if `$idx` is null - speed should be obvious
 here

 Tested with PHP 8.2.5 by launching `wp shell` a lot of times, after adding
 `mu-plugin.php` to the `mu-plugins` folder.

 With the patch applied, several runs average `0.19570341110229492`.
 Without the patch, the runs average `0.24287629127502441`. Calculating
 `abs(0.24287629127502441 - 0.19570341110229492)/0.24287629127502441` we
 get a 19.57% improvement.

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/58291>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list