Block Supports: Bail early in state styles when a block has no style attribute - #77
Draft
mukeshpanchal27 wants to merge 4 commits into
Draft
Block Supports: Bail early in state styles when a block has no style attribute#77mukeshpanchal27 wants to merge 4 commits into
mukeshpanchal27 wants to merge 4 commits into
Conversation
…attribute. `wp_render_block_states_support()` is registered on `render_block`, so it runs once per block on every request. Before determining that a block has no state styles, it performed a block type registry lookup, resolved global settings via `wp_get_global_settings()`, and computed responsive media queries through `WP_Theme_JSON::get_viewport_media_queries()`. Every CSS rule the function can produce is keyed off the block's `style` attribute, and the function already returns the content unchanged when no rules are generated. Bailing out as soon as `style` is absent is therefore behaviour-preserving, and skips that work for the overwhelming majority of blocks, which carry no `style` attribute at all. Measured on trunk with a `core/paragraph` block that has no `style` attribute: 1396.6 ns/block before, 100.4 ns/block after. Output verified byte-identical across styled and unstyled fixtures for `core/button`, `core/navigation-link`, and `core/paragraph`, plus empty-style, no-attrs, and unregistered-block cases. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Updates the prompt builder constructor and proxied method calls to catch all `Throwable` instances. This ensures errors such as `TypeError` are converted to `WP_Error` objects instead of causing fatal errors. Includes regression tests for throwable conversion and fluent method forwarding behavior. Props khokansardar, gziolo, alaminfirdows, shanemuir, vedantere, r1k0, waneezashafiq32, ugyensupport, dhavalkapadane. Fixes #65505. git-svn-id: https://develop.svn.wordpress.org/trunk@63346 602fd350-edb4-49c9-b593-d223f7449a82
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.
What
Adds an early return to
wp_render_block_states_support()when the block has nostyleattribute.Why
wp_render_block_states_support()is registered onrender_block:so it executes once per block, per request. Before it can determine that a block has no state styles, it does all of this unconditionally:
The overwhelming majority of blocks on a page carry no
styleattribute, so all of this is discarded.The
viewportlookup is particularly costly. Core'stheme.jsondefines noviewportkey, andwp_get_global_settings()ends with_wp_array_get( $settings, $path, $settings )— so a missing path returns the entire settings array. That array is then handed tosanitize_viewport_settings(), which finds nomobile/tabletmembers, discards it, and rebuildsDEFAULT_VIEWPORT_BREAKPOINTS, running twopreg_matchvalidations and building two media-query strings — for every block, on every request.How
$styleis moved above the registry lookup and the guard added. Nothing else changes.Why this is safe
Every write into
$css_rulesin this function is keyed off$style:empty( $style[ $pseudo_state ] )empty( $style[ $breakpoint ] )$style[ $breakpoint ][...]and the function already short-circuits with the content unchanged when nothing was produced:
So when
$styleis absent, the pre-existing code path was guaranteed to fall through to that return. The new guard reaches the same outcome without the intervening work.Testing
Existing suites — all green (MySQL 8.4, PHP 8.3.2):
tests/phpunit/tests/block-supports/states.phptests/phpunit/tests/block-supports/layout(53),typography(81),duotone(30),block-visibility(14)tests/phpunit/tests/blocks/states.phpwas also run against unmodified trunk first to confirm the 46 tests were green before the change, not just after.Output equivalence. Ran a fixture matrix through
wp_render_block_states_support()on trunk and on this branch, comparing md5 of the returned HTML:core/buttonwith:hover985cbd40…985cbd40…core/buttonwith@mobile14e0f950…14e0f950…core/navigation-linkwith:focus4e7eb3ad…4e7eb3ad…core/paragraphstyledbdac64ce…bdac64ce…stylearraybdac64ce…bdac64ce…attrsbdac64ce…bdac64ce…bdac64ce…bdac64ce…Byte-identical in every case.
Performance. Measured inside a booted WordPress, 20k iterations, warmed,
core/paragraphwith nostyleattribute:~1.30 µs saved per style-less block (~14× on this path).
Context
This came out of a 7.0.4 → 7.1 RC3 benchmark comparison showing a server-side regression on both Block and Classic themes (Block
wp-totalp50 +9.88% / +10.53 ms; Classic +7.92% / +3.99 ms), withLCP − TTFBflat on both — i.e. entirely PHP execution time, not front-end or database. This filter was the largest single contributor identified.Related follow-ups worth separate PRs, not included here to keep this reviewable:
wp_get_global_settings( array( 'viewport' ) )andget_viewport_media_queries()return the same value for every block in a request and could be memoized once._wp_array_get()fallback inwp_get_global_settings()so a missing path returnsnullrather than the whole settings tree. This looks like a latent correctness bug independent of performance._wp_register_default_icons()registers 88 icons plus 88_x()calls oninitfor every front-end request (~0.10 ms), none of which is used to render a front-end page.