#56654 closed defect (bug) (fixed)
Bug/performance: No need to call file_exists() on variables that use realpath()
Reported by: | aristath | Owned by: | SergeyBiryukov |
---|---|---|---|
Milestone: | 6.1 | Priority: | normal |
Severity: | normal | Version: | 6.1 |
Component: | General | Keywords: | has-patch add-to-field-guide |
Focuses: | performance | Cc: |
Description (last modified by )
In a few places, we generate a variable for a filename using realpath()
, and then run a file_exists()
check to proceed.
Example:
<?php $filename = realpath( 'foo' ); if ( file_exists( $filename ) ) { // Do something }
However, realpath()
already checks if the file exists, and returns false on failure. The additional file_exists()
check is not necessary and can be removed, improving the performance.
So the above code can be converted to this and yield the same results but faster:
<?php $filename = realpath( 'foo' ); if ( $filename ) { // Do something }
Though this is a small tweak, it saves a lot of checks since one of the places we do this is when registering block styles - so it runs quite a few times on each page-load.
Change History (6)
This ticket was mentioned in PR #3325 on WordPress/wordpress-develop by aristath.
2 years ago
#1
- Keywords has-patch added
#3
@
2 years ago
- Owner set to SergeyBiryukov
- Resolution set to fixed
- Status changed from new to closed
In 54309:
SergeyBiryukov commented on PR #3325:
2 years ago
#4
Thanks for the PR! Merged in r54309.
realpath()
returns false if the file does not exist. Running an additionalfile_exists()
check on a variable that has already passes throughrealpath()
is wasteful.This PR simplifies the checks in 2 instances, remove the function call.
Note: In both these cases, the variables passes through
wp_normalize_path
_ after_realpath
, so if the file does not exist, thefalse
value gets converted to an empty string. The updated checks work both forfalse
and''
values.Trac ticket: https://core.trac.wordpress.org/ticket/56654