Skip to content

Store the options read on every request autoloaded - #207

Merged
dnnsjsk merged 1 commit into
inline0:mainfrom
Contributolo:perf/autoload-request-options
Nov 11, 2026
Merged

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

Conversation

@Contributolo

Copy link
Copy Markdown
Collaborator

Closes #204.

Since 7.7.6 one wp_prime_option_caches() call loads the seven options Blockstudio reads on every request. Without a persistent object cache that query still ran on every request, because the options were stored with autoload off or did not exist. With this change a migrated site has all seven among the autoloaded options, so priming returns before the query.

What changes

Option Before Now
blockstudio_build_lock_cleanup_version, blockstudio_collection_post_types_signature autoload false autoload true
blockstudio_populate_cache_version autoload false autoload true, false with a persistent object cache
blockstudio_pages_collection_manifests, blockstudio_pages_template_for autoload false no autoload argument: WordPress autoloads them up to wp_max_autoloaded_option_size
blockstudio_static_prerender_feature_state deleted while inactive kept as an empty array while inactive
all seven several usually missing stored once with the value their reader falls back to

The migration 7.7.14 does the one-time part: it switches the small existing rows with wp_set_options_autoload(), adds the two page options again so that WordPress decides by size, and stores every option that does not exist yet. First activation stores them as well. The priming call stays as the fallback for what is still not autoloaded.

Where this differs from the proposal in #204

  1. More options are missing than the two the issue named. blockstudio_pages_template_for only exists after an explicit page sync, blockstudio_populate_cache_version only after a content write with populate blocks, and the cleanup version not at all with cache/enabled off. One missing row is enough to keep the query. So the migration stores all seven, each with the value its reader already uses as the default (array(), '', '0'), which reads the same as no option.
  2. The three small options get true instead of null. They are a version string, a hash and a three-key array and cannot grow. With null, update_option() runs an extra SELECT autoload on every write to re-evaluate the size.
  3. The populate state is not autoloaded with a persistent object cache (Build_Cache::autoload_populate_state()). It is written on every content change, and each write of an autoloaded option replaces the whole cached alloptions entry. Such a site has the option in its object cache anyway and gains nothing from autoloading it.
  4. The two page options are added again instead of switched. wp_set_options_autoload() can only store on or off, and on would switch off the size check for exactly the two rows that can get large. delete_option() plus add_option() stores auto, as for a new row, and later writes keep re-evaluating it. Between the two calls a parallel request sees no row once: it reads no templates for that request and rescans the manifests.
  5. The prerender feature state keeps its row. reconcile_feature_state() stored nothing for an inactive site and deleted the row when a feature was switched off, so it was missing on most sites. It now stores an empty array in that case, which is what the read falls back to. deactivate() still deletes it. Two assertions that expected the option to be gone after a switch-off now expect the empty array.

Notes

  • The migration is keyed 7.7.14, the next version according to the open release branch. It has to carry the version this ships in.
  • Migrate runs on plugins_loaded. When Blockstudio is loaded after that hook has fired, from a theme's Composer autoload, the callback is never called, as today for the 7.6.6 migration. Those sites keep the one query, nothing else changes for them. I can send a separate pull request that lets the migrations run in that case, if you want that.
  • Four assertions in MigrateTest expected blockstudio_settings not to exist after a migration; it now exists as an empty array. Both readers use get_option( 'blockstudio_settings', array() ).
  • Left out on purpose: switching autoload off again on deactivation.

Tests

  • OptionCachePrimingTest: with all seven options missing, one query (the fallback, as before). After the migration, a request reads all of them without any option query, and again after every writer has stored a real value. With a persistent object cache the populate state is not autoloaded.
  • MigrateTest: existing rows with autoload off are autoloaded afterwards with their values intact, in the caches of the running request and for a new request. A 200 KB template option stays out of the autoloaded options. Missing options are stored with their read defaults. Existing settings are kept. First activation stores the options.
  • Against main, the new priming test and the new and changed MigrateTest cases fail; with the change they pass.
  • Run locally in WordPress Playground with the test theme: the two files above and tests/unit/runtime, build, pages and settings. Those four directories give the same results with and without the change, apart from the two changed assertions in runtime. PHPCS and PHPStan are clean on the changed files. The full suites are left to CI as per AGENTS.md.

The seven options primed at the start of a request were stored with
autoload off or did not exist, so without a persistent object cache the
priming query ran on every request.

The small options are now written autoloaded, the two page options
without an autoload value so that WordPress decides by size, and an
inactive prerender feature state stays as an empty array. The 7.7.14
migration switches existing rows and stores every missing option with
the value its reader falls back to. The populate state is not autoloaded
with a persistent object cache, where it is written too often for that.

Closes inline0#204.
@dnnsjsk

dnnsjsk commented Nov 11, 2026

Copy link
Copy Markdown
Contributor

Merged and released in Blockstudio 7.7.14, integration commit 823a2fd. The integration additionally migrates page-option autoload metadata without deleting values, reevaluates the size policy when configurations grow or shrink, and runs migrations for late-loaded theme Composer packages. Regression coverage includes those paths, option query counts, and persistent-cache behavior. Full CI and distribution checks pass. Thanks for the contribution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Remaining per-request options query after priming (options are not autoloaded)

2 participants