Commit 2f41f3fdad for wordpress.org
commit 2f41f3fdaded86e2f4baca87001138542fb7fc55
Author: dmsnell <dmsnell@git.wordpress.org>
Date: Tue Jul 1 16:01:26 2025 +0000
do_blocks(): Document transient-memory-leak optimization.
Adds explanatory comment indicating why the optimization was added
and guarding against accidental removal.
This is a documentation-only change and should include no functional
or visual changes.
Props audrasjb, dmsnell, joemcgill, sirlouen, westonruter, zunaid321.
Follow-up to [60316].
Fixes #63588.
Built from https://develop.svn.wordpress.org/trunk@60400
git-svn-id: http://core.svn.wordpress.org/trunk@59736 1a063a9b-81f0-0310-95a4-ce76da25c4cd
diff --git a/wp-includes/blocks.php b/wp-includes/blocks.php
index 3d18b37b83..f0001d80b8 100644
--- a/wp-includes/blocks.php
+++ b/wp-includes/blocks.php
@@ -2408,6 +2408,25 @@ function do_blocks( $content ) {
$top_level_block_count = count( $blocks );
$output = '';
+ /**
+ * Parsed blocks consist of a list of top-level blocks. Those top-level
+ * blocks may themselves contain nested inner blocks. However, every
+ * top-level block is rendered independently, meaning there are no data
+ * dependencies between them.
+ *
+ * Ideally, therefore, the parser would only need to parse one complete
+ * top-level block at a time, render it, and move on. Unfortunately, this
+ * is not possible with {@see \parse_blocks()} because it must parse the
+ * entire given document at once.
+ *
+ * While the current implementation prevents this optimization, it’s still
+ * possible to reduce the peak memory use when calls to `render_block()`
+ * on those top-level blocks are memory-heavy (which many of them are).
+ * By setting each parsed block to `NULL` after rendering it, any memory
+ * allocated during the render will be freed and reused for the next block.
+ * Before making this change, that memory was retained and would lead to
+ * out-of-memory crashes for certain posts that now run with this change.
+ */
for ( $i = 0; $i < $top_level_block_count; $i++ ) {
$output .= render_block( $blocks[ $i ] );
$blocks[ $i ] = null;
diff --git a/wp-includes/version.php b/wp-includes/version.php
index 1e7121e38f..2be43f6456 100644
--- a/wp-includes/version.php
+++ b/wp-includes/version.php
@@ -16,7 +16,7 @@
*
* @global string $wp_version
*/
-$wp_version = '6.9-alpha-60399';
+$wp_version = '6.9-alpha-60400';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.