[wp-trac] [WordPress Trac] #31182: array_shift in taxonomy.php very slow on large array

WordPress Trac noreply at wordpress.org
Fri Jan 30 12:17:29 UTC 2015


#31182: array_shift in taxonomy.php very slow on large array
--------------------------+-----------------------------
 Reporter:  VolodymyrC    |      Owner:
     Type:  defect (bug)  |     Status:  new
 Priority:  normal        |  Milestone:  Awaiting Review
Component:  General       |    Version:  4.1
 Severity:  normal        |   Keywords:
  Focuses:                |
--------------------------+-----------------------------
 If wordpress blog have a large number posts (100000 or more), then this
 error has effect:


 {{{
 PHP Fatal error:  Maximum execution time of 30 seconds exceeded in /wpdir
 /wp-includes/taxonomy.php on line 1961
 }}}

 This is a code from line 1961 in taxonomy.php:

 {{{
 if ( 'id=>parent' == $_fields ) {
         while ( $term = array_shift( $terms ) ) {
                 $_terms[$term->term_id] = $term->parent;
         }
 } elseif ( 'ids' == $_fields ) {
         while ( $term = array_shift( $terms ) ) {
                 $_terms[] = $term->term_id;
         }
 } elseif ( 'names' == $_fields ) {
         while ( $term = array_shift( $terms ) ) {
                 $_terms[] = $term->name;
         }
 } elseif ( 'id=>name' == $_fields ) {
         while ( $term = array_shift( $terms ) ) {
                 $_terms[$term->term_id] = $term->name;
         }
 } elseif ( 'id=>slug' == $_fields ) {
         while ( $term = array_shift( $terms ) ) {
                 $_terms[$term->term_id] = $term->slug;
         }
 }
 }}}


 But array_shift function is very slow on large arrays.
 [http://kb.ucla.edu/articles/performance-of-array_shift-and-array_pop-in-
 php]


 == Result: ==
 array_pop takes 0.00089 seconds[[BR]]
 array_shift takes 15.15544 seconds[[BR]]
 array_reverse + array_pop takes 0.03934 seconds[[BR]]


 My suggestion is make a change from array_shift to array_pop.

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


More information about the wp-trac mailing list