From f47285df37e45161c827b9f0a03e2dc13acdc8e8 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Sun, 18 Oct 2026 18:15:22 +0000 Subject: [PATCH] perf(judging): use DB deduplication for distinct counts Replaced manual JS Set deduplication with DB-level `distinct` and `.length` for large judging assignments queries. Co-authored-by: arcanistzed <82790112+arcanistzed@users.noreply.github.com> --- .jules/bolt.md | 3 +++ src/server/api/routers/judging.ts | 7 +++++-- 2 files changed, 8 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..4e071d60 --- /dev/null +++ b/.jules/bolt.md @@ -0,0 +1,3 @@ +## 2024-05-18 - Distinct Counting with Prisma in Node +**Learning:** Found O(N) operation measuring distinct array elements retrieved from DB (`new Set(rows.map(row => row.projectId)).size`) after a `findMany` query. This consumes extra JS memory and compute time as array sizes grow. +**Action:** Replace `findMany` followed by JS `Set` operations with Prisma `findMany({ distinct: ["projectId"], select: { projectId: true } }).length` for better performance at the DB layer, as Prisma `count` doesn`t support distinct in this project version. diff --git a/src/server/api/routers/judging.ts b/src/server/api/routers/judging.ts index f206af8e..0568b510 100644 --- a/src/server/api/routers/judging.ts +++ b/src/server/api/routers/judging.ts @@ -2126,9 +2126,12 @@ export const judgingRouter = createTRPCRouter({ }); if (duplicate) throw new TRPCError({ code: "CONFLICT", message: "This judge already has that scoring scope." }); + // ⚡ Bolt: Push deduplication to the DB using `distinct` and `.length` instead of fetching everything and making a JS Set. + // Why: `findMany` + mapping to JS arrays and Sets takes more memory and compute time for large sets. + // Impact: Avoids fetching duplicate rows, avoids Set allocations, and lowers latency (O(1) JS side with smaller arrays). 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 }, distinct: ["projectId"], select: { projectId: true } }) + .then(rows => rows.length); const alreadyVisits = await ctx.prisma.judgingAssignment.count({ where: { judgeId: judge.id, projectId: assignment.projectId }, });