From caa04a507f1652e74a4004b8c2d6bee472316d85 Mon Sep 17 00:00:00 2001 From: Mukesh Panchal Date: Thu, 30 Jul 2026 20:42:54 +0530 Subject: [PATCH 1/4] Editor, Themes: Evaluate count() once per loop instead of once per iteration. A `for` condition runs once per iteration plus once to terminate, so `$i < count( $array )` calls `count()` n + 1 times to walk an n-element array whose length never changes. Compute the bound in the loop initialiser instead, matching the idiom already used throughout core. `register_block_type_from_metadata()` is the main beneficiary: it runs for every registered block on every request, and the affected loops sit inside `foreach` blocks covering three script fields and three style fields each. In every case the iterated array is provably invariant across the loop body, so there is no behaviour change. Props mukesh. --- src/wp-includes/blocks.php | 6 +++--- src/wp-includes/class-wp-theme-json.php | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/wp-includes/blocks.php b/src/wp-includes/blocks.php index 41e11f4a2a75f..28a50b443781e 100644 --- a/src/wp-includes/blocks.php +++ b/src/wp-includes/blocks.php @@ -617,7 +617,7 @@ function register_block_type_from_metadata( $file_or_folder, $args = array() ) { $scripts = $metadata[ $metadata_field_name ]; $processed_scripts = array(); if ( is_array( $scripts ) ) { - for ( $index = 0; $index < count( $scripts ); $index++ ) { + for ( $index = 0, $script_count = count( $scripts ); $index < $script_count; $index++ ) { $result = register_block_script_handle( $metadata, $metadata_field_name, @@ -651,7 +651,7 @@ function register_block_type_from_metadata( $file_or_folder, $args = array() ) { $modules = $metadata[ $metadata_field_name ]; $processed_modules = array(); if ( is_array( $modules ) ) { - for ( $index = 0; $index < count( $modules ); $index++ ) { + for ( $index = 0, $module_count = count( $modules ); $index < $module_count; $index++ ) { $result = register_block_script_module_id( $metadata, $metadata_field_name, @@ -687,7 +687,7 @@ function register_block_type_from_metadata( $file_or_folder, $args = array() ) { $styles = $metadata[ $metadata_field_name ]; $processed_styles = array(); if ( is_array( $styles ) ) { - for ( $index = 0; $index < count( $styles ); $index++ ) { + for ( $index = 0, $style_count = count( $styles ); $index < $style_count; $index++ ) { $result = register_block_style_handle( $metadata, $metadata_field_name, diff --git a/src/wp-includes/class-wp-theme-json.php b/src/wp-includes/class-wp-theme-json.php index 82b8e89de509c..7545c7da8b170 100644 --- a/src/wp-includes/class-wp-theme-json.php +++ b/src/wp-includes/class-wp-theme-json.php @@ -5379,7 +5379,7 @@ public function set_spacing_sizes() { // If there are 7 or fewer steps in the scale revert to numbers for labels instead of t-shirt sizes. if ( $spacing_scale['steps'] <= 7 ) { - for ( $spacing_sizes_count = 0; $spacing_sizes_count < count( $spacing_sizes ); $spacing_sizes_count++ ) { + for ( $spacing_sizes_count = 0, $total_spacing_sizes = count( $spacing_sizes ); $spacing_sizes_count < $total_spacing_sizes; $spacing_sizes_count++ ) { $spacing_sizes[ $spacing_sizes_count ]['name'] = (string) ( $spacing_sizes_count + 1 ); } } From 79d89df844596674a32e03005c6dee6eb06c56ed Mon Sep 17 00:00:00 2001 From: Mukesh Panchal Date: Wed, 5 Aug 2026 21:39:15 +0530 Subject: [PATCH 2/4] Move count() out of the loop initialiser into a variable --- src/wp-includes/blocks.php | 9 ++++++--- src/wp-includes/class-wp-theme-json.php | 3 ++- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/wp-includes/blocks.php b/src/wp-includes/blocks.php index d0834dbc5da4d..2b5ed62a74388 100644 --- a/src/wp-includes/blocks.php +++ b/src/wp-includes/blocks.php @@ -677,7 +677,8 @@ function register_block_type_from_metadata( $file_or_folder, $args = array() ) { $scripts = $metadata[ $metadata_field_name ]; $processed_scripts = array(); if ( is_array( $scripts ) ) { - for ( $index = 0, $script_count = count( $scripts ); $index < $script_count; $index++ ) { + $script_count = count( $scripts ); + for ( $index = 0; $index < $script_count; $index++ ) { $result = register_block_script_handle( $metadata, $metadata_field_name, @@ -711,7 +712,8 @@ function register_block_type_from_metadata( $file_or_folder, $args = array() ) { $modules = $metadata[ $metadata_field_name ]; $processed_modules = array(); if ( is_array( $modules ) ) { - for ( $index = 0, $module_count = count( $modules ); $index < $module_count; $index++ ) { + $module_count = count( $modules ); + for ( $index = 0; $index < $module_count; $index++ ) { $result = register_block_script_module_id( $metadata, $metadata_field_name, @@ -747,7 +749,8 @@ function register_block_type_from_metadata( $file_or_folder, $args = array() ) { $styles = $metadata[ $metadata_field_name ]; $processed_styles = array(); if ( is_array( $styles ) ) { - for ( $index = 0, $style_count = count( $styles ); $index < $style_count; $index++ ) { + $style_count = count( $styles ); + for ( $index = 0; $index < $style_count; $index++ ) { $result = register_block_style_handle( $metadata, $metadata_field_name, diff --git a/src/wp-includes/class-wp-theme-json.php b/src/wp-includes/class-wp-theme-json.php index f25927a29e9df..be77c486ce6f5 100644 --- a/src/wp-includes/class-wp-theme-json.php +++ b/src/wp-includes/class-wp-theme-json.php @@ -5379,7 +5379,8 @@ public function set_spacing_sizes() { // If there are 7 or fewer steps in the scale revert to numbers for labels instead of t-shirt sizes. if ( $spacing_scale['steps'] <= 7 ) { - for ( $spacing_sizes_count = 0, $total_spacing_sizes = count( $spacing_sizes ); $spacing_sizes_count < $total_spacing_sizes; $spacing_sizes_count++ ) { + $total_spacing_sizes = count( $spacing_sizes ); + for ( $spacing_sizes_count = 0; $spacing_sizes_count < $total_spacing_sizes; $spacing_sizes_count++ ) { $spacing_sizes[ $spacing_sizes_count ]['name'] = (string) ( $spacing_sizes_count + 1 ); } } From b2a04be77aeb3b3858768f7bd864d22bce48c96f Mon Sep 17 00:00:00 2001 From: Mukesh Panchal Date: Sun, 9 Aug 2026 21:32:11 +0530 Subject: [PATCH 3/4] Revert "Move count() out of the loop initialiser into a variable" This reverts commit 79d89df844596674a32e03005c6dee6eb06c56ed. --- src/wp-includes/blocks.php | 9 +++------ src/wp-includes/class-wp-theme-json.php | 3 +-- 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/src/wp-includes/blocks.php b/src/wp-includes/blocks.php index 2b5ed62a74388..d0834dbc5da4d 100644 --- a/src/wp-includes/blocks.php +++ b/src/wp-includes/blocks.php @@ -677,8 +677,7 @@ function register_block_type_from_metadata( $file_or_folder, $args = array() ) { $scripts = $metadata[ $metadata_field_name ]; $processed_scripts = array(); if ( is_array( $scripts ) ) { - $script_count = count( $scripts ); - for ( $index = 0; $index < $script_count; $index++ ) { + for ( $index = 0, $script_count = count( $scripts ); $index < $script_count; $index++ ) { $result = register_block_script_handle( $metadata, $metadata_field_name, @@ -712,8 +711,7 @@ function register_block_type_from_metadata( $file_or_folder, $args = array() ) { $modules = $metadata[ $metadata_field_name ]; $processed_modules = array(); if ( is_array( $modules ) ) { - $module_count = count( $modules ); - for ( $index = 0; $index < $module_count; $index++ ) { + for ( $index = 0, $module_count = count( $modules ); $index < $module_count; $index++ ) { $result = register_block_script_module_id( $metadata, $metadata_field_name, @@ -749,8 +747,7 @@ function register_block_type_from_metadata( $file_or_folder, $args = array() ) { $styles = $metadata[ $metadata_field_name ]; $processed_styles = array(); if ( is_array( $styles ) ) { - $style_count = count( $styles ); - for ( $index = 0; $index < $style_count; $index++ ) { + for ( $index = 0, $style_count = count( $styles ); $index < $style_count; $index++ ) { $result = register_block_style_handle( $metadata, $metadata_field_name, diff --git a/src/wp-includes/class-wp-theme-json.php b/src/wp-includes/class-wp-theme-json.php index 37054e7f21640..3431cc76d13ab 100644 --- a/src/wp-includes/class-wp-theme-json.php +++ b/src/wp-includes/class-wp-theme-json.php @@ -5386,8 +5386,7 @@ public function set_spacing_sizes() { // If there are 7 or fewer steps in the scale revert to numbers for labels instead of t-shirt sizes. if ( $spacing_scale['steps'] <= 7 ) { - $total_spacing_sizes = count( $spacing_sizes ); - for ( $spacing_sizes_count = 0; $spacing_sizes_count < $total_spacing_sizes; $spacing_sizes_count++ ) { + for ( $spacing_sizes_count = 0, $total_spacing_sizes = count( $spacing_sizes ); $spacing_sizes_count < $total_spacing_sizes; $spacing_sizes_count++ ) { $spacing_sizes[ $spacing_sizes_count ]['name'] = (string) ( $spacing_sizes_count + 1 ); } } From 6ef4db2d2d42968ccd74cd680169469075640990 Mon Sep 17 00:00:00 2001 From: Mukesh Panchal Date: Mon, 10 Aug 2026 09:15:41 +0530 Subject: [PATCH 4/4] Apply suggestions from code review Co-authored-by: Weston Ruter --- src/wp-includes/blocks.php | 6 +++--- src/wp-includes/class-wp-theme-json.php | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/wp-includes/blocks.php b/src/wp-includes/blocks.php index d0834dbc5da4d..f2df7226a6d78 100644 --- a/src/wp-includes/blocks.php +++ b/src/wp-includes/blocks.php @@ -677,7 +677,7 @@ function register_block_type_from_metadata( $file_or_folder, $args = array() ) { $scripts = $metadata[ $metadata_field_name ]; $processed_scripts = array(); if ( is_array( $scripts ) ) { - for ( $index = 0, $script_count = count( $scripts ); $index < $script_count; $index++ ) { + for ( $index = 0, $length = count( $scripts ); $index < $length; $index++ ) { $result = register_block_script_handle( $metadata, $metadata_field_name, @@ -711,7 +711,7 @@ function register_block_type_from_metadata( $file_or_folder, $args = array() ) { $modules = $metadata[ $metadata_field_name ]; $processed_modules = array(); if ( is_array( $modules ) ) { - for ( $index = 0, $module_count = count( $modules ); $index < $module_count; $index++ ) { + for ( $index = 0, $length = count( $modules ); $index < $length; $index++ ) { $result = register_block_script_module_id( $metadata, $metadata_field_name, @@ -747,7 +747,7 @@ function register_block_type_from_metadata( $file_or_folder, $args = array() ) { $styles = $metadata[ $metadata_field_name ]; $processed_styles = array(); if ( is_array( $styles ) ) { - for ( $index = 0, $style_count = count( $styles ); $index < $style_count; $index++ ) { + for ( $index = 0, $length = count( $styles ); $index < $length; $index++ ) { $result = register_block_style_handle( $metadata, $metadata_field_name, diff --git a/src/wp-includes/class-wp-theme-json.php b/src/wp-includes/class-wp-theme-json.php index 3431cc76d13ab..7175d7a88747d 100644 --- a/src/wp-includes/class-wp-theme-json.php +++ b/src/wp-includes/class-wp-theme-json.php @@ -5386,7 +5386,7 @@ public function set_spacing_sizes() { // If there are 7 or fewer steps in the scale revert to numbers for labels instead of t-shirt sizes. if ( $spacing_scale['steps'] <= 7 ) { - for ( $spacing_sizes_count = 0, $total_spacing_sizes = count( $spacing_sizes ); $spacing_sizes_count < $total_spacing_sizes; $spacing_sizes_count++ ) { + for ( $spacing_sizes_count = 0, $spacing_sizes_length = count( $spacing_sizes ); $spacing_sizes_count < $spacing_sizes_length; $spacing_sizes_count++ ) { $spacing_sizes[ $spacing_sizes_count ]['name'] = (string) ( $spacing_sizes_count + 1 ); } }