[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 07:12:16 UTC 2022


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

Old description:

> In a few places, we generate a variable for a filename using
> `realpath()`, and then run a `file_exists()` check to proceed.
> Example:
> {{{#!php
> <?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
> <?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 more than
> 70 times on each page-load.

New description:

 In a few places, we generate a variable for a filename using `realpath()`,
 and then run a `file_exists()` check to proceed.
 Example:
 {{{#!php
 <?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
 <?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.

--

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


More information about the wp-trac mailing list