[wp-trac] [WordPress Trac] #62126: Cache duplicate calls to WP_Theme_JSON::sanitize

WordPress Trac noreply at wordpress.org
Thu Sep 26 23:17:00 UTC 2024


#62126: Cache duplicate calls to WP_Theme_JSON::sanitize
-------------------------+-----------------------------
 Reporter:  josephscott  |      Owner:  (none)
     Type:  enhancement  |     Status:  new
 Priority:  normal       |  Milestone:  Awaiting Review
Component:  General      |    Version:
 Severity:  normal       |   Keywords:
  Focuses:  performance  |
-------------------------+-----------------------------
 A fresh out of the box WordPress site with the 2024 theme will make 53
 calls to `WP_Theme_JSON::sanitize()` during a single request to the
 default home page.  We can see the progress of duplicate `$input` calls
 with a simple addition to `wp-includes/class-wp-theme-json.php`:

 {{{
     protected static function sanitize( $input, $valid_block_names,
 $valid_element_names, $valid_variations ) {

         static $inputs = array();
         $input_key = md5( serialize( $input ) );
         if ( ! isset( $inputs[ $input_key ] ) ) {
             $inputs[ $input_key ] = 1;
         } else {
             $inputs[ $input_key ]++;
         }
         error_log( print_r( $inputs, true ) );

         $output = array();

 }}}

 The final log entry:

 {{{
 Array
 (
     [c81ffa6dbea2454497cd6bd1bb5328a0] => 2
     [c5ed022349e4a64a02171deb00a3d988] => 23
     [cb150b99ce54ad170f5d795b65f6a651] => 2
     [7aef88bb839b090be13dc01ab67a52e1] => 2
     [a277ebc729ba3b9e28c492d103043a2e] => 21
     [f8c99016466edd5a5377fdf68d6fa04d] => 1
     [4ec129bfa3c2182a1037990a0aad6232] => 1
     [6f5c486d28c1d96906e5237180daed22] => 1
 )
 }}}

 Of the 53 calls, there are only 8 unique `$input` values.  All but 3 have
 duplicates.  With that ratio it we'd likely benefit from caching the
 results and skipping the work on cache hits.  This would be a classic
 trade off of doing less work by using a bit more memory.

 I'm including a patch to cache processed `$input`s with a simple static
 variable.  This allows for multiple calls in a single page view to benefit
 from the reduced amount of work.  The exact implementation is not my main
 concern as much as reducing the amount of time spent processing
 `WP_Theme_JSON::sanitize()` calls.

 Doing 1,000 sequential HTTP GET requests for the front page, here is the
 TTFB p75 before and after.  While TTFB can vary because of any number of
 things taking longer, I'm including it to show even at that scale it can
 potentially move the needle.  To show the difference in isolation for just
 this method call I'm including the exclusive processing time for a single
 home page view for `WP_Theme_JSON::sanitize()` as measured by php-spx.

 === Before patch
 - TTFB p75: **44.28ms**
 - exclusive time: **1.74ms**

 === After patch
 - TTFB p75: **43.601ms**
 - exclusive time: **0.779ms**

 === Change
 - TTFB p75: **0.679ms** ( 1.5% )
 - exclusive time: **1.061ms** ( 61% )


 Setup:

 - WP 6.7-alpha-59092
 - PHP 8.3.10
 - Theme: Twenty Twenty-Four
 - No active plugins
 - Default home page with 2024 theme

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/62126>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list