Make WordPress Core

Opened 4 weeks ago

Last modified 4 weeks ago

#62067 new enhancement

Remove Unnecessary array_flip Function in VALID_TOP_LEVEL_KEYS Filter

Reported by: wpdebuglog's profile wpdebuglog Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version:
Component: General Keywords: has-patch
Focuses: Cc:

Description

The current implementation of the array_intersect_key function uses an array_flip on the VALID_TOP_LEVEL_KEYS array to filter input.

This adds unnecessary overhead. We can optimize the process by converting VALID_TOP_LEVEL_KEYS into an associative array and passing it directly to array_intersect_key, eliminating the need for array_flip.

Sample code:


<?php
function test(){
    $a = array(
        'blockTypes' => 0,
        'customTemplates' => 1,
        'description' => 2,
        'patterns' => 3,
        'settings' => 4,
        'slug' => 5,
        'styles' => 6,
        'templateParts' => 7,
        'title' => 8,
        'version' => 9,
    );
    return $a;
}
function flip(){
    $a = array(
                'blockTypes',
                'customTemplates',
                'description',
                'patterns',
                'settings',
                'slug',
                'styles',
                'templateParts',
                'title',
                'version',
        );
    return array_flip( $a );
}

test();
flip();

https://i.ibb.co/3RP2qp2/src5.png

File: wp-includes/class-wp-theme-json.php
WP_Theme_JSON::sanitize is calling more than 50 times.

Attachments (1)

arra_flip.patch (1.1 KB) - added by wpdebuglog 4 weeks ago.

Download all attachments as: .zip

Change History (2)

Note: See TracTickets for help on using tickets.