Skip to content
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

Open
wants to merge 8 commits into
base: trunk
Choose a base branch
from
Open

Conversation

ramonjd
Copy link
Member

@ramonjd ramonjd commented Oct 16, 2024

What?

Implement "pretty basic caching" for resolved theme URIs in WP_Theme_JSON_Resolver::get_resolved_theme_uris.

Why?

The intention to improve performance, even if slightly.

WP_Theme_JSON_Resolver::get_resolved_theme_uris, and WP_Theme_JSON_Resolver::resolve_theme_file_uris which calls it, is fired multiple times in a single session to:

  1. Generate styles
  2. Deliver resolved URIs in a global styles controller response.

Results will become more pronounced as other relative paths are introduced, e.g., fonts, and more blocks support background images.

Cached data is not be stored persistently across page loads.

How?

Simple object caching.

Testing Instructions

Run npm run test:unit:php:base -- --filter WP_Theme_JSON_Resolver_Gutenberg_Test

Create a theme.json with background image paths defined for several blocks.

Ensure these appear as expected in the editor and frontend.

In the theme.json file, replace one or several paths with new asset paths. Check that the styles have been updated and the editor/frontend looks good.

Here is some test JSON based on current assets in TT5:

{
	"$schema": "../../schemas/json/theme.json",
	"version": 3,
	"settings": {
		"appearanceTools": true
	},
	"styles": {
		"background": {
			"backgroundImage": {
				"url": "file:./assets/images/category-anthuriums.webp"
			}
		},
		"blocks": {
			"core/group": {
				"background": {
					"backgroundImage": {
						"url": "file:./assets/images/category-cactus.webp"
					}
				}
			},
			"core/post-content": {
				"background": {
					"backgroundImage": {
						"url": "file:./assets/images/hero-podcast.webp"
					}
				}
			},
			"core/quote": {
				"background": {
					"backgroundImage": {
						"url": "file:./assets/images/category-sunflowers.webp"
					}
				}
			},
			"core/pullquote": {
				"background": {
					"backgroundImage": {
						"url": "file:./assets/images/dallas-creek-square.webp"
					}
				}
			}
		}
	}
}

@ramonjd ramonjd self-assigned this Oct 16, 2024
Copy link

Flaky tests detected in fb5e0f3.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/11397729500
📝 Reported issues:

@ramonjd ramonjd added [Type] Enhancement A suggestion for improvement. Global Styles Anything related to the broader Global Styles efforts, including Styles Engine and theme.json labels Oct 21, 2024
@ramonjd ramonjd changed the title Try: cached resolved URIs Theme JSON: cached resolved URIs Oct 21, 2024
@ramonjd ramonjd marked this pull request as ready for review October 21, 2024 02:54
@ramonjd ramonjd requested a review from spacedmonkey as a code owner October 21, 2024 02:54
Copy link

github-actions bot commented Oct 21, 2024

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: ramonjd <[email protected]>
Co-authored-by: aaronrobertshaw <[email protected]>
Co-authored-by: mukeshpanchal27 <[email protected]>
Co-authored-by: spacedmonkey <[email protected]>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

ramonjd added a commit to ramonjd/wordpress-develop that referenced this pull request Oct 21, 2024
Theme JSON: cached resolved URIs
@@ -850,6 +861,13 @@ public static function get_resolved_theme_uris( $theme_json ) {
return $resolved_theme_uris;
}

$theme_json_data = $theme_json->get_raw_data();
$resolved_theme_uris_cache_key = md5( wp_json_encode( $theme_json_data ) );
Copy link
Member Author

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 😄

Copy link
Member Author

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

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

md4 outperforms md5 for file integrity on many benchmarks I've seen but hash( 'md4', wp_json_encode( $theme_json_data ) ) doesn't move the needle at all for me in performance tests.

Copy link
Contributor

@aaronrobertshaw aaronrobertshaw Oct 22, 2024

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.

@spacedmonkey
Copy link
Member

As this is performance related, CC @felixarntz @joehoyle

Copy link
Contributor

@aaronrobertshaw aaronrobertshaw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for following up on this one @ramonjd 👍

Bonus points for the theme.json snippet in the test instructions. Definitely made this easier to test.

✅ Code changes make sense
✅ Unit tests are passing
✅ Background images set with relative paths display correctly
✅ Background images update correctly when adding new path or switching ones around

Screenshot of resolved image paths rendering Screenshot 2024-10-22 at 12 26 58 pm

I think caching this data for a single request is fine for now. It would be an easy update to move it to a transient or something.

That said, I'm happy to defer to the performance experts in sanity checking the overall approach.

The CI test failure appears to be an unrelated time out. I've kicked that off again.

@@ -850,6 +861,13 @@ public static function get_resolved_theme_uris( $theme_json ) {
return $resolved_theme_uris;
}

$theme_json_data = $theme_json->get_raw_data();
$resolved_theme_uris_cache_key = md5( wp_json_encode( $theme_json_data ) );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Before the caching we should check theme development mode. WDYT?

$can_use_cached = ! wp_is_development_mode( 'theme' );
if ( $can_use_cached ) {
	// Add caching here.
}

Copy link
Member Author

Choose a reason for hiding this comment

The 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 gutenberg_get_global_stylesheet and gutenberg_get_global_stylesheet which are both using caches as well.

I could be missing some subtleties so please let me know!

@mukeshpanchal27
Copy link
Member

I run profiling for WP_Theme_JSON_Resolver::get_resolved_theme_uris in wordpress-develop but its really quick function that don't take more processing time.

TT4 theme:

Screenshot 2024-10-24 at 11 06 36 AM

@ramonjd ramonjd force-pushed the try/cache-resolved-theme-uris branch from 9d34c56 to b9762eb Compare October 24, 2024 06:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Global Styles Anything related to the broader Global Styles efforts, including Styles Engine and theme.json [Type] Enhancement A suggestion for improvement.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants