Opened 5 weeks ago
Last modified 2 weeks ago
#66236 assigned defect (bug)
Block Hooks: Skip parsing and re-serializing post content in the_content when no blocks are hooked
| Reported by: | sboisvert | Owned by: | sboisvert |
|---|---|---|---|
| Priority: | normal | Milestone: | 7.2 |
| Component: | Editor | Version: | |
| Severity: | normal | Keywords: | has-patch has-unit-tests |
| Cc: | Focuses: | performance |
Description
Pages with a lot of block content are slow because, since 6.8, every the_content call parses and re-serializes the whole post even when no block is hooked, and the per-block Block Hooks visitors run on top of that.
[58291] (b39f58ec) returned early from apply_block_hooks_to_content() when nothing was hooked.
[59101] (2952e563, #61902) removed that early return so template parts still get the theme attribute. Parsing and serializing now always run, and the has_filter( 'hooked_block_types' ) check only selects the visitors (blocks.php:1171).
[60173] (e800c60e, #63287, in 6.8.1) added a temporary hooked_block_types filter inside apply_block_hooks_to_content_from_post_object() (blocks.php:1354-1356). That filter makes the has_filter() check always true on this path, so the per-block visitors always run.
The PRs for [60173] (8695, 8700) do not mention this, and it appears to be unintended.
Timing apply_block_hooks_to_content_from_post_object() on ~1 MB of block markup with nothing hooked (PHP 8.4):
trunk: 34-35 ms per call
with the patch: 0.4 ms per call
Steps to reproduce
In the PHPUnit test environment on trunk 0004f4d2, with no plugins and no hooked blocks, create a post with ~1 MB of nested paragraph, group and columns blocks.
Time repeated calls of apply_block_hooks_to_content_from_post_object( $post->post_content, $post ).
Expected: the content is not parsed when nothing is hooked. Actual: it is parsed and re-serialized on every call.
Proposed fix
Return the content unchanged at the top of apply_block_hooks_to_content_from_post_object(), before the temporary filter is added, when:
get_hooked_blocks() is empty and has_filter( 'hooked_block_types' ) is false;
has_block( 'core/template-part', $content ) is false;
$ignored_hooked_blocks_at_root is null (REST passes an empty array, which keeps the full path, #65008).
Output is unchanged for well-formed content. Content with a stray closing delimiter renders as it did before 6.8. (Since 6.8, the temporary wrapper block has been making the parser recover and render those broken blocks anyway, and the patch removes that recovery whenever it skips the pass.)
The patch adds one test, test_apply_block_hooks_to_content_from_post_object_returns_content_unchanged_if_no_hooked_blocks, to applyBlockHooksToContentFromPostObject.php. Run with php vendor/bin/phpunit --group block-hooks (SQLite).
AI disclosure:
AI assistance: Yes
Tool(s): Claude Code, Codex CLI
Model(s): Claude Opus 5.5, GPT-6 Astra
Used for: root-cause analysis, git history checks, patch, test and benchmark drafting, accuracy review;
I reviewed the patch and the claims.
Attachments (1)
Change History (6)
#1
@
5 weeks ago
- Milestone Awaiting Review → 7.2
- Owner set to
- Status new → assigned
@sboisvert Thanks for the ticket and patch. Could you create a pull request with that patch? In general PRs are better than patches because they allow for CI jobs to run and make it easier to review.
This ticket was mentioned in PR #14022 on WordPress/wordpress-develop by @sboisvert.
5 weeks ago
#2
- Keywords has-patch has-unit-tests added
Pages with a lot of block content are slow because, since 6.8, every the_content call parses and re-serializes the whole post even when no block is hooked. Since [https://core.trac.wordpress.org/changeset/60173 [60173]] (6.8.1), the temporary hooked_block_types filter also makes the has_filter( 'hooked_block_types' ) check in apply_block_hooks_to_content() always true on this path, so the per-block Block Hooks visitors run on top of that.
Timing apply_block_hooks_to_content_from_post_object() on ~1 MB of block markup with nothing hooked (PHP 8.4): 34-35 ms per call on trunk, 0.4 ms with this change.
This change returns the content unchanged at the top of apply_block_hooks_to_content_from_post_object(), before the temporary filter is added, when all of these are true:
get_hooked_blocks()is empty andhas_filter( 'hooked_block_types' )is false;has_block( 'core/template-part', $content )is false;$ignored_hooked_blocks_at_rootis null (the REST API passes an empty array, so REST requests still get the full parse; see #65008).
Rendered output is unchanged, except for content that begins with a stray closing block delimiter. Since 6.8, the function wraps the post content in a temporary wrapper block before parsing, and that wrapper absorbs a leading stray closer, so the blocks after it render. With this change, such content renders as it did before 6.8.
Adds test_apply_block_hooks_to_content_from_post_object_returns_content_unchanged_if_no_hooked_blocks to applyBlockHooksToContentFromPostObject.php. Run with php vendor/bin/phpunit --group block-hooks.
Use of AI Tools
AI assistance: Yes
Tool(s): Claude Code, Codex CLI
Model(s): Claude Opus 5.5, GPT-6 Astra
Used for: root-cause analysis, git history checks, patch, test and benchmark drafting, accuracy review;
I reviewed the patch and the claims.
@rejaulalomkhan commented on PR #14022:
3 weeks ago
#3
Thanks for working on this, @sboisvert! The speedup is large, and keeping the template part and ignoredHookedBlocks cases in mind makes it safe. I tested it locally:
Patch Testing Report
Patch tested: https://github.com/WordPress/wordpress-develop/pull/14022
Environment
- WordPress: 7.2-alpha-63166-src
- PHP: 8.3.35
- Server: PHP CLI (PHPUnit 9.6.38)
- Database: MySQL 8.4.11
- Browser: N/A (unit tests)
- OS: macOS (wordpress-develop Docker environment)
- Theme: Twenty Twenty-Five
- MU Plugins: None activated
- Plugins: None activated
Steps taken
- Applied only the new test to current trunk → it fails ("Content should be returned unchanged."), as expected.
- Applied the full PR →
Tests_Blocks_ApplyBlockHooksToContentFromPostObjectpasses on single site and multisite; theblocks(random order),block-templates,block-hooksandrestapigroups pass. - Timed
apply_block_hooks_to_content_from_post_object()on a post with 1,000 paragraph blocks and nothing hooked: median 7.1 ms per call on trunk, 0.04 ms with the PR. - Replaced each of the guard's exception conditions with
true, one at a time. With throwaway tests, each one breaks real behaviour that works on trunk and with the PR:has_block( 'core/template-part', ... ): a template part in the content no longer gets itsthemeattribute.has_filter( 'hooked_block_types' ): a block hooked only through the filter (core/separatoraftercore/paragraph) is no longer inserted.null === $ignored_hooked_blocks_at_root:$ignored_hooked_blocks_at_rootis no longer filled from the post's_wp_ignored_hooked_blocksmeta.
- ✅ Patch is solving the problem: content is returned unchanged and unparsed when nothing is hooked, and the three exceptions behave as before.
Expected result
- When no blocks are hooked,
the_contentno longer parses and re-serializes the post content, without changing the template part andignoredHookedBlocksbehaviour.
Additional Notes
- None of the three mutations in step 4 is caught by the existing tests (
blocks,block-templatesandblock-hooksgroups all pass with each one). It might be worth adding one test per exception so a later refactor can't drop them silently. Happy to share the throwaway tests if they help. - AI assistance: Claude Code was used to run the tests; results reviewed by me.
#4
@
2 weeks ago
Test Report
Description
This report validates whether the indicated patch works as expected, on a running site rather than in the unit tests only.
Patch tested: https://github.com/WordPress/wordpress-develop/pull/14022 (head c2e966e)
Environment
- WordPress: 7.2-beta1-64606-src (wordpress-develop local environment in Docker)
- PHP: 8.3.32
- Server: nginx/1.31.3
- Database: MySQL 9.7.1
- Browser: none, requests made with curl
- OS: Windows 11 Pro
- Theme: Warqa (block theme with a
headertemplate part) - MU Plugins: one test-only helper (see Steps)
- Plugins: TestFlow Console 1.0.2 (nothing hooked:
get_hooked_blocks()is empty and there is nohooked_block_typesfilter)
Steps
- Create a published post with 1,000,680 bytes of block markup: 1,309 groups, each with columns, paragraphs and a heading.
- A test-only mu-plugin calls
apply_block_hooks_to_content_from_post_object()repeatedly onwp_loadedand records the time per call, the md5 of the output and what it contains. Three cases:- the post content with nothing hooked (20 calls per request);
- the same content with a
hooked_block_typesfilter that hookscore/separatoraftercore/heading(5 calls per request); <!-- wp:template-part {"slug":"header"} /-->on its own, to check thethemeattribute (200 calls per request).
- 5 requests per case. Apply the patch and repeat, then revert it and repeat as a control.
- Run the new test without the fix, and the
blockstest group without and with the patch.
Actual Results
Median time per call over 5 requests:
| Case | Before | With patch | Patch reverted (control) | Output, all three runs |
| 1 MB post, nothing hooked | 34.18 ms | 0.305 ms (99.1% less) | 33.96 ms | identical (same md5) |
| Same post with the separator hooked | 38.09 ms | 38.38 ms | 37.83 ms | identical, 1,309 separators inserted |
| Header template part only | 0.016 ms | 0.018 ms | 0.015 ms | identical, "theme":"warqa" added
|
Unit tests:
| Run | Result |
| New test on trunk (test file only) | fails: "Failed asserting that two strings are identical." |
blocks group, trunk | 729 tests, 1 failure |
blocks group, with patch | 730 tests, 1 failure (the same test) |
- ✅ Patch works as expected.
Additional Notes
- With nothing hooked, the patch skips the parse and serialize round trip, and the output is byte-identical to trunk for this content. The remaining 0.3 ms is mostly the
has_block( 'core/template-part', ... )check on the 1 MB string. - When a block is hooked, the early return does not apply: the hooked separators are inserted exactly as on trunk and the timing is unchanged.
- Template part content still gets its
themeattribute. - The one failure in the
blocksgroup,Tests_Blocks_GetHookedBlocks::test_loading_pattern_with_hooked_blocks, fails on trunk as well. The compared strings contain\r, so it comes from CRLF line endings in the fixture files of this Windows checkout, not from the patch. - Not tested: the
$ignored_hooked_blocks_at_rootpath on a running site (covered by the existing unit tests).
Thanks @sboisvert for the patch and the clear analysis.
![(please configure the [header_logo] section in trac.ini)](/chrome/site/your_project_logo.png)
patch