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

Issue #13618 - only request three fields: id, title and parent to populate the Parent page select list #23637

Merged
merged 2 commits into from
Aug 26, 2020

Conversation

bobbingwide
Copy link
Contributor

@bobbingwide bobbingwide commented Jul 2, 2020

closes #13618

Description

This is a fix for the performance issue reported in #13618
The REST requests now pass the _fields parameter, requesting only 3 fields: id, title and parent.

How has this been tested?

I tested the fix in my development environment where I reported the problem initially.
I measured the server response time of the requests using my oik-bwtrace plugin.

Here's the raw data.

Before:

/oikcom/wp-json/wp/v2/pages?per_page=100&exclude=7413&parent_exclude=7413&orderby=menu_order&order=asc&context=edit&_locale=user,,14.389279,7.3.8,1291,4073,539,45,705,58,33,15,375,0.76585483551025,C:/apache/htdocs/oikcom/bwtrace/bwtrace.rest.14,829,0,367789,127.0.0.1,14.374432,2020-07-02T05:41:44+00:00,Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML; like Gecko) Chrome/83.0.4103.116 Safari/537.36,GET

/oikcom/wp-json/wp/v2/pages?per_page=100&exclude%5B0%5D=7413&parent_exclude%5B0%5D=7413&orderby=menu_order&order=asc&context=edit&_locale=user&page=2,,12.952587,7.3.8,1291,4062,539,45,704,58,33,15,379,0.60033750534058,C:/apache/htdocs/oikcom/bwtrace/bwtrace.rest.15,817,0,392390,127.0.0.1,12.938389,2020-07-02T05:41:58+00:00,Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML; like Gecko) Chrome/83.0.4103.116 Safari/537.36,GET

/oikcom/wp-json/wp/v2/pages?per_page=100&exclude%5B0%5D=7413&parent_exclude%5B0%5D=7413&orderby=menu_order&order=asc&context=edit&_locale=user&page=3,,15.117301,7.3.8,1291,4062,539,45,704,58,33,15,394,0.72246360778809,C:/apache/htdocs/oikcom/bwtrace/bwtrace.rest.17,858,0,368139,127.0.0.1,15.098362,2020-07-02T05:42:13+00:00,Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML; like Gecko) Chrome/83.0.4103.116 Safari/537.36,GET

/oikcom/wp-json/wp/v2/pages?per_page=100&exclude%5B0%5D=7413&parent_exclude%5B0%5D=7413&orderby=menu_order&order=asc&context=edit&_locale=user&page=4,,8.023179,7.3.8,1291,4062,539,45,704,58,33,15,288,0.52651596069336,C:/apache/htdocs/oikcom/bwtrace/bwtrace.rest.16,614,0,214852,127.0.0.1,8.009388,2020-07-02T05:42:21+00:00,Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML; like Gecko) Chrome/83.0.4103.116 Safari/537.36,GET

After

/oikcom/wp-json/wp/v2/pages?per_page=100&exclude=7413&parent_exclude=7413&orderby=menu_order&order=asc&_fields=id%2Ctitle%2Cparent&context=edit&_locale=user,,2.413475,7.3.8,1291,4035,479,45,643,58,33,15,118,0.26945757865906,C:/apache/htdocs/oikcom/bwtrace/bwtrace.rest.19,129,0,34319,127.0.0.1,2.409543,2020-07-02T05:46:13+00:00,Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML; like Gecko) Chrome/83.0.4103.116 Safari/537.36,GET

/oikcom/wp-json/wp/v2/pages?per_page=100&exclude%5B0%5D=7413&parent_exclude%5B0%5D=7413&orderby=menu_order&order=asc&_fields=id%2Ctitle%2Cparent&context=edit&_locale=user&page=2,,3.262983,7.3.8,1291,4024,479,45,642,58,33,15,116,0.27022075653076,C:/apache/htdocs/oikcom/bwtrace/bwtrace.rest.20,117,0,34177,127.0.0.1,3.252519,2020-07-02T05:46:17+00:00,Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML; like Gecko) Chrome/83.0.4103.116 Safari/537.36,GET

/oikcom/wp-json/wp/v2/pages?per_page=100&exclude%5B0%5D=7413&parent_exclude%5B0%5D=7413&orderby=menu_order&order=asc&_fields=id%2Ctitle%2Cparent&context=edit&_locale=user&page=3,,1.740284,7.3.8,1291,4024,479,45,642,58,33,15,118,0.44018483161926,C:/apache/htdocs/oikcom/bwtrace/bwtrace.rest.3,158,0,34457,127.0.0.1,1.729080,2020-07-02T05:46:19+00:00,Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML; like Gecko) Chrome/83.0.4103.116 Safari/537.36,GET

