Skip to content

Read rendered pages once for block comments and component attributes - #193

Merged
dnnsjsk merged 1 commit into
inline0:mainfrom
Contributolo:perf/render-pipeline
Nov 2, 2026
Merged

dnnsjsk merged 1 commit into
inline0:mainfrom
Contributolo:perf/render-pipeline

Conversation

@Contributolo

@Contributolo Contributolo commented Oct 28, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Three places in the render pipeline do work per registered block or per field that does not depend on what the page contains. On a page with many blocks that adds up to a few milliseconds per uncached request. This PR makes them scale with what is actually on the page. The rendered output is byte-identical.

Changes

Assets::parse_output()

  • It called stripos() and strpos() on the whole page for every registered block, then str_replace() with all block comments over the whole page.
  • It now collects the block comments on the page with one preg_match_all() (Block::find_comments()), decides per block with an isset() lookup, and removes the comments with Block::strip_comments().
  • strip_comments() uses the same comment pattern with preg_replace_callback() and removes only comments of registered blocks. Unregistered <!--blockstudio/x--> comments stay in the output, as before. This also makes the page buffer and bs_strip_render_markers() share one implementation, which the docblock of strip_comments() already described as the intent.

Block::replace_components()

  • It ran replace_custom_tag() (one regex over the content) for every field with an id, although the markup usually names one or two fields in <RichText attribute="...">. It also ran the InnerBlocks replacement for every block.
  • It now reads the attribute values of the <RichText> tags once and only replaces those fields. The loop order and the existing handling of path attributes (attribute="items[0].text") are unchanged. The InnerBlocks replacement runs only when <InnerBlocks is in the content.
  • The DOM cleanup checked every element for useblockprops and then for each of the 17 names in the cleanup list. It now skips elements that carry none of the 17 attribute names (hasAttributes() plus one pass over the element's attributes) and runs the existing logic for the others.

Not changed on purpose: the DOMDocument pass itself. Replacing it with WP_HTML_Tag_Processor or DOMXPath was slower in my tests (PHP code against libxml in C), so the cleanup keeps the DOM and only avoids per-element work.

Behaviour notes

  • Output is byte-identical on the pages below (compared with cmp, same cache state before and after).
  • The block comment is matched case-sensitively in the presence check, like the removal and the asset checks already were. Before, the "block is on the page" check used stripos(), the others used strpos(). Block names are lowercase, so this only matters for hand-written upper-case comments.
  • One edge case differs. If the replacement text of one RichText field itself contained <RichText attribute="other">, the old loop could replace that tag when it reached the other field later. The new code decides which fields to replace before replacing anything, so it does not. That was a side effect of replacing inside content that is already user data, not a documented behaviour.

Measurements

Setup: WordPress 7.1.3 with the SQLite integration, PHP 8.5.12 with opcache, WP_ENVIRONMENT_TYPE=production (Blockstudio caches as on a live site), no page cache, each request in its own forked process. Two pages built from section blocks: 25 blocks (75 KB HTML) and 66 blocks (128 KB HTML). 9 rounds of ABAB (order flips every round), 15 requests per variant and round after 3 warm-up requests, medians per round, then the median of the paired differences. The "Blockstudio" rows come from the built-in profiler (?blockstudio-perf=1): assets is parse_output(), components is the phase:components group.

Page Metric Before After Paired difference Faster in
25 blocks Page total 35.98 ms 34.64 ms -1.34 ms 9/9 rounds
25 blocks Blockstudio total 30.91 ms 29.66 ms -1.25 ms 8/9 rounds
25 blocks parse_output() 1.46 ms 0.82 ms -0.65 ms 9/9 rounds
25 blocks components phase 4.44 ms 3.72 ms -0.72 ms 9/9 rounds
66 blocks Page total 47.47 ms 44.91 ms -2.36 ms 8/9 rounds
66 blocks Blockstudio total 42.47 ms 39.65 ms -2.63 ms 9/9 rounds
66 blocks parse_output() 2.33 ms 0.90 ms -1.43 ms 9/9 rounds
66 blocks components phase 8.98 ms 7.77 ms -1.20 ms 9/9 rounds

That is roughly 4 to 5 percent of an uncached page here. The gain grows with page size and with the number of registered block types, and a page without Blockstudio blocks is not affected.

Micro-benchmark behind the parse_output() change (128 KB page, 28 registered block types, 300 repetitions): stripos() plus strpos() for 28 blocks 0.36 ms, str_replace() with 28 comments 1.65 ms, one preg_match_all() 0.012 ms, one preg_replace() 0.017 ms.

Tests

New:

  • BlockReplaceComponentsTest: RichText for fields named in the markup, several fields in one block, fields declared but not in the markup, fields in the markup but not declared, richtext typed fields, path attributes (items[1].text), InnerBlocks next to RichText, no InnerBlocks tag in the content, and for each of the 16 cleanup attributes that it is removed from a normal element and kept on input and textarea, plus elements without any component attribute staying untouched.
  • RenderMarkersTest: a registered comment after an unclosed <!--blockstudio/ comment is still removed, a registered comment between unregistered ones, find_comments() returns each distinct comment once and ignores other markup.
  • AssetsTest: parse_output() keeps comments of unregistered blocks and removes every occurrence of a registered one, and prints block assets only when the block's comment is on the page.

All new tests also pass on the unchanged code of 7.7.10, so they pin the existing behaviour.

I ran the focused PHPUnit classes RenderMarkersTest, AssetsTest, BlockTest, BlockUseBlockPropsMarkerTest, BlockAllowedBlocksTest, BlockReplaceComponentsTest, BlockTagsRichTextRoutingTest, BlockTagsAttributeForwardingTest, RenderTest and BsuiButtonTest against a WordPress and SQLite install with the test theme, and phpcs on the two changed classes (clean). Two failures in AssetsTest (Bootstrap SCSS needs node_modules) and BlockTagsAttributeForwardingTest (the Twig templates need the Composer dependencies of the test theme) come from that local setup and are identical on 7.7.10 without this change. The full suites and the E2E tests are left to CI.

parse_output() searched the whole page twice per registered block and then
ran one str_replace over every block comment. It now collects the comments
on the page in a single pass, decides per block with an array lookup and
removes them with Block::strip_comments(), which only touches comments of
registered blocks. Unregistered comments stay in the output.

replace_components() ran one search per block field to find its RichText
tag, and one for InnerBlocks, whether or not the markup contained them. It
now reads the attribute names of the RichText tags once, replaces only those
fields, and calls the InnerBlocks replacement only when the tag is present.
The DOM cleanup skips elements that carry none of the component attributes
instead of checking each element for all of them.

The rendered output is unchanged.
@dnnsjsk
dnnsjsk merged commit 2d02366 into inline0:main Nov 2, 2026
@dnnsjsk

dnnsjsk commented Nov 2, 2026

Copy link
Copy Markdown
Contributor

Merged and released in Blockstudio 7.7.11. The full CI and distribution checks passed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants