Skip to content

perf: eliminate redundant D1 settings queries per API request#1

Merged
LNemo05 merged 3 commits intomainfrom
copilot/optimize-code-reduce-cpu-time
Mar 1, 2026
Merged

perf: eliminate redundant D1 settings queries per API request#1
LNemo05 merged 3 commits intomainfrom
copilot/optimize-code-reduce-cpu-time

Conversation

Copy link

Copilot AI commented Mar 1, 2026

getSettings() was being called twice per request — once in requireApiAuth middleware and again in each route handler — and each call issued 6 sequential D1 round trips.

Changes

src/settings.ts — batch reads and writes

  • getSettings: replace 6× dbFirst with single dbAll using WHERE key IN (...) + Map lookup — 6 D1 round trips → 1
  • saveSettings: replace 6× dbRun with env.DB.batch() — 6 D1 writes → 1

src/auth.ts + src/routes/openai.ts — cache settings in request context

Store settings in Hono context after the single fetch in requireApiAuth; route handlers read from context instead of re-querying.

// auth.ts — fetch once, store in context
const settings = await getSettings(c.env);
c.set("settingsBundle", settings);

// openai.ts — read from context, no DB hit
const settingsBundle = c.get("settingsBundle");

Net effect: settings cost per API request drops from 12 D1 queries to 1.


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

- getSettings: replace 6 sequential dbFirst calls with a single dbAll
  query using WHERE key IN (...), cutting D1 round trips from 6 to 1
- saveSettings: replace 6 sequential dbRun calls with db.batch(),
  cutting D1 round trips from 6 to 1
- Remove unused dbFirst/dbRun imports from settings.ts

Co-authored-by: LNemo05 <192667997+LNemo05@users.noreply.github.com>
Copilot AI changed the title [WIP] Optimize code to reduce CPU time perf: batch settings DB queries to reduce Cloudflare CPU time Mar 1, 2026
…ries

requireApiAuth already fetches settings once per request. Every route
handler in openai.ts was calling getSettings(c.env) a second time,
wasting an extra D1 round trip on every API call.

Fix: store the settings in the Hono context variables after the first
fetch in requireApiAuth (c.set("settingsBundle", settings)), then have
route handlers read from c.get("settingsBundle") instead of re-querying.

- auth.ts: add SettingsBundle to Variables type; c.set after getSettings
- openai.ts: add SettingsBundle to router Variables type; replace 4x
  `await getSettings(c.env)` with `c.get("settingsBundle")`

Co-authored-by: LNemo05 <192667997+LNemo05@users.noreply.github.com>
Copilot AI changed the title perf: batch settings DB queries to reduce Cloudflare CPU time perf: reduce Cloudflare D1 round trips on every API request Mar 1, 2026
Copilot AI changed the title perf: reduce Cloudflare D1 round trips on every API request perf: eliminate redundant D1 settings queries per API request Mar 1, 2026
@LNemo05 LNemo05 marked this pull request as ready for review March 1, 2026 14:23
@LNemo05 LNemo05 merged commit 5037916 into main Mar 1, 2026
1 check passed
Copilot stopped work on behalf of LNemo05 due to an error March 1, 2026 14:27
Copilot stopped work on behalf of LNemo05 due to an error March 1, 2026 14:28
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