Repository navigation
Read rendered pages once for block comments and component attributes - #193
Merged
Merged
Conversation
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.
Contributor
|
Merged and released in Blockstudio 7.7.11. The full CI and distribution checks passed. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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()stripos()andstrpos()on the whole page for every registered block, thenstr_replace()with all block comments over the whole page.preg_match_all()(Block::find_comments()), decides per block with anisset()lookup, and removes the comments withBlock::strip_comments().strip_comments()uses the same comment pattern withpreg_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 andbs_strip_render_markers()share one implementation, which the docblock ofstrip_comments()already described as the intent.Block::replace_components()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 theInnerBlocksreplacement for every block.attributevalues 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. TheInnerBlocksreplacement runs only when<InnerBlocksis in the content.useblockpropsand 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
DOMDocumentpass itself. Replacing it withWP_HTML_Tag_ProcessororDOMXPathwas slower in my tests (PHP code against libxml in C), so the cleanup keeps the DOM and only avoids per-element work.Behaviour notes
cmp, same cache state before and after).stripos(), the others usedstrpos(). Block names are lowercase, so this only matters for hand-written upper-case comments.<RichText attribute="other">, the old loop could replace that tag when it reached theotherfield 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):assetsisparse_output(),componentsis thephase:componentsgroup.parse_output()parse_output()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()plusstrpos()for 28 blocks 0.36 ms,str_replace()with 28 comments 1.65 ms, onepreg_match_all()0.012 ms, onepreg_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,richtexttyped fields, path attributes (items[1].text),InnerBlocksnext to RichText, noInnerBlockstag in the content, and for each of the 16 cleanup attributes that it is removed from a normal element and kept oninputandtextarea, 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,RenderTestandBsuiButtonTestagainst a WordPress and SQLite install with the test theme, andphpcson the two changed classes (clean). Two failures inAssetsTest(Bootstrap SCSS needsnode_modules) andBlockTagsAttributeForwardingTest(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.