Make WordPress Core

Opened 20 months ago

Closed 20 months ago

Last modified 20 months ago

#57057 closed task (blessed) (fixed)

Reduce usage of `wp_get_theme`

Reported by: spacedmonkey's profile spacedmonkey Owned by: spacedmonkey's profile spacedmonkey
Milestone: 6.1.1 Priority: normal
Severity: normal Version: 6.1
Component: Themes Keywords: has-patch has-screenshots commit fixed-major
Focuses: performance Cc:

Description

The function wp_get_theme function returns an instance of WP_Theme instance. Creating an instance of this class uses resources. In many cases this is not needed, as a single property is used. Lowering usage will save CPU cycles.

Attachments (4)

Screenshot 2022-11-10 at 10.50.38.png (53.8 KB) - added by spacedmonkey 20 months ago.
Before
Screenshot 2022-11-10 at 10.48.16.png (101.5 KB) - added by spacedmonkey 20 months ago.
After
Screenshot 2022-11-11 at 10.39.18.png (10.3 KB) - added by spacedmonkey 20 months ago.
Before
Screenshot 2022-11-11 at 10.40.06.png (10.4 KB) - added by spacedmonkey 20 months ago.

Download all attachments as: .zip

Change History (23)

This ticket was mentioned in PR #3599 on WordPress/wordpress-develop by @spacedmonkey.


20 months ago
#1

  • Keywords has-patch added

#2 @krupalpanchal
20 months ago

  • Keywords has-screenshots added

#3 @desrosj
20 months ago

@spacedmonkey It looks like one of the changes needs to be submitted upstream to Gutenberg.

There are definitely some instances where removing wp_get_theme() may be appropriate, and this seems like a great improvement at the surface. But I wonder if there's a better way since this does not address the root problem that creating instances of WP_Theme can be resource intensive. But what about reworking WP_Theme to have a get_instance() method that's called by wp_get_theme() (similar to what is done for posts, terms, comments, etc.) and using the Cache API?

This also feels like a major release change rather than a minor one to me. I'm going to leave it in 6.1.2 for now and will let the folks leading that release make the final call, but I recommend waiting for 6.2 for including this.

#4 @flixos90
20 months ago

  • Keywords needs-unit-tests added

@spacedmonkey Doesn't really need many unit tests, but one unit test to cover the change in supported method parameter value would be useful.

@desrosj For me, most of this is reasonable to land in 6.1.x as it's trivial replacements of wp_get_theme()->get_stylesheet() with get_stylesheet() which is good for performance. However, changing the supported values for a method parameter, despite being backward compatible, strictly speaking would go beyond a 6.1.x. Though I don't have a strong opinion either way.

#5 follow-up: @spacedmonkey
20 months ago

@flixos90 Changing the get_user_data_from_wp_global_styles is the biggest change in this patch. If we remove that, I think can commit in 6.1.x.

This is a trival change as it a function replacement and drop-in solution.

#6 in reply to: ↑ 5 @flixos90
20 months ago

Replying to spacedmonkey:

@flixos90 Changing the get_user_data_from_wp_global_styles is the biggest change in this patch. If we remove that, I think can commit in 6.1.x.

This is a trival change as it a function replacement and drop-in solution.

SGTM. In that case, it doesn't need unit tests either.

Last edited 20 months ago by flixos90 (previous) (diff)

#7 @desrosj
20 months ago

I think if the change to get_user_data_from_wp_global_styles is removed, we could look at including this in 6.1.1.

#8 @desrosj
20 months ago

  • Keywords changes-requested added; needs-unit-tests removed

#9 @spacedmonkey
20 months ago

@peterwilsoncc @flixos90 @desrosj I have scaled back to the PR, so it just does drop-in replacements of one function to another.

Please review. I have provided before and after numbers.

#10 @desrosj
20 months ago

  • Keywords commit added; changes-requested removed
  • Milestone changed from 6.1.2 to 6.1.1

Thanks @spacedmonkey! Looks good. I say let's include this improvement in 6.1.1.

#11 @JeffPaul
20 months ago

  • Type changed from enhancement to task (blessed)

#12 @spacedmonkey
20 months ago

  • Owner set to spacedmonkey
  • Resolution set to fixed
  • Status changed from new to closed

In 54817:

Themes: Reduce usage of wp_get_theme function.

Calling the wp_get_theme function creates a instance of the WP_Theme class. This can be a performance issue, if all you need is one property of the class instance. This change replaces the usage of wp_get_theme()->get_stylesheet() with get_stylesheet() to improve performance.

Props spacedmonkey, flixos90, peterwilsoncc, desrosj.
Fixes #57057.

#13 @desrosj
20 months ago

  • Keywords fixed-major added
  • Resolution fixed deleted
  • Status changed from closed to reopened

Reopening for backport to 6.1.1.

#14 @desrosj
20 months ago

  • Resolution set to fixed
  • Status changed from reopened to closed

In 54818:

Themes: Reduce usage of wp_get_theme function.

Calling the wp_get_theme function creates a instance of the WP_Theme class. This can be a performance issue, if all you need is one property of the class instance. This change replaces the usage of wp_get_theme()->get_stylesheet() with get_stylesheet() to improve performance.

Props spacedmonkey, flixos90, peterwilsoncc, desrosj.
Merges [54817] to the 6.1 branch.
Fixes #57057.

#15 @desrosj
20 months ago

In 54819:

Themes: Revert one instance of wp_get_theme() from [54817].

Since this specific call to wp_get_theme() is found within wp-includes/blocks, this change will need to be made upstream in the Gutenberg repository.

See #57057.

#16 @desrosj
20 months ago

In 54820:

Themes: Revert one instance of wp_get_theme() from [54817].

Since this specific call to wp_get_theme() is found within wp-includes/blocks, this change will need to be made upstream in the Gutenberg repository.

Merges [54819] to the 6.1 branch.
See #57057.

@spacedmonkey commented on PR #3599:


20 months ago
#17

Committed

#18 @desrosj
20 months ago

In 54824:

Coding Standards: Apply spacing changes after composer format.

Follow up to [54817].
See #57057.

#19 @desrosj
20 months ago

In 54826:

Coding Standards: Apply spacing changes after composer format.

Follow up to [54817].

Merges [54824] to the 6.1 branch.
See #57057.

Note: See TracTickets for help on using tickets.