-
Notifications
You must be signed in to change notification settings - Fork 4.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Theme JSON: cached resolved URIs #66155
base: trunk
Are you sure you want to change the base?
Changes from all commits
c8660e3
6dea724
436486f
5cdb27e
2f172a9
739d40c
b9762eb
6140eb0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
https://github.com/WordPress/wordpress-develop/pull/7596 | ||
|
||
* https://github.com/WordPress/gutenberg/pull/66155 |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -91,6 +91,14 @@ class WP_Theme_JSON_Resolver_Gutenberg { | |
*/ | ||
protected static $theme_json_file_cache = array(); | ||
|
||
/** | ||
* Cache for resolved files per theme. | ||
* | ||
* @since 6.8.0 | ||
* @var array | ||
*/ | ||
protected static $resolved_theme_uris_cache = array(); | ||
|
||
/** | ||
* Processes a file that adheres to the theme.json schema | ||
* and returns an array with its contents, or a void array if none found. | ||
|
@@ -696,20 +704,22 @@ protected static function get_file_path_from_theme( $file_name, $template = fals | |
* and `$i18n_schema` variables to reset. | ||
* @since 6.1.0 Added the `$blocks` and `$blocks_cache` variables | ||
* to reset. | ||
* @since 6.8.0 Added the `$resolved_theme_uris_cache` variable to reset. | ||
*/ | ||
public static function clean_cached_data() { | ||
static::$core = null; | ||
static::$blocks = null; | ||
static::$blocks_cache = array( | ||
static::$core = null; | ||
static::$blocks = null; | ||
static::$blocks_cache = array( | ||
'core' => array(), | ||
'blocks' => array(), | ||
'theme' => array(), | ||
'user' => array(), | ||
); | ||
static::$theme = null; | ||
static::$user = null; | ||
static::$user_custom_post_type_id = null; | ||
static::$i18n_schema = null; | ||
static::$theme = null; | ||
static::$user = null; | ||
static::$user_custom_post_type_id = null; | ||
static::$i18n_schema = null; | ||
static::$resolved_theme_uris_cache = array(); | ||
} | ||
|
||
/** | ||
|
@@ -839,6 +849,7 @@ public static function get_style_variations_from_directory( $directory, $scope = | |
* | ||
* @since 6.6.0 | ||
* @since 6.7.0 Added support for resolving block styles. | ||
* @since 6.8.0 Added caching for resolved theme URIs. | ||
* | ||
* @param WP_Theme_JSON_Gutenberg $theme_json A theme json instance. | ||
* @return array An array of resolved paths. | ||
|
@@ -850,7 +861,12 @@ public static function get_resolved_theme_uris( $theme_json ) { | |
return $resolved_theme_uris; | ||
} | ||
|
||
$theme_json_data = $theme_json->get_raw_data(); | ||
$theme_json_data = $theme_json->get_raw_data(); | ||
$resolved_theme_uris_cache_key = md5( wp_json_encode( $theme_json_data ) ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Before the caching we should check theme development mode. WDYT?
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good point, but I'm not sure whether it's required here just yet. The method is called several times in a single session and the main performance enhancement here is to avoid subsequent (and expensive) processing in the same session. This applies to the frontend and editor. Nothing is saved to the database so I don't expect theme developers to run into any gotchas when testing sites given that the cache is wiped every time the page refreshes. Also it's being used in I could be missing some subtleties so please let me know! |
||
|
||
if ( ! empty( static::$resolved_theme_uris_cache[ $resolved_theme_uris_cache_key ] ) ) { | ||
return static::$resolved_theme_uris_cache[ $resolved_theme_uris_cache_key ]; | ||
} | ||
|
||
// Using the same file convention when registering web fonts. See: WP_Font_Face_Resolver:: to_theme_file_uri. | ||
$placeholder = 'file:./'; | ||
|
@@ -901,7 +917,7 @@ public static function get_resolved_theme_uris( $theme_json ) { | |
} | ||
} | ||
} | ||
|
||
static::$resolved_theme_uris_cache[ $resolved_theme_uris_cache_key ] = $resolved_theme_uris; | ||
return $resolved_theme_uris; | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not yet sure if this will make things better or worse 😄
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It'd be great if WordPress had minimum PHP 8 support for faster hashing: https://php.watch/articles/php-hash-benchmark
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
md4
outperformsmd5
for file integrity on many benchmarks I've seen buthash( 'md4', wp_json_encode( $theme_json_data ) )
doesn't move the needle at all for me in performance tests.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we had a minimum PHP version of 8, we could avoid the hashing and use a
WeakMap
where the theme_json data/object was the key.