Skip to content

Conversation

marcusquinn
Copy link
Contributor

Performance Optimization: Improved Filesystem Operations

Description

This PR optimizes filesystem operations to improve performance by implementing caching and using the WordPress Filesystem API for better compatibility and security. These functions are used throughout the plugin for file operations, and optimizing them can significantly improve performance.

Changes Made

  1. Added caching to wu_get_main_site_upload_dir() to cache upload directory information for 1 hour
  2. Added caching to wu_maybe_create_folder() to cache folder paths
  3. Replaced direct file operations with WordPress Filesystem API for better compatibility and security
  4. Added caching to wu_get_folder_url() to cache folder URLs
  5. Added a new wu_clear_filesystem_cache() function to clear the filesystem cache when needed

Benefits

  • Reduced filesystem operations for frequently accessed paths
  • Improved performance when creating and accessing folders
  • Better compatibility with different hosting environments
  • More secure file operations using WordPress Filesystem API
  • Reduced server load for file operations

Testing

  1. Create and access folders using the plugin's functionality
  2. Verify that files are created correctly
  3. Check that performance is improved for repeated file operations

Performance Impact

  • Before: Each filesystem operation executed multiple filesystem checks and operations on every request
  • After: Filesystem operations are cached and optimized, reducing filesystem load and improving response time

This optimization is particularly effective for sites with many file operations, such as those with many uploads or custom files.

@superdav42
Copy link
Collaborator

@marcusquinn Did you run any kind of profiling to see the performance improvements of adding a cache? I would not expect any of these functions to have any kind of overhead that would make caching beneficial. Maybe if they are hot functions being called on every page load but I'm not sure they are.

@marcusquinn
Copy link
Contributor Author

Just Code Profile Pro: https://nintechnet.com/codeprofiler/

I think worth adding a GitHub action for this: https://github.com/swissspidy/wp-performance-action

I'll add a separate issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants