Skip to content

perf: reduce dots() N+1 queries to a single batch fetch - #159

Open
dhruv1220 wants to merge 1 commit into
CopilotKit:mainfrom
dhruv1220:perf-dots-batch
Open

dhruv1220 wants to merge 1 commit into
CopilotKit:mainfrom
dhruv1220:perf-dots-batch

Conversation

@dhruv1220

Copy link
Copy Markdown

Closes #146.

What changed

WorkspaceStore.dots() issued one SELECT * FROM dots followed by one SELECT spaceId FROM dot_spaces WHERE dotId=? per dot — N+1 database round-trips just to list the workspace. It now fetches all dot_spaces rows once and groups them in memory with a Map, cutting the listing down to 2 queries total.

Notes

  • Per-dot grant ordering is preserved (grants are sorted in memory, matching the previous ORDER BY spaceId).
  • A dot with no grants reads back spaceIds: [] — the grouping keys off the grants table, so a missing entry is an empty list rather than a fallback to a nonexistent column.

Testing

  • New regression test in tests/workspace.test.ts: seeds dots across multiple spaces (including one with zero grants), asserts the returned spaceIds are correct, and asserts dots() issues exactly 2 prepare calls (red-checked: fails on unpatched code, which issues N+1).
  • Full suite: 343 passed; the only 2 failures are the pre-existing network-dependent tests/transport.test.ts cases, which fail identically on clean main.
  • tsc --noEmit, eslint, prettier --check clean; production build (npm run build) green.

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.

1 participant