Skip to content

Commit 20546a0

Browse files
committed
do_blocks(): Document transient-memory-leak optimization. (WordPress#9052)
Trac ticket: Core-63588 See WordPress#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 20546a0

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

src/wp-includes/blocks.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2398,6 +2398,7 @@ function parse_blocks( $content ) {
23982398
* Parses dynamic blocks out of `post_content` and re-renders them.
23992399
*
24002400
* @since 5.0.0
2401+
* @since 6.9.0 Reduces peak memory use by unsetting blocks after they render.
24012402
*
24022403
* @param string $content Post content.
24032404
* @return string Updated post content.
@@ -2407,6 +2408,25 @@ function do_blocks( $content ) {
24072408
$top_level_block_count = count( $blocks );
24082409
$output = '';
24092410

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

0 commit comments

Comments
 (0)