Opened 3 weeks ago
Last modified 3 weeks ago
#58319 new enhancement
Improve performance of get_block_theme_folders
Reported by: |
|
Owned by: | |
---|---|---|---|
Milestone: | Awaiting Review | Priority: | normal |
Severity: | normal | Version: | 5.9 |
Component: | Editor | Keywords: | has-patch |
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)
Change History (6)
This ticket was mentioned in PR #4456 on WordPress/wordpress-develop by @spacedmonkey.
3 weeks ago
#2
- Keywords has-patch added
Trac ticket: https://core.trac.wordpress.org/ticket/58319#ticket
#3
@
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.
#5
@
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.
is_dir()
is much faster thanfile_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=78285So replacing
file_exists( $theme_dir . '/block-templates' )
withis_dir( $theme_dir . '/block-templates' )
would be faster. Maybe withis_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.