Skip to content

Commit 39718d5

Browse files
committed
perf(review): address PR-1027 round-3 nits (deleted-file fetch, summary alloc, shift O(N²))
Three centralized-review findings on the previous commit `6729f5374`, all perf/hygiene: ## MEDIUM — schema.yml orchestrator fetched `oldContent` for deleted files `file.status !== "added"` on the gate matched `"deleted"` too, triggering a `git show` round-trip whose result the detector immediately discarded (early-returns `[]` for `status === "deleted"`). Fix: filter deleted schema files out of the loop upfront — `reviewable.filter((f) => f.kind === "schema_yml" && f.status !== "deleted")`. The `newContent` fetch also simplifies to unconditional `getContent?.(file.path, "new")` since deleted files never reach the loop. ## MEDIUM — `distinctModels`/`modelClause` recomputed on every loop iteration The `[...new Set(...).filter(...).map(...).join(...)]` chain was evaluated on every removal even though the result is only used when `shouldEmitSummary` is true (once per file). Moved the computation inside the guard. ## LOW — `fallbackDiscriminators.shift()` inside the loop is O(N²) `Array.prototype.shift()` re-indexes all remaining elements on each call. Replaced with a hoisted `let fallbackIdx = 0` + `fallbackDiscriminators[fallbackIdx++]`. Negligible on 2-3 removals but grows quadratically on wider PRs. ## Test status 188 pass / 0 fail across the 7 review test files. Typecheck clean. No behavior change beyond the deleted-file fetch skip (which is a pure performance improvement — same output, fewer round-trips).
1 parent 6729f53 commit 39718d5

2 files changed

Lines changed: 25 additions & 12 deletions

File tree

‎packages/opencode/src/altimate/review/dbt-patterns.ts‎

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1050,6 +1050,11 @@ export function detectSchemaYmlPatterns(
10501050
// global summary. A single boolean guard is enough.
10511051
let summaryEmitted = false
10521052

1053+
// Consumed in lock-step with the removals loop below (fallback path only).
1054+
// Using an index avoids `shift()`, which would be O(N) per call on the
1055+
// discriminator array — negligible on 2-3 removals but O(N²) on a large
1056+
// schema.yml PR that drops many tests.
1057+
let fallbackIdx = 0
10531058
for (const r of removals) {
10541059
const isUniquenessSignal = r.test === "unique"
10551060
const notNullOnLikelyPK =
@@ -1096,24 +1101,27 @@ export function detectSchemaYmlPatterns(
10961101
// Emit the aggregate summary once per file, on the first finding, and
10971102
// only when there are multiple removals to summarise. Uses the file-scoped
10981103
// `summaryEmitted` flag rather than a per-model guard so a diff touching
1099-
// three models emits ONE summary, not three.
1104+
// three models emits ONE summary, not three. Distinct-models computation
1105+
// is gated inside the branch so we don't pay for the Set/spread/map on
1106+
// every loop iteration when the summary won't be attached.
11001107
const shouldEmitSummary = !summaryEmitted && removals.length > 1
1101-
if (shouldEmitSummary) summaryEmitted = true
1102-
const distinctModels = [...new Set(removals.map((x) => x.model).filter(Boolean))]
1103-
const modelClause = distinctModels.length
1104-
? ` on model(s) ${distinctModels.map((m) => `\`${m}\``).join(", ")}`
1105-
: ""
1106-
const bodyTail = shouldEmitSummary
1107-
? `\n\n_This PR removes ${removals.length} data tests in total${modelClause}._`
1108-
: ""
1108+
let bodyTail = ""
1109+
if (shouldEmitSummary) {
1110+
summaryEmitted = true
1111+
const distinctModels = [...new Set(removals.map((x) => x.model).filter(Boolean))]
1112+
const modelClause = distinctModels.length
1113+
? ` on model(s) ${distinctModels.map((m) => `\`${m}\``).join(", ")}`
1114+
: ""
1115+
bodyTail = `\n\n_This PR removes ${removals.length} data tests in total${modelClause}._`
1116+
}
11091117

11101118
// ruleKey feeds the finding fingerprint (finding.ts:107). For attributed
11111119
// (structural) findings, `(model.column.test)` is unique per removal in
11121120
// the file. For unattributed fallback findings, `(?.?.test)` collapses
11131121
// distinct removals of the same test type — we append the fallback
11141122
// discriminator so each removed line becomes a distinct finding
11151123
// downstream of the global dedupe.
1116-
const discriminator = attributed || modelLevel ? "" : `.${fallbackDiscriminators.shift() ?? "#?"}`
1124+
const discriminator = attributed || modelLevel ? "" : `.${fallbackDiscriminators[fallbackIdx++] ?? "#?"}`
11171125
findings.push(
11181126
makeFinding({
11191127
severity: clampSeverity("test_coverage", sev, "high"),

‎packages/opencode/src/altimate/review/orchestrate.ts‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1230,14 +1230,19 @@ export async function runReview(input: OrchestrateInput): Promise<VerdictEnvelop
12301230
//
12311231
// Fetches run in parallel across schema files (a schema-heavy PR could touch
12321232
// dozens of yml files; serial `git show` per file adds up).
1233-
const schemaFiles = reviewable.filter((f) => f.kind === "schema_yml")
1233+
//
1234+
// DELETED schema files are filtered upfront because `detectSchemaYmlPatterns`
1235+
// early-returns `[]` for `status === "deleted"` anyway; not filtering here
1236+
// would trigger an unnecessary `getContent(oldRef, "old")` fetch (a git-show
1237+
// round-trip) whose result is thrown away by the detector.
1238+
const schemaFiles = reviewable.filter((f) => f.kind === "schema_yml" && f.status !== "deleted")
12341239
if (schemaFiles.length) {
12351240
const schemaFindingSets = await Promise.all(
12361241
schemaFiles.map(async (file) => {
12371242
const oldRef = file.oldPath ?? file.path
12381243
const [oldContent, newContent] = await Promise.all([
12391244
file.status !== "added" ? getContent?.(oldRef, "old") : Promise.resolve(undefined),
1240-
file.status !== "deleted" ? getContent?.(file.path, "new") : Promise.resolve(undefined),
1245+
getContent?.(file.path, "new"),
12411246
])
12421247
return detectSchemaYmlPatterns(file, input.rubric, { oldContent, newContent })
12431248
}),

0 commit comments

Comments
 (0)