Opened 5 months ago
Last modified 7 weeks ago
#63946 new enhancement
Introduce a mechanism for lazy-loading REST routes and handlers
| Reported by: |
|
Owned by: | |
|---|---|---|---|
| Milestone: | Awaiting Review | Priority: | normal |
| Severity: | normal | Version: | |
| Component: | REST API | Keywords: | has-patch has-unit-tests |
| Focuses: | performance | Cc: |
Description
Registering REST API routes through controllers introduces additional overhead on every REST request. Benchmarking shows that the default routes registered during create_initial_rest_routes() take approximately 8.5–10ms in local testing. While this cost is relatively small in isolation, it is incurred on every request regardless of the endpoint being accessed. The impact grows even more as plugins register their own endpoints on top of core routes.
The main performance cost is not from register_rest_route() itself, but from preparing the arguments passed into it. Approximately 20% of this overhead is attributed to translation handling.
One possible solution would be to introduce a function such as register_rest_namespace(). This would allow a namespace to be declared during rest_api_init without requiring route arguments to be constructed immediately. At request time, during rest_pre_dispatch, a namespaced hook could be fired for the relevant namespace(s), enabling just-in-time registration of only the routes required for that request.
For API discovery requests, all namespaces would still need to be fully registered to ensure complete endpoint visibility. For most other requests, however, this approach could significantly reduce the cost of processing for all other requests by avoiding argument construction for unused routes.
Change History (4)
#2
@
5 months ago
After some further digging, I noticed that the WP_REST_Server::get_target_hints_for_link(), which attempts to match internal links to handlers also degrades quite a bit as new route handlers are added.
The default /wp-json/wp/v2/posts endpoint ends up calling running this for every post returned in the collection.
This ticket was mentioned in PR #10080 on WordPress/wordpress-develop by prettyboymp.
4 months ago
#3
- Keywords has-patch has-unit-tests added
Adds support for registering namespaces to the REST API Server for the purposes of lazy loading. The server will trigger a action specific to that namespace when corresponding routes would need to be loaded. This provides a performance improvement when adding new namespaces and routes by reducing the number of routes that need to be checked for matches and avoiding unnecessary translation processing.
Trac ticket: https://core.trac.wordpress.org/ticket/63946
Related: #41305