Make WordPress Core

Opened 5 years ago

Closed 5 years ago

#45553 closed enhancement (fixed)

Improve wp_is_stream() performance

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


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

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 5 years ago.

Download all attachments as: .zip

Change History (3)

5 years ago

#1 @pento
5 years ago

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

#2 @pento
5 years 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.