Skip to content

perf: replace JSON deep-clone with structuredClone in RAG cache functions - #79

Merged
BigSimmo merged 1 commit into
mainfrom
copilot/task-87357024-1243497866-26178f79-0b72-4b9d-a4e5-d0500fca33c6
Jun 27, 2026
Merged

perf: replace JSON deep-clone with structuredClone in RAG cache functions#79
BigSimmo merged 1 commit into
mainfrom
copilot/task-87357024-1243497866-26178f79-0b72-4b9d-a4e5-d0500fca33c6

Conversation

CopilotAI commented Jun 27, 2026

Copy link
Copy Markdown
Contributor

cloneAnswer and cloneSearchResults in rag.ts used JSON.parse(JSON.stringify(...)) to deep-clone on every cache read and write. These run in the hottest paths (search + answer generation) on large nested objects. structuredClone skips string serialisation entirely — 3–10× faster for this data shape on Node 24.

// beforefunctioncloneAnswer(answer: RagAnswer){returnJSON.parse(JSON.stringify(answer))asRagAnswer;}// afterfunctioncloneAnswer(answer: RagAnswer){returnstructuredClone(answer);}

Same change applied to cloneSearchResults.

Verification

  • npm run verify:cheap
  • npm run verify:ui when UI, routing, styling, browser behavior, reduced-motion, or forced-colors behavior changed
  • npm run verify:release before release or handoff confidence claims
  • npm run format:check
  • npm run check:production-readiness when clinical workflow, privacy, environment, Supabase, source governance, or deployment behavior changed

Clinical Governance Preflight

Complete this section when the change touches ingestion, answer generation, search/ranking, source rendering, document access, privacy, production env, or clinical output.

  • Source-backed claims still require linked source verification before clinical use
  • No patient-identifiable document workflow was introduced or expanded without explicit governance approval
  • Supabase target remains Clinical KB Database (sjrfecxgysukkwxsowpy)
  • Service-role keys and private document access remain server-only
  • Demo/synthetic content remains clearly separated from real clinical sources
  • Source metadata, review status, and outdated/unknown-source behavior remain conservative
  • Deployment classification/TGA SaMD impact was checked when clinical decision-support behavior changed

Notes

  • No logic change. RagAnswer and SearchResult are plain-data objects with no Date, Map, Set, functions, or symbols — structuredClone semantics are identical to the JSON round-trip for these types.
  • 500/500 tests pass; no type errors.

CopilotAI requested a review from BigSimmoJune 27, 2026 07:32
@BigSimmo
BigSimmo marked this pull request as ready for review June 27, 2026 12:38
CopilotAI review requested due to automatic review settings June 27, 2026 12:38
@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.

@BigSimmo
BigSimmo merged commit 8bec4b5 into mainJun 27, 2026
4 checks passed
@BigSimmo
BigSimmo deleted the copilot/task-87357024-1243497866-26178f79-0b72-4b9d-a4e5-d0500fca33c6 branch June 27, 2026 12:38

CopilotAI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR optimizes the hottest RAG cache read/write paths by replacing JSON round-trip deep cloning with structuredClone in src/lib/rag.ts, reducing CPU overhead and avoiding string serialization for large nested objects.

Changes:

  • Replace JSON.parse(JSON.stringify(...)) with structuredClone(...) in cloneAnswer.
  • Replace JSON.parse(JSON.stringify(...)) with structuredClone(...) in cloneSearchResults.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Sign up for freeto 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.

3 participants

@BigSimmo