Make WordPress Core

Opened 15 months ago

Closed 15 months ago

Last modified 15 months ago

#56654 closed defect (bug) (fixed)

Bug/performance: No need to call file_exists() on variables that use realpath()

Reported by: aristath's profile aristath Owned by: sergeybiryukov's profile 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 aristath)

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.


15 months ago
#1

  • Keywords has-patch added

realpath() returns false if the file does not exist. Running an additional file_exists() check on a variable that has already passes through realpath() 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, the false value gets converted to an empty string. The updated checks work both for false and '' values.

Trac ticket: https://core.trac.wordpress.org/ticket/56654

#2 @aristath
15 months ago

  • Description modified (diff)

#3 @SergeyBiryukov
15 months ago

  • Owner set to SergeyBiryukov
  • Resolution set to fixed
  • Status changed from new to closed

In 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: PHP Manual: realpath().

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

Props aristath.
Fixes #56654.

SergeyBiryukov commented on PR #3325:


15 months ago
#4

Thanks for the PR! Merged in r54309.

#5 @milana_cap
15 months ago

  • Keywords add-to-field-guide added

This ticket was mentioned in Slack in #core by sergey. View the logs.


15 months ago

Note: See TracTickets for help on using tickets.