Repository navigation
perf(core): cache site:* settings prefix scan across requests - #839
Conversation
Site settings change rarely but were re-fetched on every public page load. A globalThis-scoped holder caches the resolved settings until the next site:* write bumps its version, dropping one prefix-scan query per public route. Cross-isolate staleness is bounded by isolate lifetime.
OptionsRepository.setMany iterates row-by-row without a transaction. If a write fails mid-loop, earlier rows are committed but invalidateSiteSettingsCache() never ran — leaving the globalThis cache pointing at pre-write data while the DB has post-write data on some rows. Wrapping the call in try/finally guarantees invalidation runs whether the write succeeded or threw. Adversarial-review finding from PR #800-class perf branch.
Deploying with
|
| Status | Name | Latest Commit | Updated (UTC) |
|---|---|---|---|
| ✅ Deployment successful! View logs |
emdash-perf-coordinator | 9f841d1 | Apr 29 2026, 09:53 PM |
🦋 Changeset detectedLatest commit: 9f841d1 The changes in this PR will be included in the next version bump. This PR includes changesets to release 13 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
Query-count snapshot changes24 routes changed, total Δ -24 queries. SQLite
D1
Comparing snapshot files between base and head. Updated automatically on each push. |
Deploying with
|
| Status | Name | Latest Commit | Updated (UTC) |
|---|---|---|---|
| ✅ Deployment successful! View logs |
emdash-i18n | 9f841d1 | Apr 29 2026, 09:53 PM |
Deploying with
|
| Status | Name | Latest Commit | Updated (UTC) |
|---|---|---|---|
| ✅ Deployment successful! View logs |
docs | 9f841d1 | Apr 29 2026, 09:54 PM |
Deploying with
|
| Status | Name | Latest Commit | Updated (UTC) |
|---|---|---|---|
| ✅ Deployment successful! View logs |
emdash-playground | 9f841d1 | Apr 29 2026, 09:55 PM |
Deploying with
|
| Status | Name | Latest Commit | Updated (UTC) |
|---|---|---|---|
| ✅ Deployment successful! View logs |
emdash-demo-cache | 9f841d1 | Apr 29 2026, 09:56 PM |
@emdash-cms/admin
@emdash-cms/auth
@emdash-cms/blocks
@emdash-cms/cloudflare
emdash
create-emdash
@emdash-cms/gutenberg-to-portable-text
@emdash-cms/x402
@emdash-cms/plugin-ai-moderation
@emdash-cms/plugin-atproto
@emdash-cms/plugin-audit-log
@emdash-cms/plugin-color
@emdash-cms/plugin-embeds
@emdash-cms/plugin-forms
@emdash-cms/plugin-webhook-notifier
commit: |
|
/review |
|
LGTM! |
Adds the query-dump harness and analysis scripts that produced the catalogue used to drive PRs #838, #839, #840: - scripts/query-counts-dump.mjs — sibling of query-counts.mjs that emits per-route × phase NDJSON dumps under scripts/query-dumps/. - scripts/build-perf-d1.mjs — standalone "build the perf fixture for d1" wrapper, useful when iterating with the dump harness without re-running the full counts harness. - scripts/query-dumps/{classify,cold-only,inspect-other}.mjs — analysis helpers for slicing the dumps. classify.mjs writes a markdown classification report; cold-only.mjs surfaces the d1 cold-isolate tax; inspect-other.mjs prints distinct SQL for a class. - scripts/query-dumps/README.md — workflow doc. The dump JSON itself and the generated classification.{target}.md reports stay gitignored — they're analysis artefacts that regenerate from the harness in seconds. The query-dumps analysis scripts are excluded from oxlint (one-off tooling, not production code).
What does this PR do?
Adds a
globalThis-keyed isolate-wide cache for thesite:*settings prefix scan. The catalogue showedSELECT name, value FROM options WHERE name LIKE 'site:%'firing once per public read on every route — the cache reduces that to once per isolate lifetime. Cross-isolate staleness is bounded by isolate lifetime (minutes on workerd), which is acceptable for chrome (site title, tagline, logo, SEO defaults).Implementation in
packages/core/src/settings/index.ts:Symbol.for("emdash:site-settings")onglobalThis, storing{ cached: Promise | null, version: number }.getSiteSettings()checks the holder; on hit, returns the cached promise (no DB).getSiteSettingsWithDb(), populates both the request-cache promise wrapper and the globalThis holder.setSiteSettings()callsinvalidateSiteSettingsCache()which bumps the version and clearsholder.cached.holder.cachedstill references the failed promise — robust against in-flight overwrite.The invalidation is wrapped in
try/finallyso a partial-write failure insideOptionsRepository.setMany(which iterates row-by-row, non-transactionally) still drops the cache. Without this, a mid-loop throw would leave the cache pointing at pre-write data while the DB has post-write data on some rows.Snapshot diff (sqlite, fixtures/perf-site)
Every public read drops 1 query in both cold and warm phases:
-16 queries per harness run. The d1 snapshot will be refreshed by the
query-counts-applyworkflow.Tests added
Five tests in
packages/core/tests/unit/settings/settings.test.tsusing a real in-memory SQLite + Kyselyloghook to count actual SQL — no mocks:getSiteSetting()does not hit the DB aftergetSiteSettings()in the same request.setSiteSettings()invalidates the globalThis cache.setSiteSettings()invalidates the cache even when the write throws (covers the partial-write failure path).invalidateSiteSettingsCache()drops the cached value (manual-invalidation path).Write-path audit
Verified by exhaustive grep of
packages/: onlysetSiteSettings()writes thesite:*prefix. Other namespaces (emdash:*,plugin:<id>:*,site_*snake-case in WordPress import) don't invalidate this cache and shouldn't.invalidateSiteSettingsCacheis internal — not exported frompackages/core/src/index.ts.Type of change
Checklist
pnpm typecheckpassespnpm lintpassespnpm testpasses (or targeted tests for my change)pnpm formathas been runmessages.pochanges except in translation PRs — a workflow extracts catalogs on merge tomain.AI-generated code disclosure