WordPress.org

Make WordPress Core

#45553 closed enhancement (fixed)

Improve wp_is_stream() performance

Reported by: swissspidy Owned by: pento
Milestone: 5.1 Priority: low
Severity: minor Version:
Component: Filesystem API Keywords: has-patch
Focuses: Cc:

Description

The wp_is_stream() function (used by functions like wp_normalize_path() currently uses a dynamically constructed regex to detect streams. This could be verified a lot by using in_array() instead.

@schlessera suggested the following version over at https://github.com/wp-cli/wp-cli/issues/5008:

function wp_is_stream( $path ) {
    $scheme_separator = strpos( $path, '://' );

    if ( false === $scheme_separator ) {
        // $path isn't a stream
        return false;
    }
 
    $stream = substr( $path, 0, $scheme_separator );

    return in_array( $stream, stream_get_wrappers(), true );
}

A quick benchmark shows that this is around four times faster than the current method.

Attachments (1)

45553.diff (830 bytes) - added by swissspidy 22 months ago.

Download all attachments as: .zip

Change History (3)

@swissspidy
22 months ago

#1 @pento
21 months ago

  • Owner set to pento
  • Status changed from new to assigned

#2 @pento
21 months ago

  • Resolution set to fixed
  • Status changed from assigned to closed

In 44506:

Filesystem: Improve wp_is_stream() performance.

Instead of turning the return value of stream_get_wrappers() into a regex to match the scheme, we can instead extract the scheme and search the return value of stream_get_wrappers().

Props schlessera, swissspidy.
Fixes #45553.

Note: See TracTickets for help on using tickets.