[wp-trac] [WordPress Trac] #63946: Introduce a mechanism for lazy-loading REST routes and handlers

WordPress Trac noreply at wordpress.org
Mon Mar 9 15:39:38 UTC 2026


#63946: Introduce a mechanism for lazy-loading REST routes and handlers
--------------------------------------+------------------------------
 Reporter:  prettyboymp               |       Owner:  (none)
     Type:  enhancement               |      Status:  new
 Priority:  normal                    |   Milestone:  Awaiting Review
Component:  REST API                  |     Version:
 Severity:  normal                    |  Resolution:
 Keywords:  has-patch has-unit-tests  |     Focuses:  performance
--------------------------------------+------------------------------

Comment (by rmccue):

 This feels to me a bit like the wrong approach: registering the namespaces
 and their routes isn't expensive (it's basically just adding to an array),
 rather it's the argument building that's slow.

 I'd suggest a better API here would be to make the `$args` parameter of
 type `array|callable`, and lazily call this when the route's arguments are
 needed. This would keep namespaces and routes registered, while still
 offloading the actual majority of the slow operations.

 It would also be easy to transition, especially with arrow functions;
 you'd change your code from
 {{{
 register_rest_route( 'myexample/v1', '/ping', [
         'methods'  => 'GET',
         'callback' => [ $this, 'handle_ping' ],
         'args' => $this->get_endpoint_args_for_item_schema(),
         'schema' => $this->get_item_schema(),
 ] );
 }}}

 to

 {{{
 register_rest_route( 'myexample/v1', '/ping', fn () => [
         'methods'  => 'GET',
         'callback' => [ $this, 'handle_ping' ],
         'args' => $this->get_endpoint_args_for_item_schema(),
         'schema' => $this->get_item_schema(),
 ] );
 }}}
 and magically your performance would improve.

 This conceptually moves the route registration then from being potentially
 a heavy operation to effectively
 {{{
 $this->routes[ $namespace ][ $route ] = $args;
 }}}
 which is heavily optimised in PHP (3 opcodes).

 This improves the performance of non-REST API entrypoints (although proper
 usage of `rest_api_init` avoids this anyway), ''and'' the performance of
 individual routes themselves, as the args only need full evaluation after
 URL matching.

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/63946#comment:7>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list