WordPress.org

Make WordPress Core

Changeset 32383


Ignore:
Timestamp:
05/06/2015 04:18:11 PM (7 years ago)
Author:
boonebgorges
Message:

Improve performance of loop detection in _get_term_children().

Using an array keyed by term_id allows us to use isset() rather than the
slower in_array(). In addition, it lets us avoid the use of wp_list_pluck()
on large arrays, and helps us to avoid arrays that are unnecessarily large due
to duplicate entries.

Fixes #32144 for 4.2 branch.

Location:
branches/4.2
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/4.2

  • branches/4.2/src/wp-includes/taxonomy.php

    r32117 r32383  
    39273927 * @param array  $ancestors Term ancestors that have already been identified. Passed by reference, to keep track of
    39283928 *                          found terms when recursing the hierarchy. The array of located ancestors is used to prevent
    3929  *                          infinite recursion loops.
     3929 *                          infinite recursion loops. For performance, term_ids are used as array keys, with 1 as value.
    39303930 * @return array The subset of $terms that are descendants of $term_id.
    39313931 */
     
    39433943    // Include the term itself in the ancestors array, so we can properly detect when a loop has occurred.
    39443944    if ( empty( $ancestors ) ) {
    3945         $ancestors[] = $term_id;
     3945        $ancestors[ $term_id ] = 1;
    39463946    }
    39473947
     
    39563956
    39573957        // Don't recurse if we've already identified the term as a child - this indicates a loop.
    3958         if ( in_array( $term->term_id, $ancestors ) ) {
     3958        if ( isset( $ancestors[ $term->term_id ] ) ) {
    39593959            continue;
    39603960        }
     
    39693969                continue;
    39703970
    3971             if ( $use_id ) {
    3972                 $ancestors = array_merge( $ancestors, $term_list );
    3973             } else {
    3974                 $ancestors = array_merge( $ancestors, wp_list_pluck( $term_list, 'term_id' ) );
    3975             }
     3971            $ancestors[ $term->term_id ] = 1;;
    39763972
    39773973            if ( $children = _get_term_children( $term->term_id, $terms, $taxonomy, $ancestors) )
Note: See TracChangeset for help on using the changeset viewer.