Make WordPress Core

Opened 3 weeks ago

Last modified 3 weeks ago

#58319 new enhancement

Improve performance of get_block_theme_folders

Reported by: spacedmonkey's profile spacedmonkey Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 5.9
Component: Editor Keywords: has-patch
Focuses: performance Cc:


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 3 weeks ago.

Download all attachments as: .zip

Change History (6)

#1 @swissspidy
3 weeks 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

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:

That seems like overkill.

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

3 weeks ago

  • Keywords has-patch added

#3 @spacedmonkey
3 weeks 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
3 weeks ago

For extra context [55236] and #57114.

#5 @spacedmonkey
3 weeks 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
Note: See TracTickets for help on using tickets.