[wp-trac] [WordPress Trac] #43819: Improve performance of function "wp_update_attachment_metadata" in post.php

WordPress Trac noreply at wordpress.org
Fri Apr 20 14:18:59 UTC 2018


#43819: Improve performance of function "wp_update_attachment_metadata" in post.php
-----------------------------+-----------------------------
 Reporter:  lucasbustamante  |      Owner:  (none)
     Type:  enhancement      |     Status:  new
 Priority:  normal           |  Milestone:  Awaiting Review
Component:  Upload           |    Version:  4.9.5
 Severity:  normal           |   Keywords:
  Focuses:  performance      |
-----------------------------+-----------------------------
 I had to import thousands of images to WordPress and it was taking an
 average of 5 seconds to upload each image, even though they were all on
 localhost, so WordPress only had to move it and register it in the
 database.

 To speed up this process, I debugged each and every call made during a
 media upload, and found out the great guilty that, after a little tweak,
 made my media upload going from **5 seconds to 0.7 seconds**.

 What was slowing it down was {{{
 add_filter('wp_update_attachment_metadata', $data, $post->ID) }}} inside
 function {{{ wp_update_attachment_metadata()}}} in post.php:

 {{{#!php
 <?php
 /**
  * Update metadata for an attachment.
  *
  * @since 2.1.0
  *
  * @param int   $attachment_id Attachment post ID.
  * @param array $data          Attachment meta data.
  * @return int|bool False if $post is invalid.
  */
 function wp_update_attachment_metadata( $attachment_id, $data ) {
         $attachment_id = (int) $attachment_id;
         if ( ! $post = get_post( $attachment_id ) ) {
                 return false;
         }

         /**
          * Filters the updated attachment meta data.
          *
          * @since 2.1.0
          *
          * @param array $data          Array of updated attachment meta
 data.
          * @param int   $attachment_id Attachment post ID.
          */
         if ( $data = apply_filters( 'wp_update_attachment_metadata',
 $data, $post->ID ) )
                 return update_post_meta( $post->ID,
 '_wp_attachment_metadata', $data );
         else
                 return delete_post_meta( $post->ID,
 '_wp_attachment_metadata' );
 }
 }}}

 As I had to upload thousands of images, I modified it to this, just during
 the import:

 {{{#!php
 <?php
 /**
  * Update metadata for an attachment.
  *
  * @since 2.1.0
  *
  * @param int   $attachment_id Attachment post ID.
  * @param array $data          Attachment meta data.
  * @return int|bool False if $post is invalid.
  */
 function wp_update_attachment_metadata( $attachment_id, $data ) {
         $attachment_id = (int) $attachment_id;
         if ( ! $post = get_post( $attachment_id ) ) {
                 return false;
         }

         /**
          * Filters the updated attachment meta data.
          *
          * @since 2.1.0
          *
          * @param array $data          Array of updated attachment meta
 data.
          * @param int   $attachment_id Attachment post ID.
          */
         if (WP_DOING_IMPORT) {
                 return update_post_meta( $post->ID,
 '_wp_attachment_metadata', $data );
         }
         if ( $data = apply_filters( 'wp_update_attachment_metadata',
 $data, $post->ID ) )
                 return update_post_meta( $post->ID,
 '_wp_attachment_metadata', $data );
         else
                 return delete_post_meta( $post->ID,
 '_wp_attachment_metadata' );
 }
 }}}

 Before the tweak:

 {{{
   'uploadtimedebug_start' => 0 seconds
   'uploadtimedebug_after_requires' => 0.006 seconds
   'uploadtimedebug_after_tmp_filled' => 0.597 seconds
   'uploadtimedebug_before_media_handle_sideload' => 0.597 seconds
   'uploadtimedebug_after_media_handle_sideload' => 5.370 seconds
   'uploadtimedebug_finish' => 5.370 seconds
 }}}


 After the tweak:

 {{{
   'uploadtimedebug_inicio' => 0 seconds
   'uploadtimedebug_after_requires' => 0.007 seconds
   'uploadtimedebug_after_tmp_filled' => 0.620 seconds
   'uploadtimedebug_before_media_handle_sideload' => 0.620 seconds
   'uploadtimedebug_after_media_handle_sideload' => 0.723 seconds
   'uploadtimedebug_finish' => 0.723 seconds
 }}}

 I wonder if we could improve this piece of code to be more performant?

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


More information about the wp-trac mailing list