#52992 closed enhancement (fixed)
REST API: Avoid unnecessarily preparing item links
Reported by: | dlh | Owned by: | spacedmonkey |
---|---|---|---|
Milestone: | 6.1 | Priority: | normal |
Severity: | normal | Version: | 4.7 |
Component: | REST API | Keywords: | has-patch has-unit-tests needs-testing needs-testing-info commit has-dev-note |
Focuses: | performance | Cc: |
Description
The bundled REST API controllers currently prepare links for each item in a REST response regardless of whether the links will appear in the response or are needed to fulfill an _embed
request.
For example, when responding to the request from the block editor for available hierarchical terms:
?per_page=100&orderby=name&order=asc&_fields=id,name,parent&_locale=user
WP_REST_Terms_Controller
will call prepare_links()
for all found terms even though the _links
property is omitted from each, adding unnecessary processing that delays the response to the client.
Based on a discussion in the core Slack, the change proposed for this ticket would:
- Include
_links
by default inWP_REST_Controller::get_fields_for_response()
, and include_embedded
if the request object includes the_embed
parameter. As discussed in Slack, these fields are included as a convention. The fields aren't typically included in the item schema, and becauseprepare_links()
isn't a method defined on the base controller, it isn't possible for the controller to know before the response is prepared whether it will actually include links.
- Wrap the calls to
prepare_links()
andadd_links()
in each of the bundled controllers in checks for whether the_links
field or_embedded
field is included in the response.
Lastly, during the Slack discussion, Timothy asked:
Do you think we should bother checking for each individual link? We could theoretically do rest_is_field_included( '_links.self' ), etc...
I've opted to not do this in the current patch, simply to avoid the complexity of modifying the prepare_links()
signatures across the various controllers to accept the requested fields or the request object, but I'm happy to do so if desired.
Change History (29)
This ticket was mentioned in PR #1179 on WordPress/wordpress-develop by dlh01.
4 years ago
#1
- Keywords has-unit-tests added
#2
@
4 years ago
- Keywords needs-testing added
- Milestone changed from Awaiting Review to 5.8
- Owner set to rachelbaker
- Status changed from new to accepted
Thanks for putting together the patch @dlh. I added needs testing
to ensure this is more of a performance change versus a breaking API change.
#3
@
4 years ago
- Milestone changed from 5.8 to 5.9
Today is 5.8 feature freeze. Unfortunately this one ran out of time.
Punting to 5.9 as there has been good recent momentum.
This ticket was mentioned in Slack in #core-restapi by spacedmonkey. View the logs.
3 years ago
This ticket was mentioned in Slack in #core-test by hellofromtonya. View the logs.
3 years ago
#6
@
3 years ago
- Keywords needs-refresh needs-testing-info added
Marking as needs-refresh
as there are merge conflicts with the PR.
In review with the test team, is manual behavior testing needed? If yes, then step-by-step instructions are needed to perform these manual tests and provide test reports. Marking as needs-testing-info
. If performance benchmarking testing is required and not manual behavior or smoke testing, please add a comment and remove this keyword. Thanks.
Please note, 5.9 feature freeze quickly approaching, i.e. Nov 9th. There's potentially still time for this ticket.
This ticket was mentioned in Slack in #core-restapi by spacedmonkey. View the logs.
3 years ago
#8
@
3 years ago
- Milestone changed from 5.9 to Future Release
In REST meeting today, team agreed to punt. Why? There are unknowns that need soak time and consideration. Concerned there's not enough time left before 5.9 feature freeze.
Moving to Future Release
as the next release cycle is not available yet.
However, if these concerns are addressed and it's ready for commit
before feature freeze, please feel free to move back into the 5.9 milestone.
This ticket was mentioned in Slack in #core-restapi by dlh. View the logs.
3 years ago
#10
@
3 years ago
- Keywords needs-refresh removed
I've updated the Pull Request to address the merge conflicts.
I've also added a PHPBench benchmarking class that evaluates the performance of dispatching the same REST API request to the terms endpoint that was included in the ticket description.
(The class was modeled on a benchmarking class in a Pull Request for #53218: https://github.com/WordPress/wordpress-develop/pull/1259.)
The benchmarking class is not meant to be committed, of course, but it should allow for others to run the same benchmarks for the changes in this ticket or explore their own. I also hadn't used PHPBench before, so it would be good to have more pairs of eyes on the benchmark class itself for any mistakes.
To add PHPBench to my local development environment for testing, I ran composer require phpbench/phpbench --dev
from the wordpress-develop
root.
I then used steps from the PHPBench documentation to create a baseline measurement on the master
branch:
vendor/bin/phpbench run tests/Benchmark/ --tag=original
and then a comparison with the branch in the Pull Request:
vendor/bin/phpbench run tests/Benchmark/ --report=aggregate --ref=original
In my local environment, running PHP 7.4, PHPBench typically reported around a 60% decrease in wall time and a minor decrease in memory usage to dispatch the request with the patch applied. Here's an example report from the comparison run:
+-----------------------+-------------------+-----+------+-----+-----------------+-----------------+-----------------+ | benchmark | subject | set | revs | its | mem_peak | mode | rstdev | +-----------------------+-------------------+-----+------+-----+-----------------+-----------------+-----------------+ | RestPrepareLinksBench | bench_default | | 1000 | 5 | 23.624mb -1.72% | 2.555ms -64.15% | ±1.23% -61.63% | | RestPrepareLinksBench | bench_with_filter | | 1000 | 5 | 23.625mb -1.71% | 2.479ms -67.04% | ±3.42% +269.48% | +-----------------------+-------------------+-----+------+-----+-----------------+-----------------+-----------------+
In terms of manual or smoke testing, the main things to check would be:
- The
_links
property is included in REST API responses when_fields
isn't specified or when it includes_links
. - Requests with
_embed
included the expected embedded resources.
This ticket was mentioned in Slack in #core-restapi by dlh. View the logs.
2 years ago
spacedmonkey commented on PR #1179:
2 years ago
#15
Missed links.
#16
@
2 years ago
- Keywords needs-refresh added
I have spent some time reviewing this patch. It seems simple enough and a solid change. The patch needs a refresh.
I think this can make it into 6.1. Are you still happy to own and commit - @rachelbaker ?
This ticket was mentioned in PR #2884 on WordPress/wordpress-develop by dlh01.
2 years ago
#20
- Keywords needs-refresh removed
Trac ticket: https://core.trac.wordpress.org/ticket/52992
Previously #1179. Includes the changes from #2883, which were suggested in the old PR but have since been split into different tickets.
#21
@
2 years ago
@spacedmonkey A few updates:
- I've opened https://github.com/WordPress/wordpress-develop/pull/2883 with changes that you had originally suggested in the context of this ticket and that are now covered by tickets #56019 and #56020.
- I closed my previous PR for this ticket and opened a new one with a refreshed patch: https://github.com/WordPress/wordpress-develop/pull/2884. The second PR is based on the first, so it includes the same changes.
- Regarding performance, the PHPBench benchmarking class that I used before now reports around a 50% decrease in wall time with the patch applied, compared to a 60% decrease when this ticket was reviewed last fall.
+-----------------------+-------------------+-----+------+-----+-----------------+-----------------+----------------+ | benchmark | subject | set | revs | its | mem_peak | mode | rstdev | +-----------------------+-------------------+-----+------+-----+-----------------+-----------------+----------------+ | RestPrepareLinksBench | bench_default | | 1000 | 5 | 27.265mb -1.02% | 1.473ms -54.02% | ±3.44% -19.93% | | RestPrepareLinksBench | bench_with_filter | | 1000 | 5 | 27.268mb -1.02% | 1.452ms -56.60% | ±1.31% -9.83% | +-----------------------+-------------------+-----+------+-----+-----------------+-----------------+----------------+
This ticket was mentioned in PR #3009 on WordPress/wordpress-develop by spacedmonkey.
2 years ago
#22
Trac ticket: https://core.trac.wordpress.org/ticket/52992
#23
@
2 years ago
- Keywords commit added
- Owner changed from rachelbaker to spacedmonkey
- Status changed from accepted to assigned
spacedmonkey commented on PR #3009:
2 years ago
#25
Committed.
spacedmonkey commented on PR #2884:
2 years ago
#26
Committed.
Trac ticket: https://core.trac.wordpress.org/ticket/52992