Skip to content

perf: skip pagebuilder preload regex scan for blocks with no preload image#8

Merged
SamJUK merged 1 commit into
masterfrom
perf/scope-pagebuilder-preload-scan
Jul 15, 2026
Merged

perf: skip pagebuilder preload regex scan for blocks with no preload image#8
SamJUK merged 1 commit into
masterfrom
perf/scope-pagebuilder-preload-scan

Conversation

@SamJUK

@SamJUK SamJUK commented Jul 15, 2026

Copy link
Copy Markdown
Owner

Summary

Micro-optimization, not a functional change.

view_block_abstract_to_html_after fires for every block on every
page
— headers, footers, product grids, CMS blocks, everything —
with no block-type filter. BlockToHtmlAfter ran a 4-part
chained-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 the
regex — 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 the
guard, 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

  • Full Test/Unit/Observer/View/BlockToHtmlAfterTest.php suite still
    passes unchanged (8/8) against Magento 2.4.8-p3/PHP 8.3 — existing
    testDoesNothingWhenNoPreloadImagesFound already covers the
    short-circuit path.

…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
SamJUK force-pushed the perf/scope-pagebuilder-preload-scan branch from 6bb8f8c to 7da4f03 Compare July 15, 2026 22:12
@SamJUK
SamJUK merged commit b5edd43 into master Jul 15, 2026
12 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.

1 participant