From 0ecaf73f34b34df61b7f33a229eb2b85dec0c2c2 Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Tue, 23 May 2023 22:00:21 +0000 Subject: [PATCH] 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 --- wp-includes/functions.php | 33 +++++++++++++++++++++++++++------ wp-includes/version.php | 2 +- 2 files changed, 28 insertions(+), 7 deletions(-) diff --git a/wp-includes/functions.php b/wp-includes/functions.php index 52c447d996..d7ad450680 100644 --- a/wp-includes/functions.php +++ b/wp-includes/functions.php @@ -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; diff --git a/wp-includes/version.php b/wp-includes/version.php index 485ddcbdba..14e0ad7b47 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -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.