-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
wp_get_global_stylesheet
: substitute 1-minute transient by non-persistent object cache
#3712
wp_get_global_stylesheet
: substitute 1-minute transient by non-persistent object cache
#3712
Conversation
wp_get_global_stylesheet
: substitute 1-minute transient by object cachewp_get_global_stylesheet
: substitute 1-minute transient by non-persistent object cache
Sharing the context I know in case we want to test this in a plugin, first. This function is used in a couple of places:
Gutenberg substitutes the front-end styles here and the editor styles here. I presume the test plugin should do something along those lines. |
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.
Same nitpitcs, but this is looking okay. Will provide profiles in time.
* @access private | ||
*/ | ||
function _wp_add_non_persistent_theme_json_cache_group() { | ||
wp_cache_add_non_persistent_groups( 'theme_json' ); |
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.
This should be here. See this example,
wordpress-develop/src/wp-includes/load.php
Line 756 in acbbee8
wp_cache_add_non_persistent_groups( array( 'counts', 'plugins' ) ); |
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.
I see wp_cache_add_non_persistent_groups
is used in a few places:
I'm not sure where is best. Why should we use load.php instead of having a dedicated place?
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.
🤔 this further. It looks like there is no a single place for this kind of thing. If so, I'd rather try to collocate wp_cache_add_non_persistent_groups( 'theme_json' )
with the actual code that uses the cache.
The rationale for doing so is that if we move this to a different part of the codebase it becomes "out of sight, out of mind": we need to resort to human memory to remember, which is far from ideal.
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.
Also, for cross-reference and extra clarity. I'm not sure where this should be hooked. Using plugins_loaded
was the way we came up with at WordPress/gutenberg#46150 (comment) Is there a better place?
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.
Hmm, I wonder if it's better to move
add_action( 'plugins_loaded', '_wp_add_non_persistent_theme_json_cache_group' );
to src/wp-includes/default-filters.php
.
System-wide hooks/actions are supposed to go into that file.
At the same time, _wp_add_non_persistent_theme_json_cache_group()
can stay in global-styles-and-settings.php
.
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.
Moved the hook to default-filters.php
in ff7fd1e I had missed that, thanks for bringing it up!
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.
I'm not sure where this should be hooked. Using plugins_loaded was the way we came up with at WordPress/gutenberg#46150 (comment) Is there a better place?
Mostly depends on whether plugins may need it right away (while loading).
The other question is: does it make sense for this to be removable by plugins? As far as I understand it, no.
As @spacedmonkey points out above the similar wp_cache_add_non_persistent_groups( array( 'counts', 'plugins' ) );
is hard-coded and happens earlier. Thinking it makes sense for wp_cache_add_non_persistent_groups( 'theme_json' );
to be there too.
Then the hook and the helper function _wp_add_non_persistent_theme_json_cache_group()
won't be needed and the only change to the code would be to add theme_json to the first one:
wp_cache_add_non_persistent_groups( array( 'counts', 'plugins', 'theme_json' ) );
Or perhaps even better would be to add the separate wp_cache_add_non_persistent_groups( 'theme_json' );
at the bottom of wp_start_object_cache()
and add the nice explanation from the docblock as a comment.
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.
The other question is: does it make sense for this to be removable by plugins? As far as I understand it, no.
That's a good point. 🤔
Is there a way for it to happen earlier but still be collocated in this file?
Alternatively, if it is moved elsewhere, how can we make sure people reading this code has clarity about it being non-persistent? I can only think of adding comments in every function that uses that group.
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.
Is there a way for it to happen earlier but still be collocated in this file?
Don't think so unless there is a function that is called on including that file (which is generally a bad idea). Also not sure if that is particularly important. Thinking that the best option here is to add it at the bottom of wp_start_object_cache()
which seems to be the best place for such settings, and add the explanation as inline comment.
Many modern IDEs (probably all) would show cross references. Having a good explanation of what is being set and why, as in the current docblock, would be more than enough imho. It is easy to find.
can only think of adding comments in every function that uses that group
Not sure that's needed every time the cache group is used. If you're worried that somebody may miss it, perhaps adding an inline comment (pointing to where it is set) the first time it is used in global-styles-and-settings.php would make it even easier.
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.
Done at 45969f1
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.
Thanks for the PR @oandregal! Can we get some PHPUnit tests brought over from the Gutenberg PR(s)?
While WordPress/gutenberg#45679 introduced a test for this behavior, it had to be removed later at WordPress/gutenberg#45993 due to late-removal of a filter that enabled testing this. This is an ongoing conversation happening at WordPress/gutenberg#45912 so, I'm afraid I cannot bring any test here. |
While I couldn't bring any test here for the reasons I've mentioned, I've added a new one at ef450b6. This is not perfect in that it does not test the cache, though it's still useful. When we settle on how to do this going forward, it is easy to tweak. |
@costdev @azaozz @spacedmonkey @anton-vlasenko all feedback has been addressed. What's left for this to be merged? |
Thanks for the updates, @oandregal! I've tested these latest PR changes from WordPress 5.9.5 and 6.0.3, and it looks good 👍🏻 In response to @oandregal:
While local dev testing shows this to fix the stale cache issue with inline CSS, we should wait for reporters to install the hotfix plugin (or alternate test plugin) to determine if either resolves the issue in non-isolated environments. Once more feedback has been received, it will inform us of next steps. In particular, it is important to understand how this plays with plugin or host-level caching. |
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.
Looking good, @oandregal!
@ironprogrammer: I appreciate the hotfix testing. How long do you estimate we should wait for feedback?
LGTM too, just one typo ( |
In response to @mcsf:
That's a good question, and maybe best addressed by one or more core committers. Maybe @azaozz or @spacedmonkey could share some thoughts? My personal take: While we've seen a general vote of confidence in this from core contributors, we have yet to hear back from reporters. I think it's worth allowing another couple weeks for them to test and provide feedback, since the earliest this could ship is mid-January anyways -- that is, assuming it qualifies as a fix to changes introduced in 6.1.1, and isn't pushed to 6.2 (maybe March). |
Yes, there's some time until WP 6.1.2. This PR is a fix for https://core.trac.wordpress.org/ticket/56970 that most likely will be there. However thinking it can be committed to trunk (WP 6.2-alpha) now, no need to wait. That will also facilitate some more testing (in nightly builds, etc.). If there's feedback that requires changes, it can be addressed in another ticket/PR. |
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.
Sadly, in my testing, I am seeing a large performance regression.
Before
After
It is 25ms slower, this is after a merge in trunk.
This should NOT be merged until this is resolved.
I wonder if porting WordPress/gutenberg#46074 and WordPress/gutenberg#46112 over first would help this issue.
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.
LGTM!
@spacedmonkey Can you provide some more context on the performance regression? Is it just because the value was already in the object cache before? Since it is now a non persistent cache, I'd say it's expected that it is now behaving worse than before on second load. However, a 1-minute transient is arguably a less proper solution than a non-persistent cache. And on first page load performance should still be the same. I'm onboard with the current solution, however I also think we should continue working towards an actually working persistent solution to improve performance for sites that use a persistent object cache. |
Rebased and removed the no longer relevant parts. Changes for I was unable to reproduce the bug raised at https://core.trac.wordpress.org/ticket/56975#comment:32 in this PR. I'll need to go back there and see if I can have some testing steps. I want to understand how to reproduce to either introduce the fix in this PR directly or that we don't block this from landnig if it doesn't. As I shared in other places, this week I'm unable to contribute for more than half an hour here and there. So, go ahead when you feel is ready and don't wait for my feedback. Or ping me via DM if there's something I must absolutely review. |
That's a good next step, thanks for working on it. As it is now, it is based on this PR. I'd recommend preparing a separate PR instead as they are unrelated changes (though, sensibly, going in the same direction). Ideally, that work happens in Gutenberg first, so it is tested by users before merging it into core. |
Noting: Similar work is being done in @felixarntz @spacedmonkey @oandregal Do you agree? |
I see two blocking reviews, though I think I've addressed all the feedback. Please, let me know if I should look at anything to unblock this. |
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.
@oandregal Overall this looks almost good to go for me, however I have one potentially critical concern based on what we saw with the related #3556 approach.
Test ReportAs @felixarntz notes #3712 (comment), need to test the PR in the WP Admin area to determine if a fatal error happens from not having Testing Instructions
define( 'CONCATENATE_SCRIPTS', true );
define( 'WP_DEBUG', false );
define( 'SCRIPT_DEBUG', false );
If no, navigate through the screens. Does a fatal error occur? Testing ResultsEnv:
No fatal error ✅ Opened the Network tab in Dev Tools: yes, Works as expected ✅ |
Dismissing as it was for wp_get_global_settings()
- a different PR.
Does this cache need to be reset in |
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.
The changes here match the previously agreed to caching strategy in changeset https://core.trac.wordpress.org/changeset/55086.
From my testing, it does not appear to impact load-styles.php
when in the admin area.
From a code point of view, LGTM 👍 Will rely on @felixarntz and @spacedmonkey to confirm performance gains within a single request (since it's non-persistent).
Well done everyone 👏
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.
Thanks @hellofromtonya for testing and verifying this is not an issue here. In that case, good to go from my end!
Good catch, I missed that 🙃 Can you please add the matching |
@felixarntz done in d9c0909 ✅ Retested in WP Admin. No issues 💹 |
Test ReportTested with Patch tested: this PR since d9c0909. Environment
Actual Results
|
Thanks @ironprogrammer for testing it! @felixarntz should be okay to commit. Thank you everyone for your contributions! |
…global-stylesheet
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.
Sorry to come in late. Source looks good, I just have a suggestion for an additional assertion.
switch_theme( 'block-theme' ); | ||
$stylesheet_for_block_theme = wp_get_global_stylesheet(); | ||
switch_theme( WP_DEFAULT_THEME ); | ||
|
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.
switch_theme( 'block-theme' ); | |
$stylesheet_for_block_theme = wp_get_global_stylesheet(); | |
switch_theme( WP_DEFAULT_THEME ); | |
switch_theme( 'block-theme' ); | |
$post_switch_cache = wp_cache_get( 'wp_get_global_stylesheet', 'theme_json' ); | |
$stylesheet_for_block_theme = wp_get_global_stylesheet(); | |
switch_theme( WP_DEFAULT_THEME ); | |
// This needs to be tested after switching back to the default screen, thus the variable. | |
$this->assertFalse( $post_switch_cache ); |
@peterwilsoncc Apologies, this feedback came a bit too late, I had already committed this in https://core.trac.wordpress.org/changeset/55148. If it's crucial, we can do a follow up commit? |
@felixarntz I think it should be fine without. I noticed my terrible timing ;) |
Just FYI: ideally testing should happen when WP runs from
|
I was able to reproduce with the steps Tonya provided. @azaozz I tried to use the steps you suggested, but
I tried a few things, including clearing the build folder or passing It seems this PR has been committed, so I guess it can be closed/deleted. |
Thanks all for the additional feedback. No actionable change in terms of committing per the above, so I'll indeed close this for now. |
Uhh, sorry, this is a bug in core. Generally when you build to There's a ticket for that already, will try to fix it soon. |
Trac tickets: https://core.trac.wordpress.org/ticket/56910
Related to WordPress/gutenberg#45171
Backports the relevant parts of these PRs:
gutenberg_get_global_stylesheet
to useWP_Object_Cache
gutenberg#45679theme.json
object caches non persistent gutenberg#46150This PR:
Props @mmtr @felixarntz @spacedmonkey @aristath