[wp-trac] [WordPress Trac] #61112: Avoid re-constructing `WP_Theme_JSON` object from raw theme_json, instead use `WP_Theme_JSON` object inside `WP_Theme_JSON_data`

WordPress Trac noreply at wordpress.org
Wed May 1 08:25:09 UTC 2024


#61112: Avoid re-constructing `WP_Theme_JSON` object from raw theme_json, instead
use `WP_Theme_JSON` object inside `WP_Theme_JSON_data`
--------------------------+-----------------------------
 Reporter:  thekt12       |      Owner:  (none)
     Type:  defect (bug)  |     Status:  new
 Priority:  normal        |  Milestone:  Awaiting Review
Component:  General       |    Version:
 Severity:  normal        |   Keywords:  has-patch
  Focuses:  performance   |
--------------------------+-----------------------------
 In many places, we have code similar to the following.

 {{{#!php
 <?php
 $theme_json   = apply_filters( 'wp_theme_json_data_default', new
 WP_Theme_JSON_Data( $config, 'default' ) );
 $config       = $theme_json->get_data();
 static::$core = new WP_Theme_JSON( $config, 'default' );
 }}}

 In the above code, we try to get the raw JSON using
 `$theme_json->get_data()` and then we try to build back `WP_Theme_JSON`
 object. Calling `WP_Theme_JSON::_construct` is expensive.

 If we go a bit deeper we can see that `class WP_Theme_JSON_Data` has the
 following constructor
 {{{#!php
 <?php

         public function __construct( $data = array(), $origin = 'theme' )
 {
                 $this->origin     = $origin;
                 $this->theme_json = new WP_Theme_JSON( $data,
 $this->origin );
         }
 }}}

 By exposing the private attribute `theme_json` inside `WP_Theme_JSON_Data`
 via a public getter, we can avoid the expensive cyclic operation of
 creating the `WP_Theme_JSON` object from raw JSON data.

 Some early tests [https://github.com/WordPress/wordpress-
 develop/pull/6271#issuecomment-2085982737 'PR
 6271#issuecomment-2085982737'] showed by doing this we can avoid 8 calls
 to `WP_Theme_JSON::_construct` which improves the overall server load time
 performance by `~2.5%`.

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


More information about the wp-trac mailing list