WordPress.org

Make WordPress Core

Opened 11 months ago

Closed 11 months ago

Last modified 8 months ago

#50700 closed defect (bug) (fixed)

REST API: Small optimizations to filtering by context

Reported by: dlh Owned by: TimothyBlynJacobs
Milestone: 5.5 Priority: normal
Severity: normal Version: 5.5
Component: REST API Keywords: has-patch has-unit-tests
Focuses: Cc:

Description

As previously discussed in Slack, the attached patch proposes some minor optimizations to rest_filter_response_by_context() and adds a few tests.

In the patch:

  • Avoid recalculating the schema type with each iteration of the loop.
  • If the schema type is an array including both array and object, attempt to use the best type for the given response data (currently, the data is always be treated as array). Includes a test for this case.
  • If the type is array and the context of items doesn't match, return an empty array and break out of the loop since each item will be checked against the same schema anyway. Includes a test for this case.

Attachments (1)

50700.diff (5.1 KB) - added by dlh 11 months ago.

Download all attachments as: .zip

Change History (5)

@dlh
11 months ago

#1 @TimothyBlynJacobs
11 months ago

  • Milestone changed from Awaiting Review to 5.5

Thanks for this @dlh! This looks right to me. Can you make this a PR so we can get the full test suite to run?

This ticket was mentioned in PR #424 on WordPress/wordpress-develop by dlh01.


11 months ago

  • Keywords has-unit-tests added

The attached patch proposes some minor optimizations to rest_filter_response_by_context() and adds a few tests..

Trac ticket: https://core.trac.wordpress.org/ticket/50700

#3 @TimothyBlynJacobs
11 months ago

  • Owner set to TimothyBlynJacobs
  • Resolution set to fixed
  • Status changed from new to closed

In 48555:

REST API: Optimize rest_filter_response_by_context performance.

In [47758] a new function rest_filter_response_by_context was introduced to expand the JSON schema features supported by the context filtering mechanism.

This commit improves the performance of that function by eliminating repetitive comparisons and loops. Additionally, it improves multi-type support for object + array types.

Fixes #50700.
Props dlh.

Note: See TracTickets for help on using tickets.