#56924 closed defect (bug) (fixed)
get_attached_file(): New call to path_join() can have poor performance on NFS file systems
Reported by: | mreishus | Owned by: | |
---|---|---|---|
Milestone: | 6.1 | Priority: | normal |
Severity: | normal | Version: | 6.1 |
Component: | Media | Keywords: | commit dev-reviewed |
Focuses: | Cc: |
Description (last modified by )
In changeset [53934], this change was made to get_attached_file():
- $file = $uploads['basedir'] . "/$file"; + $file = path_join( $uploads['basedir'], $file );
Each call to path_join() calls path_is_absolute(), which calls realpath(). Most of the time this is not a problem, as realpath runs very fast, on the order of 0.003 milliseconds. However, if your uploads directory is on an NFS fileshare, I'm seeing calls to realpath() be 1000-2000x slower, or on the order of 2ms-4ms.
One call to a 4ms realpath() is not an issue, but get_attached_file() can run on each post in a list in some APIs, causing significant slowdowns. Additionally, the path_join() file can be called on the same file several times in a row, each creating a new call to realpath() without caching or memoizing the results.
Rough debugging code:
-
src/wp-includes/post.php
diff --git a/src/wp-includes/post.php b/src/wp-includes/post.php index a67d7a79a1..f728e80a7a 100644
a b function get_attached_file( $attachment_id, $unfiltered = false ) { 728 728 $uploads = wp_get_upload_dir(); 729 729 730 730 if ( false === $uploads['error'] ) { 731 732 $url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"; 733 error_log("path join: Called from $url"); 734 $t1 = microtime( true ); 735 731 736 $file = path_join( $uploads['basedir'], $file ); 737 738 $t2 = microtime( true ); 739 $e = ( $t2 - $t1 ) * 1000; 740 error_log("Elasped $e"); 741 error_log( print_r( [ 742 $uploads['basedir'], 743 $file, 744 ], true ) ); 732 745 } 733 746 }
Have a site with some posts and some images in those posts. Browse around and look for the calls, for example, while visiting media library. Note that the speeds will appear fast unless there are network-mounted filesystems using NFS being queried.
Change History (11)
This ticket was mentioned in Slack in #hosting-community by mike. View the logs.
2 years ago
#5
@
2 years ago
- Keywords commit dev-feedback added
Flagging for a revert in the 6.1 branch as well, pending a second committer's review.
#6
@
2 years ago
- Keywords dev-reviewed added; dev-feedback removed
I left a note to this extent in Slack, but +1 for reverting / backporting for 6.1.
This will give time for folks to investigate / benchmark without time pressure, and hopefully find a better performing solution for the original issue in #36308.
#8
@
2 years ago
- Resolution set to fixed
- Status changed from new to closed
I've reopened #36308 to continue working on a better solution for this.
Since this ticket is specifically about the regression in 6.1, I'm going to close this out.
Thanks for the report @mreishus. Moving to the 6.1 milestone to investigate.