Skip to content

perf: load per-request options with a single query - #152

Merged
dnnsjsk merged 1 commit into
inline0:mainfrom
Contributolo:perf/prime-options
Oct 22, 2026
Merged

dnnsjsk merged 1 commit into
inline0:mainfrom
Contributolo:perf/prime-options

Conversation

@Contributolo

Copy link
Copy Markdown
Collaborator

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 the blockstudio_editor_expected_capture_assets_id transient 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 of Plugin::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 in notoptions, so later get_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: OptionCachePrimingTest clears the option caches, then calls the real readers and asserts that they cost a single query (7 before this change). AdminFrontendCaptureTest checks 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.

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.
@dnnsjsk
dnnsjsk merged commit 7b0a3c6 into inline0:main Oct 22, 2026
@dnnsjsk

dnnsjsk commented Oct 22, 2026

Copy link
Copy Markdown
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.

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.

2 participants