From ed9855725f7d64d6da515ffce7dbfd6d49d7623c Mon Sep 17 00:00:00 2001 From: Dante Dam Date: Tue, 17 Nov 2026 11:05:28 -0800 Subject: [PATCH 1/2] refactor(websoc): save one request for small by-section-codes --- apps/api/src/services/websoc.ts | 51 +++++++++++++++++++-------------- 1 file changed, 30 insertions(+), 21 deletions(-) diff --git a/apps/api/src/services/websoc.ts b/apps/api/src/services/websoc.ts index 90681fa4a..01d5ab3e2 100644 --- a/apps/api/src/services/websoc.ts +++ b/apps/api/src/services/websoc.ts @@ -417,9 +417,27 @@ export class WebsocService { return { schools }; } + async buildFromSingleQuery(input: WebsocServiceInput) { + return this.makeSelect( + { + school: getTableColumns(websocSchool), + department: getTableColumns(websocDepartment), + course: getTableColumns(websocCourse), + section: getTableColumns(websocSection), + }, + true, + ) + .where(buildQuery(input)) + .then((rows) => rows as JoinedRow[]) + .then(this.transformJoinedRows); + } + async getWebsocResponse( input: WebsocServiceInput, ): Promise> { + let sectionsUpperBound = + input.sectionCodes && !input.includeRelatedCourses ? input.sectionCodes.length : undefined; + let matchingSectionRows: { sectionId: string }[] = []; if (input.includeRelatedCourses) { @@ -432,36 +450,27 @@ export class WebsocService { matchingSectionRows = await this.makeSelect({ sectionId: websocSection.id }, false) .rightJoin(sub, eq(websocCourse.id, sub.courseId)) .then((rows) => rows as { sectionId: string }[]); - } else { + sectionsUpperBound = matchingSectionRows.length; + } + + if (sectionsUpperBound === undefined) { + // don't do this query unless we really don't know the upper bound matchingSectionRows = await this.makeSelect({ sectionId: websocSection.id }, true) .where(buildQuery(input)) .then((row) => row as { sectionId: string }[]); + sectionsUpperBound = matchingSectionRows.length; } - const sectionIds = matchingSectionRows.map(({ sectionId }) => sectionId); - if (sectionIds.length === 0) { + if (sectionsUpperBound === 0) { return { schools: [] }; - } - - if (sectionIds.length > 1000) { + } else if (sectionsUpperBound < 1000) { + // we know the query will be small enough + return this.buildFromSingleQuery(input); + } else { // we sometimes OOM on big (e.g. entire term) requests... // do multiple queries to avoid thousands of copies of dept/school object coming from sql and being gc'd too slowly + const sectionIds = matchingSectionRows.map(({ sectionId }) => sectionId); return this.buildFromRequeries(sectionIds); - } else { - // when set is small enough, short round-trip time is preferable for slightly higher memory - // and this set is small enough to never OOM - return this.makeSelect( - { - school: getTableColumns(websocSchool), - department: getTableColumns(websocDepartment), - course: getTableColumns(websocCourse), - section: getTableColumns(websocSection), - }, - true, - ) - .where(buildQuery(input)) - .then((rows) => rows as JoinedRow[]) - .then(this.transformJoinedRows); } } From 954f646ed0956c9f9de3a6af3bd5a434326b0d7d Mon Sep 17 00:00:00 2001 From: Dante Dam Date: Tue, 17 Nov 2026 11:10:49 -0800 Subject: [PATCH 2/2] comments --- apps/api/src/services/websoc.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/apps/api/src/services/websoc.ts b/apps/api/src/services/websoc.ts index 01d5ab3e2..bc4a9897d 100644 --- a/apps/api/src/services/websoc.ts +++ b/apps/api/src/services/websoc.ts @@ -435,6 +435,9 @@ export class WebsocService { async getWebsocResponse( input: WebsocServiceInput, ): Promise> { + // compute this upper bound as eagerly as possible; if a list of section codes is provided + // (which it commonly is, use that immediately) + // NB that related courses may expand the query so we can't do this in that case let sectionsUpperBound = input.sectionCodes && !input.includeRelatedCourses ? input.sectionCodes.length : undefined; @@ -454,7 +457,7 @@ export class WebsocService { } if (sectionsUpperBound === undefined) { - // don't do this query unless we really don't know the upper bound + // our convenient estimates did not apply; get an upper bound (which will be true by equality) matchingSectionRows = await this.makeSelect({ sectionId: websocSection.id }, true) .where(buildQuery(input)) .then((row) => row as { sectionId: string }[]);