[wp-trac] [WordPress Trac] #48530: Match REST API route by namespace before performing regex checks

WordPress Trac noreply at wordpress.org
Thu Nov 28 21:08:08 UTC 2019


#48530: Match REST API route by namespace before performing regex checks
--------------------------------------+--------------------------
 Reporter:  TimothyBlynJacobs         |       Owner:  (none)
     Type:  enhancement               |      Status:  assigned
 Priority:  normal                    |   Milestone:  5.4
Component:  REST API                  |     Version:  4.4
 Severity:  normal                    |  Resolution:
 Keywords:  has-patch has-unit-tests  |     Focuses:  performance
--------------------------------------+--------------------------
Changes (by TimothyBlynJacobs):

 * keywords:   => has-patch has-unit-tests


Comment:

 Here is a first pass at doing this filtering. I opted to add a filter
 argument to `WP_REST_Server::get_routes()` because the transformation
 process it applies results in losing the `namespace` registration route
 argument being lost so we can't easily filter the list after.

 Right now, the list is filtered before `apply_filters( 'rest_endpoints'
 )`, that feels somewhat better to me since whatever is happening in that
 filter would be able to work on less items. We could filter the list after
 the `apply_filters( 'rest_endpoints' )` too. I guess that would make sense
 if we were worried that someone might be caching the results of their
 filter call?

 I did a simple performance test. Please double check my workings, it could
 be completely bogus :)

 Registering 10 namespaces with 15 routes each.

 {{{#!php
 <?php
 function test_dispatch_performance() {
         $namespaces = 10;
         $routes     = 15;

         for ( $i = 0; $i < $namespaces; $i ++ ) {
                 for ( $j = 0; $j < $routes; $j ++ ) {
                         register_rest_route( 'my-namespace-' . $i, '/my-'
 . $j . '-route/(?P<id>[\d]+)' , array(
                                 'methods'  => 'GET',
                                 'callback' => '__return_empty_array',
                         ) );
                 }
         }

         $start = microtime( true );
         rest_get_server()->dispatch( new WP_REST_Request( 'GET', sprintf(
 '/my-namespace-%d/my-%d-route/5', 0, 0 ) ) );
         var_dump( microtime( true ) - $start );

         $start = microtime( true );
         rest_get_server()->dispatch( new WP_REST_Request( 'GET', sprintf(
 '/my-namespace-%d/my-%d-route/5', $namespaces / 2, $routes / 2 ) ) );
         var_dump( microtime( true ) - $start );

         $start = microtime( true );
         rest_get_server()->dispatch( new WP_REST_Request( 'GET', sprintf(
 '/my-namespace-%d/my-%d-route/5', $namespaces - 1, $routes - 1 ) ) );
         var_dump( microtime( true ) - $start );
 }
 }}}


 {{{
 // Before patch
 float(0.00058817863464355)
 float(0.00049686431884766)
 float(0.00046992301940918)

 // After patch
 float(0.00019693374633789)
 float(0.0002131462097168)
 float(0.00015592575073242)
 }}}

 It looks about 2.5x faster? While the absolute numbers are small, this can
 be run hundreds of times during a request for a collection response with
 embedding enabled.

 I'd like to try and figure out a real world test that would work. But my
 initial tests using a bunch of plugins with REST API routes showed way too
 much variance between API requests to get meaningful data out of it. If
 anyone has any ideas that'd be awesome.

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


More information about the wp-trac mailing list