Make WordPress Core

Opened 13 months ago

Closed 8 months ago

Last modified 8 months ago

#58319 closed enhancement (fixed)

Improve performance of get_block_theme_folders

Reported by: spacedmonkey's profile spacedmonkey Owned by: spacedmonkey's profile spacedmonkey
Milestone: 6.4 Priority: normal
Severity: normal Version: 5.9
Component: Themes Keywords: has-patch needs-testing commit needs-dev-note
Focuses: performance Cc:

Description

The function get_block_theme_folders can be called a lot in a template load. This function contains 2 file_exists checks. This can be slow, as it is accessing the file system.

Attachments (1)

Screenshot 2023-05-15 at 18.01.45.png (56.1 KB) - added by spacedmonkey 13 months ago.

Download all attachments as: .zip

Change History (27)

#1 @swissspidy
13 months ago

is_dir() is much faster than file_exists for checking directory existence. On the other hand, file_exists() also checks if the directory can be accessed. See https://bugs.php.net/bug.php?id=78285

So replacing file_exists( $theme_dir . '/block-templates' ) with is_dir( $theme_dir . '/block-templates' ) would be faster. Maybe with is_dir( $theme_dir . '/block-templates' ) && is_readable( $theme_dir . '/block-templates' ) to have the same effect.


Aside: makes me wonder why the following code uses all three checks:

https://github.com/WordPress/wordpress-develop/blob/c40e0c6f816ed27ba8b75462632498a36f624181/src/wp-includes/block-patterns.php#L374-L377

That seems like overkill.

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


13 months ago
#2

  • Keywords has-patch added

#3 @spacedmonkey
13 months ago

@swissspidy I have looked into is_dir. It is faster, but not by much. It this still the same code that is 20+ per page and it results in overhead.

#4 @spacedmonkey
13 months ago

For extra context [55236] and #57114.

#5 @spacedmonkey
13 months ago

Benchmarks run 1000 times against TT3 theme.

Trunk PR
Response Time (median) 127.75 118.17
wp-load-alloptions-query (median) 5.06 5.09
wp-before-template (median) 64.99 61.77
wp-before-template-db-queries (median) 5.67 5.28
wp-template (median) 57.21 51.32
wp-total (median) 123.3 113.31
wp-template-db-queries (median) 3.38 3.06

#6 @spacedmonkey
10 months ago

  • Milestone changed from Awaiting Review to 6.4

This ticket was mentioned in Slack in #core by oglekler. View the logs.


9 months ago

#8 @oglekler
9 months ago

  • Keywords needs-testing added

The patch needs to be tested:

  1. That everything is working as before,
  2. That there is a performance improvement.

And this is an enhancement, so, 19 days to be in trunk to get into 6.4.

This ticket was mentioned in Slack in #core-performance by spacedmonkey. View the logs.


9 months ago

This ticket was mentioned in Slack in #core-performance by thekt12. View the logs.


9 months ago

#11 @spacedmonkey
9 months ago

  • Owner set to thekt12
  • Status changed from new to assigned

This ticket was mentioned in Slack in #core-performance by thekt12. View the logs.


9 months ago

This ticket was mentioned in Slack in #core-performance by mukeshpanchal27. View the logs.


9 months ago

This ticket was mentioned in Slack in #core by oglekler. View the logs.


9 months ago

@spacedmonkey commented on PR #5202:


9 months ago
#16

@kt-12 Please check https://github.com/WordPress/wordpress-develop/pull/3629/files to check how caching was added here.

This ticket was mentioned in Slack in #core-performance by thekt12. View the logs.


9 months ago

@flixos90 commented on PR #4456:


8 months ago
#18

Closing in favor of #5202

@spacedmonkey commented on PR #5202:


8 months ago
#19

I am going to wait until @costdev reviews before committing this.

#20 @spacedmonkey
8 months ago

  • Owner changed from thekt12 to costdev
  • Status changed from assigned to reviewing

#21 @spacedmonkey
8 months ago

  • Keywords commit added
  • Owner changed from costdev to spacedmonkey

#22 @spacedmonkey
8 months ago

  • Component changed from Editor to Themes

@Kar

#23 @spacedmonkey
8 months ago

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

In 56621:

Themes: Improve performance of get_block_theme_folders function

This commit enhances the performance of the get_block_theme_folders function by introducing a new method called get_block_template_folders within the WP_Theme class. Previously, this function suffered from poor performance due to repeated file lookups using file_exists. The new method implements basic caching, storing the result in the theme's cache, similar to how block themes are cached in the block_theme property (see [55236]).

Additionally, this change improves error handling by checking if a theme exists before attempting to look up the file. It also enhances test coverage.

Props spacedmonkey, thekt12, swissspidy, flixos90, costdev, mukesh27.
Fixes #58319.

#26 @spacedmonkey
8 months ago

  • Keywords needs-dev-note added
Note: See TracTickets for help on using tickets.