[wp-trac] [WordPress Trac] #50679: Optimization for wp_get_attachment_metadata()
WordPress Trac
noreply at wordpress.org
Thu Jul 16 23:46:10 UTC 2020
#50679: Optimization for wp_get_attachment_metadata()
-------------------------+------------------------------
Reporter: Tkama | Owner: (none)
Type: enhancement | Status: new
Priority: normal | Milestone: Awaiting Review
Component: Media | Version: 5.4.2
Severity: normal | Resolution:
Keywords: | Focuses: performance
-------------------------+------------------------------
Comment (by Tkama):
{{{#!php
<?php
// attachment invalid ID
$attachment_id = 'aa1342';
$post = get_post( $attachment_id = (int) $attachment_id );
// 0.0000001 sec.
$data = get_post_meta( $attachment_id, '_wp_attachment_metadata', true );
// 0.0000001 sec.
for( $i=1; $i<50000; $i++ ){
$post = get_post( $attachment_id = (int) $attachment_id );
// 0.010389 sec.
$data = get_post_meta( $attachment_id, '_wp_attachment_metadata', true
); // 0.005079 sec.
}
// this attachment exists
$attachment_id = 13423;
clean_post_cache( $attachment_id );
$post = get_post( $attachment_id = (int) $attachment_id );
// 0.000927 sec.
$data = get_post_meta( $attachment_id, '_wp_attachment_metadata', true );
// 0.001256 sec.
for( $i=1; $i<50000; $i++ ){
$post = get_post( $attachment_id = (int) $attachment_id );
// 0.126539 sec.
$data = get_post_meta( $attachment_id, '_wp_attachment_metadata', true
); // 0.210088 sec.
}
// attachment NOT exists
$attachment_id = 13423333;
clean_post_cache( $attachment_id );
$post = get_post( $attachment_id = (int) $attachment_id );
// 0.000516 sec.
$data = get_post_meta( $attachment_id, '_wp_attachment_metadata', true );
// 0.000432 sec.
for( $i=1; $i<50000; $i++ ){
$post = get_post( $attachment_id = (int) $attachment_id );
// 10.004103 sec.
$data = get_post_meta( $attachment_id, '_wp_attachment_metadata', true
); // 0.199436 sec.
}
}}}
NOTE about code: each line of code with benchmark was run separately.
My conclusion: we no need any additional checks to run `get_post_meta()` —
code above it only slow down the code. We even can't pass WP_Post object
into the `wp_get_attachment_metadata()` to increase the function speed.
For example, I need to call `wp_get_attachment_metadata()` more then 1200
times per site page generation and I have no choice except use
`get_post_meta( $post->ID, '_wp_attachment_metadata', true )` directly.
P.S. I was shocked when seen `get_post()` for not existence attachment -
10.004103 sec. fro 50k iterations.
--
Ticket URL: <https://core.trac.wordpress.org/ticket/50679#comment:3>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list