Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .jules/bolt.md
Original file line number Diff line number Diff line change
@@ -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.
6 changes: 4 additions & 2 deletions src/server/api/routers/judging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
});
Expand Down
Loading