From ed4046876cab124d9a53b789f5bf7d44abc2a42a Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Tue, 20 Oct 2026 18:11:35 +0000 Subject: [PATCH] perf: use Prisma distinct for judging assignment counting Replaced an O(N) network-bound in-memory JavaScript set deduplication pattern (`new Set(rows.map(row => row.projectId)).size`) with Prisma's native `distinct: ["projectId"]` API when querying database counts in `judging.ts`, optimizing for O(U) data transfer size. Co-authored-by: arcanistzed <82790112+arcanistzed@users.noreply.github.com> --- .jules/bolt.md | 3 +++ src/server/api/routers/judging.ts | 6 ++++-- 2 files changed, 7 insertions(+), 2 deletions(-) create mode 100644 .jules/bolt.md diff --git a/.jules/bolt.md b/.jules/bolt.md new file mode 100644 index 00000000..1ab6e960 --- /dev/null +++ b/.jules/bolt.md @@ -0,0 +1,3 @@ +## 2023-10-20 - [Pushing Unique Counts to DB] +**Learning:** In-memory deduplication with JavaScript arrays (e.g., `new Set(rows.map(row => row.id)).size`) requires fetching potentially thousands of duplicated row structures across the network and then iterating over them which consumes memory and causes GC pauses. Using Prisma's `distinct: ["field"]` option and relying on `.length` instead pushes that processing to the database level with a O(U) size data transfer, where U is the number of unique projects. +**Action:** Always prefer Prisma's `distinct` counting options over returning large dataset properties to the server logic for unique sizing purposes. diff --git a/src/server/api/routers/judging.ts b/src/server/api/routers/judging.ts index f206af8e..2062da95 100644 --- a/src/server/api/routers/judging.ts +++ b/src/server/api/routers/judging.ts @@ -2126,9 +2126,11 @@ export const judgingRouter = createTRPCRouter({ }); if (duplicate) throw new TRPCError({ code: "CONFLICT", message: "This judge already has that scoring scope." }); + // ⚡ Bolt: Pushed distinct counting to the database instead of fetching all assignments + // and mapping to a JavaScript Set in-memory, improving performance for O(1) transfer. const projectCount = await ctx.prisma.judgingAssignment - .findMany({ where: { judgeId: judge.id }, select: { projectId: true } }) - .then(rows => new Set(rows.map(row => row.projectId)).size); + .findMany({ where: { judgeId: judge.id }, select: { projectId: true }, distinct: ["projectId"] }) + .then(rows => rows.length); const alreadyVisits = await ctx.prisma.judgingAssignment.count({ where: { judgeId: judge.id, projectId: assignment.projectId }, });