Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WP_Theme_JSON_Resolver_Gutenberg::get_user_data_from_custom_post_type uses uncached SQL queries #36574

Closed
xknown opened this issue Nov 17, 2021 · 0 comments · Fixed by #36584
Closed
Assignees
Labels
[Status] In Progress Tracking issues with work in progress [Type] Performance Related to performance efforts

Comments

@xknown
Copy link
Member

xknown commented Nov 17, 2021

Description

We noticed a few performance related issues in big multisite WP site related to the uncached queries used in WP_Theme_JSON_Resolver_Gutenberg::get_user_data_from_custom_post_type(). While there's some caching done at the class level, it'd be good to use object caching to avoid unnecessary SQL queries triggered by the call to wp_get_recent_posts.

Step-by-step reproduction instructions

Please run the following snippet in a WP install with Gutenberg installed.

Screenshots, screen recording, code snippet

<?php
require __DIR__ . '/wp-load.php';

$__queries = array();
$__func = function( $query ) use ( &$__queries ) {
        if ( preg_match( '#post_type = \'wp_global_styles\'#', $query ) ) {
                $__queries[] = $query;
        }
        return $query;
};
add_filter( 'query', $__func );
for ( $i = 0; $i < 10; $i++ ) {
        WP_Theme_JSON_Resolver_Gutenberg::get_user_data();
        WP_Theme_JSON_Resolver_Gutenberg::clean_cached_data();
}
var_dump($__queries);

Ideally, we should only do the SQL query once.

Environment info

  • WordPress 5.8.2
  • Gutenberg 11.8.3 (the latest versions are also affected, and also add more similar code in gutenberg_add_active_global_styles_link)

Please confirm that you have searched existing issues in the repo.

Yes

Please confirm that you have tested with all plugins deactivated except Gutenberg.

Yes

@xknown xknown added the [Type] Performance Related to performance efforts label Nov 17, 2021
@github-actions github-actions bot added the [Status] In Progress Tracking issues with work in progress label Nov 17, 2021
Mamaduka pushed a commit that referenced this issue Nov 24, 2021
…ustom_post_type() (#36584)

* Add caching to WP_Theme_JSON_Resolver_Gutenberg::get_user_data_from_custom_post_type

Refactor
`WP_Theme_JSON_Resolver_Gutenberg::get_user_data_from_custom_post_type`
to add caching to the `wp_global_styles` post type lookup, and also to
remove the duplicate code added in
`gutenberg_add_active_global_styles_link`.

Fixes #36574

* Add unit tests

* Use the correct query filter

* Use wp_cache_set instead of wp_cache_add.

* Remove the filter when finished.

* Simplify the if blocks
noahtallen pushed a commit that referenced this issue Nov 24, 2021
…ustom_post_type() (#36584)

* Add caching to WP_Theme_JSON_Resolver_Gutenberg::get_user_data_from_custom_post_type

Refactor
`WP_Theme_JSON_Resolver_Gutenberg::get_user_data_from_custom_post_type`
to add caching to the `wp_global_styles` post type lookup, and also to
remove the duplicate code added in
`gutenberg_add_active_global_styles_link`.

Fixes #36574

* Add unit tests

* Use the correct query filter

* Use wp_cache_set instead of wp_cache_add.

* Remove the filter when finished.

* Simplify the if blocks
noisysocks pushed a commit that referenced this issue Nov 29, 2021
…ustom_post_type() (#36584)

* Add caching to WP_Theme_JSON_Resolver_Gutenberg::get_user_data_from_custom_post_type

Refactor
`WP_Theme_JSON_Resolver_Gutenberg::get_user_data_from_custom_post_type`
to add caching to the `wp_global_styles` post type lookup, and also to
remove the duplicate code added in
`gutenberg_add_active_global_styles_link`.

Fixes #36574

* Add unit tests

* Use the correct query filter

* Use wp_cache_set instead of wp_cache_add.

* Remove the filter when finished.

* Simplify the if blocks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Status] In Progress Tracking issues with work in progress [Type] Performance Related to performance efforts
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant