perf: eliminate redundant D1 settings queries per API request#1
Merged
perf: eliminate redundant D1 settings queries per API request#1
Conversation
- 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
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
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.
getSettings()was being called twice per request — once inrequireApiAuthmiddleware and again in each route handler — and each call issued 6 sequential D1 round trips.Changes
src/settings.ts— batch reads and writesgetSettings: replace 6×dbFirstwith singledbAllusingWHERE key IN (...)+Maplookup — 6 D1 round trips → 1saveSettings: replace 6×dbRunwithenv.DB.batch()— 6 D1 writes → 1src/auth.ts+src/routes/openai.ts— cache settings in request contextStore settings in Hono context after the single fetch in
requireApiAuth; route handlers read from context instead of re-querying.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.