Skip to content

Commit f36b8ee

Browse files
committed
do_blocks(): Document transient-memory-leak optimization. (#9052)
Trac ticket: Core-63588 See #8999 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.
1 parent 40bc4f5 commit f36b8ee

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

src/wp-includes/blocks.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2407,6 +2407,25 @@ function do_blocks( $content ) {
24072407
$top_level_block_count = count( $blocks );
24082408
$output = '';
24092409

2410+
/**
2411+
* Parsed blocks consist of a list of top-level blocks. Those top-level
2412+
* blocks may themselves contain nested inner blocks. However, every
2413+
* top-level block is rendered independently, meaning there are no data
2414+
* dependencies between them.
2415+
*
2416+
* Ideally, therefore, the parser would only need to parse one complete
2417+
* top-level block at a time, render it, and move on. Unfortunately, this
2418+
* is not possible with {@see parse_blocks()} because it must parse the
2419+
* entire given document at once.
2420+
*
2421+
* While the current implementation prevents this optimization, it’s still
2422+
* possible to reduce the peak memory use when calls to `render_block()`
2423+
* on those top-level blocks are memory-heavy (which many of them are).
2424+
* By setting each parsed block to `NULL` after rendering it, any memory
2425+
* allocated during the render will be freed and reused for the next block.
2426+
* Before making this change, that memory was retained and would lead to
2427+
* out-of-memory crashes for certain posts that now run with this change.
2428+
*/
24102429
for ( $i = 0; $i < $top_level_block_count; $i++ ) {
24112430
$output .= render_block( $blocks[ $i ] );
24122431
$blocks[ $i ] = null;

0 commit comments

Comments
 (0)