[wp-trac] [WordPress Trac] #59314: Cache the result of template file lookups

WordPress Trac noreply at wordpress.org
Thu Oct 26 16:08:18 UTC 2023


#59314: Cache the result of template file lookups
-------------------------------------+--------------------------
 Reporter:  joemcgill                |       Owner:  mukesh27
     Type:  enhancement              |      Status:  assigned
 Priority:  normal                   |   Milestone:  6.5
Component:  Themes                   |     Version:
 Severity:  normal                   |  Resolution:
 Keywords:  needs-patch 2nd-opinion  |     Focuses:  performance
-------------------------------------+--------------------------

Comment (by thekt12):

 I did a small experiment to see how stat caching improves the performance
 of `file_exists`.
 I added the following code to the Hello Dolly plugin.

 The following code carries out two experiments:
 One to measure the effect of stat cache when the file exists and another
 for when the file doesn't exist.

 Each test calls two functions, one to measure the time taken for first
 `file_exists` call and another for time taken by 1000 subsequent
 `file_exists` checks for the same file.

 By dividing the value for 1000 calls by the value of the first call, we
 will get to know that a caching is effective or not. A very small value
 indicate that caching is effective. A value larger and closer to 1000
 indicates that caching is not effective.



 {{{
 add_action( 'init', 'test_file_not_exists' );
 function test_file_not_exists() {
         $filename = plugin_dir_path(__FILE__)."notexists.php";
         $start  = microtime( true );
         if ( file_exists( $filename ) ) {
                 echo "The file `$filename` exists <br>";
         } else {
                 echo "The file $filename does not exist<br>";
         }
         $end_time = $start - microtime( true );
         $GLOBALS["fcN"] = $end_time;
         print_r( "Time for First call (A)-> {$end_time} <br>");
 }

 add_action( 'init', 'test_file_not_exists_2' );
 function test_file_not_exists_2() {
         global $fcN;
         $filename = plugin_dir_path(__FILE__)."notexists.php";
         $start  = microtime( true );
         for( $i = 0; $i < 1000; $i++ ) {
                 if ( file_exists( $filename ) ) {
                         // echo "The file $filename exists";
                 } else {
                         // echo "The file $filename does not exist";
                 }
         }
         $end_time = $start - microtime( true );
         print_r( "Time for 1000 call after first call (B)-> {$end_time}
 <br>");
         print_r( "Proportion (B/A) - ". ($end_time/$fcN). " <br>");
 }
 }}}


 The result obtained was

 {{{
 The file `/var/www/src/wp-content/plugins/hello.php` exists
 Time for First call (A)-> -3.8862228393555E-5
 Time for 1000 call after first call (B)-> -0.0023980140686035
 Proportion (B/A) - 61.705521472393


 The file /var/www/src/wp-content/plugins/notexists.php does not exist
 Time for First call (A)-> -0.00048208236694336
 Time for 1000 call after first call (B)-> -0.33195185661316
 Proportion (B/A) - 688.57912957468
 }}}

 The result was nearly the same across multiple rounds.

 == Findings
 Caching internal to `file_exists` is effective if the file exists.
 However, it shows minimal benefit if the file we are searching for doesn't
 exits.

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


More information about the wp-trac mailing list