Repository navigation
perf: load per-request options with a single query - #152
Merged
Merged
Conversation
Seven options read on every request are not autoloaded or usually do not exist, so each read was its own query without an object cache. Prime them with one wp_prime_option_caches() call at the start of Plugin::init(), and return before reading the capture transient when the request has no capture id.
Contributor
|
Included in Blockstudio 7.7.6, now officially released. This PR is merged into main as part of the reviewed release, with full CI and distribution checks passing for the release commit. |
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.
Every front-end request without a persistent object cache runs eight separate option queries for Blockstudio. Seven options are read on each request, and either they are stored without autoload or they usually don't exist:
blockstudio_settings(Settings::load_settings_from_options, settings.php:431)blockstudio_static_prerender_feature_state(Static_Prerender_Runtime::reconcile_feature_state, static-prerender-runtime.php:2656)blockstudio_build_lock_cleanup_version(Runtime_Cache::init, runtime-cache.php:147)blockstudio_pages_collection_manifests(Pages::persisted_collection_manifests, pages.php:1203)blockstudio_collection_post_types_signature(Pages::maybe_flush_collection_rewrite_rules, pages.php:804)blockstudio_populate_cache_version(Build_Cache::get_populate_state, build-cache.php:363)blockstudio_pages_template_for(Pages::template_for_configuration, pages.php:1556)Missing options are no exception: WordPress only remembers that an option does not exist after it has asked the database once. The eighth query comes from
Admin::capture_frontend_assets()(admin.php:210), which reads theblockstudio_editor_expected_capture_assets_idtransient on every page, even when the request has no capture id and the function is about to return anyway.This PR primes the seven options with one
wp_prime_option_caches()call at the start ofPlugin::init(), before the first read. The function is available since WordPress 6.4, and Blockstudio requires 6.7. It skips options that are already cached and records missing ones innotoptions, so laterget_option()calls make no query. It also moves the empty capture id check in front of the transient read, which does not change behavior. Autoload stays as it is: three of these options normally don't exist, so autoload would not help them.Measured with WordPress Playground (WP 7.1.3, PHP 8.4, no object cache), viewing a block-heavy page anonymously: Blockstudio option queries drop from 8 to 1, and total queries per request from 79 to 72. In wp-admin and the block editor they drop from 8 to 2; the second query is the updater's own option. The rendered HTML is byte-identical.
Tests:
OptionCachePrimingTestclears the option caches, then calls the real readers and asserts that they cost a single query (7 before this change).AdminFrontendCaptureTestchecks that a normal front-end view does not read the capture transient, and that a matching capture id still stores the captured assets while a wrong id stores nothing.