Make WordPress Core


Ignore:
Timestamp:
09/18/2023 12:39:18 PM (16 months ago)
Author:
spacedmonkey
Message:

Plugins: Store result of call to array_keys, to save repeated calls in WP_Hook class.

In the WP_Hook class the function array_keys was called every time an array of hook priorities was needed. For sites with lots of filters or actions, this would result in thousands of calls to the array_keys function, which uses server resources. Instead of recomputing this array every time it is needed, only compute it when filters are added and removed, then store the result as a class property. Improve unit tests to ensure this behaviour is tested.

Props spacedmonkey, bor0, flixos90, hellofromTonya, mukesh27.
Fixes #58458.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-hook.php

    r56549 r56609  
    2828
    2929    /**
     30     * Priorities list.
     31     *
     32     * @since 6.4.0
     33     * @var array
     34     */
     35    protected $priorities = array();
     36
     37    /**
    3038     * The priority keys of actively running iterations of a hook.
    3139     *
     
    8795        }
    8896
     97        $this->priorities = array_keys( $this->callbacks );
     98
    8999        if ( $this->nesting_level > 0 ) {
    90100            $this->resort_active_iterations( $priority, $priority_existed );
     
    103113     */
    104114    private function resort_active_iterations( $new_priority = false, $priority_existed = false ) {
    105         $new_priorities = array_keys( $this->callbacks );
     115        $new_priorities = $this->priorities;
    106116
    107117        // If there are no remaining hooks, clear out all running iterations.
     
    187197            if ( ! $this->callbacks[ $priority ] ) {
    188198                unset( $this->callbacks[ $priority ] );
     199
     200                $this->priorities = array_keys( $this->callbacks );
    189201
    190202                if ( $this->nesting_level > 0 ) {
     
    263275
    264276        if ( false === $priority ) {
    265             $this->callbacks = array();
     277            $this->callbacks  = array();
     278            $this->priorities = array();
    266279        } elseif ( isset( $this->callbacks[ $priority ] ) ) {
    267280            unset( $this->callbacks[ $priority ] );
     281            $this->priorities = array_keys( $this->callbacks );
    268282        }
    269283
     
    290304        $nesting_level = $this->nesting_level++;
    291305
    292         $this->iterations[ $nesting_level ] = array_keys( $this->callbacks );
     306        $this->iterations[ $nesting_level ] = $this->priorities;
    293307
    294308        $num_args = count( $args );
     
    349363    public function do_all_hook( &$args ) {
    350364        $nesting_level                      = $this->nesting_level++;
    351         $this->iterations[ $nesting_level ] = array_keys( $this->callbacks );
     365        $this->iterations[ $nesting_level ] = $this->priorities;
    352366
    353367        do {
     
    482496            $this->callbacks[ $offset ] = $value;
    483497        }
     498
     499        $this->priorities = array_keys( $this->callbacks );
    484500    }
    485501
     
    496512    public function offsetUnset( $offset ) {
    497513        unset( $this->callbacks[ $offset ] );
     514        $this->priorities = array_keys( $this->callbacks );
    498515    }
    499516
Note: See TracChangeset for help on using the changeset viewer.