[wp-trac] [WordPress Trac] #56654: Bug/performance: No need to call file_exists() on variables that use realpath()

WordPress Trac noreply at wordpress.org
Mon Sep 26 13:05:44 UTC 2022


#56654: Bug/performance: No need to call file_exists() on variables that use
realpath()
--------------------------+-----------------------------
 Reporter:  aristath      |       Owner:  SergeyBiryukov
     Type:  defect (bug)  |      Status:  closed
 Priority:  normal        |   Milestone:  6.1
Component:  General       |     Version:  trunk
 Severity:  normal        |  Resolution:  fixed
 Keywords:  has-patch     |     Focuses:  performance
--------------------------+-----------------------------
Changes (by SergeyBiryukov):

 * owner:  (none) => SergeyBiryukov
 * status:  new => closed
 * resolution:   => fixed


Comment:

 In [changeset:"54309" 54309]:
 {{{
 #!CommitTicketReference repository="" revision="54309"
 General: Remove `file_exists()` checks after calling `realpath()`.

 `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.

 This commit simplifies the checks in two functions:
 * `register_block_type_from_metadata()`
 * `wp_json_file_decode()`

 Note: In both of these cases, the values are passed through
 `wp_normalize_path()` after `realpath()`, so if the file does not exist,
 the `false` value gets converted to an empty string. The updated checks
 work both for `false` and `''` values.

 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.

 Reference: [https://www.php.net/manual/en/function.realpath.php PHP
 Manual: realpath()].

 Follow-up to [51599], [54132], [54290], [54291].

 Props aristath.
 Fixes #56654.
 }}}

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/56654#comment:3>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list