General: Improve performance of the _wp_array_get() function.

When using a block theme, `_wp_array_get()` is the most called function on the front end of a site.

This commit makes a few minor performance optimizations, which add up to a noticeable improvement.

Follow-up to [49135], [49143], [49580].

Props aristath, jrf, afercia, costdev, swissspidy, flixos90, spacedmonkey, mukesh27, samiamnot, SergeyBiryukov.
Fixes #58376.
Built from https://develop.svn.wordpress.org/trunk@55851


git-svn-id: http://core.svn.wordpress.org/trunk@55363 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Sergey Biryukov 2023-05-23 22:00:21 +00:00
parent 8f8114fb06
commit 0ecaf73f34
2 changed files with 28 additions and 7 deletions

View File

@ -4947,14 +4947,35 @@ function _wp_array_get( $input_array, $path, $default_value = null ) {
}
foreach ( $path as $path_element ) {
if (
! is_array( $input_array ) ||
( ! is_string( $path_element ) && ! is_integer( $path_element ) && ! is_null( $path_element ) ) ||
! array_key_exists( $path_element, $input_array )
) {
if ( ! is_array( $input_array ) ) {
return $default_value;
}
$input_array = $input_array[ $path_element ];
if ( is_string( $path_element )
|| is_integer( $path_element )
|| null === $path_element
) {
/*
* Check if the path element exists in the input array.
* We check with `isset()` first, as it is a lot faster
* than `array_key_exists()`.
*/
if ( isset( $input_array[ $path_element ] ) ) {
$input_array = $input_array[ $path_element ];
continue;
}
/*
* If `isset()` returns false, we check with `array_key_exists()`,
* which also checks for `null` values.
*/
if ( array_key_exists( $path_element, $input_array ) ) {
$input_array = $input_array[ $path_element ];
continue;
}
}
return $default_value;
}
return $input_array;

View File

@ -16,7 +16,7 @@
*
* @global string $wp_version
*/
$wp_version = '6.3-alpha-55850';
$wp_version = '6.3-alpha-55851';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.