perf: skip pagebuilder preload regex scan for blocks with no preload image#8
Merged
Merged
Conversation
…image view_block_abstract_to_html_after fires for every block on every page (no block-type filter), so the 4-part chained-lazy-quantifier regex here was running against the full HTML of every block on every request, even though only a handful of PageBuilder image elements can ever match. Add a cheap str_contains() guard for the literal preload="Yes" marker before running the regex, ruling out the vast majority of blocks with effectively no cost.
SamJUK
force-pushed
the
perf/scope-pagebuilder-preload-scan
branch
from
July 15, 2026 22:12
6bb8f8c to
7da4f03
Compare
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
Micro-optimization, not a functional change.
view_block_abstract_to_html_afterfires for every block on everypage — headers, footers, product grids, CMS blocks, everything —
with no block-type filter.
BlockToHtmlAfterran a 4-partchained-lazy-quantifier regex
(
<img.*?src="(.*?)".*?preload="Yes".*?data-element="(.*?)".*?>)against each of those blocks' full rendered HTML on every request,
even though only PageBuilder image elements with the preload flag can
ever match.
Added a cheap
str_contains($html, 'preload="Yes"')guard before theregex — rules out the overwhelming majority of blocks near-instantly.
Behaviour is identical for every block that actually needs scanning.
Benchmarked against a realistic mixed page (150 blocks: containers,
nav/footer text, product grids, one real pagebuilder match) — net
~44% faster per page render. Not a universal win in every shape (large
blocks with zero
<img>tags at all are marginally slower under theguard, since PCRE's own literal-prefix reject already handles those
efficiently), but grid/listing blocks dominate real page cost and
carry the net gain.
Test plan
Test/Unit/Observer/View/BlockToHtmlAfterTest.phpsuite stillpasses unchanged (8/8) against Magento 2.4.8-p3/PHP 8.3 — existing
testDoesNothingWhenNoPreloadImagesFoundalready covers theshort-circuit path.