/oikcom/wp-json/wp/v2/pages?per_page=100&exclude%5B0%5D=7413&parent_exclude%5B0%5D=7413&orderby=menu_order&order=asc&_fields=id%2Ctitle%2Cparent&context=edit&_locale=user&page=4,,2.088225,7.3.8,1291,4024,479,45,642,58,33,15,86,0.55468797683716,C:/apache/htdocs/oikcom/bwtrace/bwtrace.rest.4,124,0,27440,127.0.0.1,2.077574,2020-07-02T05:46:21+00:00,Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML; like Gecko) Chrome/83.0.4103.116 Safari/537.36,GET

From this raw data you can glean.

Page Elapsed before Elapsed after Queries before Queries after
1 14.39 2.41 375 118
2 12.95 3.26 379 116
3 15.11 1.74 394 118
4 8.02 2.08 288 86

The detailed trace output shows which actions and filters were invoked.

For page 1 the total number was 367789 before. This reduced to 34319 after.
Before the change the the_content filter was called once for each page returned.
Afterwards, it wasn't called at all. Nor, for that matter was the_excerpt.

My testing environment is a local copy ( s.b/oikcom ) of a website called oik-plugins.com
The before and after tests summarised above were performed against pages - 373 published

I also ran the after tests against the oik-file CPT, which is also hierarchical, with 330 published posts.
server times were: 1.08, 0.86, 0.86 and 0.64

Screenshots

The Parent Page: select list is populated as before applying the fix.
The duplicate entries are still present. See #12795
image

The order doesn't match the order shown in Quick Edit either. But that's another issue... to be raised.
image

Types of changes

Performance improvement. On slow systems with complex page content it should prevent the user experiencing 500 error codes, mostly due to elapsed execution time being exceeded.

Checklist:

  • My code is tested.
  • My code follows the WordPress code style.
  • My code follows the accessibility standards.
  • My code has proper inline documentation.
  • I've included developer documentation if appropriate.
  • I've updated all React Native files affected by any refactorings/renamings in this PR.

@@ -66,6 +66,7 @@ const applyWithSelect = withSelect( ( select ) => {
parent_exclude: postId,
orderby: 'menu_order',
order: 'asc',
_fields: 'id,title,parent',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is something we tried but it's not a valid approach because the data module keeps a cache per id and if you do this and another component request the same entity but want access to the remaining properties, it will get a trimmed version causing issues. Basically whenever you use getEntityRecords you shouldn't explicitly pass context or fields. This PR tried to solve it #19498 but it's more complex than it seems.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well that's a bit of a bummer. Why not prevent caching for requests where _fields is used? And what should be done about context which is already being passed as edit. Or should the request use a completely different approach - such as an AJAX request that calls wp_dropdown_pages, which is what this code is trying to emulate after all.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree that it's not great. Ideally, something like #19498 is pursued to completion and support multiple entities shapes in core-data.

I also think the unlimited API requests is one of the worst aspects of Gutenberg right now and it has other solutions: pagination and I believe @epiqueras and @adamsilverstein are also looking into this issue.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given that the information returned for this _fields request is not going to be useful elsewhere, would it be possible to store it as a different entity type?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To land something like #19498, we would need to reinvent GraphQL, as can be seen from the caveats that led to the closing of the PR.

Can we use a direct API call in this component until pagination lands?

@youknowriad
Copy link
Contributor

Now that #19498 is merged, I think we can move forward with this. I've rebased the PR @bobbingwide do you think you can check that everything is still working as expected?

@bobbingwide
Copy link
Contributor Author

bobbingwide commented Aug 24, 2020

I've rebased the PR @bobbingwide do you think you can check that everything is still working as expected?

Hi @youknowriad. I'm sorry but I don't understand what you want me to do. I downloaded the latest version of Gutenberg but the new line of code's not there. Adding it again to the build showed that the solution works.
Though the orderby problem still exists. My other PR should fix that.

Please advise.

@bobbingwide
Copy link
Contributor Author

bobbingwide commented Aug 24, 2020

Though the orderby problem still exists. My other PR should fix that.

Can you review #12795

@youknowriad
Copy link
Contributor

I was actually talking about testing the current PR. I just did so and it seems to work properly for me.

@bobbingwide
Copy link
Contributor Author

I was actually talking about testing the current PR. I just did so and it seems to work properly for me.

As I said, I retested with the fix applied.
The results were returned quickly.
The next problem is to fix the order by problem . See #23647, which is a fix for #12795 - however

  1. The PR hasn't been reviewed
  2. And now the change I submitted has conflicts in lib/compat.php

@youknowriad youknowriad merged commit a36b3dd into WordPress:master Aug 26, 2020
@github-actions github-actions bot added this to the Gutenberg 8.9 milestone Aug 26, 2020
@bobbingwide bobbingwide deleted the fix/13618 branch September 10, 2020 06:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Abysmal response time when fetching pages to be listed in page attributes. 2nd try
3 participants