Make WordPress Core

Opened 9 years ago

Closed 9 years ago

#34368 closed enhancement (fixed)

WP_Site_Icon->get_post_metadata performance improvement

Reported by: apkoponen's profile ap.koponen Owned by: obenland's profile obenland
Milestone: 4.4 Priority: normal
Severity: normal Version: 4.4
Component: Administration Keywords: has-patch commit has-unit-tests
Focuses: performance Cc:

Description

Hi,

while trying to improve the performance of a plugin that used a lot of get_post_meta (WooCommerce Bookings) I noticed from my Cache Grind analysis that WP_Site_Icon->get_post_metadata was calling get_option a lot (11 336 times in my case).

I noticed that this call was unnecessary, if $single and $meta_key did not match so I refactored the function to call get_option only if it was necessary. This refactoring reduced the get_option calls to 0.

Ari-Pekka Koponen

Attachments (3)

wp-site-icon.diff (957 bytes) - added by ap.koponen 9 years ago.
Diff for the refactored code.
34368.diff (953 bytes) - added by swissspidy 9 years ago.
34368.2.diff (2.0 KB) - added by swissspidy 9 years ago.

Download all attachments as: .zip

Change History (9)

@ap.koponen
9 years ago

Diff for the refactored code.

#1 @ocean90
9 years ago

  • Keywords has-patch commit added
  • Milestone changed from Awaiting Review to 4.4
  • Owner set to obenland
  • Status changed from new to assigned

Nice catch!

@swissspidy
9 years ago

#2 follow-up: @swissspidy
9 years ago

34368.diff uses strict comparison and fixes the line indentation.

#3 in reply to: ↑ 2 ; follow-up: @ocean90
9 years ago

Replying to swissspidy:

34368.diff uses strict comparison

Be careful with strict comparison:

<?php
var_dump( get_option( 'site_icon' ) ); // string(4) "1785"

@swissspidy
9 years ago

#4 in reply to: ↑ 3 ; follow-up: @swissspidy
9 years ago

  • Keywords has-unit-tests added

Replying to ocean90:

Replying to swissspidy:

34368.diff uses strict comparison

Be careful with strict comparison:

<?php
var_dump( get_option( 'site_icon' ) ); // string(4) "1785"

That wasn't a problem in the unit test (see 34368.2.diff), but I added an (int) nonetheless.

#5 in reply to: ↑ 4 @ocean90
9 years ago

Replying to swissspidy:

That wasn't a problem in the unit test (see 34368.2.diff),

Right, because _insert_attachment()/wp_insert_attachment() returns an integer. But if it gets saved through the Customizer then the value is currently a string.

#6 @obenland
9 years ago

  • Resolution set to fixed
  • Status changed from assigned to closed

In 35299:

Site Icon: Get site icon ID only when needed.

Cuts down on unnecessary queries, especially in environments that rely on
post meta a lot. Reverts [32997].

Props ap.koponen, swissspidy.
Fixes #34368.

Note: See TracTickets for help on using tickets.