Skip to content

Commit 48bf7fd

Browse files
t3dotggclaude
andcommitted
perf(usage): steps without metadata no longer extend one statement
`WHERE metadata IS NOT NULL ... LIMIT 64` bounds the rows a statement returns, not the rows it scans. A run of 50,000 steps without metadata blocked the event loop for 193 ms in one statement. Read every step row and skip the empty ones in the loop, so each statement scans at most 64. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent cb4b93f commit 48bf7fd

2 files changed

Lines changed: 11 additions & 5 deletions

File tree

‎apps/server/src/provider/Drivers/antigravityUsageReader.test.ts‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -493,10 +493,11 @@ describe("readAntigravityUsage", () => {
493493
antigravityGeneration(`g-${idx}`),
494494
]);
495495
}
496-
for (let idx = 0; idx < 150; idx++) {
496+
// A run of steps without metadata sits between steps with usage.
497+
for (let idx = 0; idx < 250; idx++) {
497498
yield* db.run("INSERT INTO steps (idx, metadata) VALUES (?, ?)", [
498499
idx,
499-
stepUsage(`s-${idx}`),
500+
idx >= 75 && idx < 175 ? null : stepUsage(`s-${idx}`),
500501
]);
501502
}
502503
yield* db.exec("COMMIT");
@@ -518,7 +519,8 @@ describe("readAntigravityUsage", () => {
518519
assert.deepStrictEqual(
519520
records(result).map((record) => record.dedupeKey),
520521
[
521-
...Array.from({ length: 150 }, (_, idx) => `antigravity:11:s-${idx}`),
522+
...Array.from({ length: 75 }, (_, idx) => `antigravity:11:s-${idx}`),
523+
...Array.from({ length: 75 }, (_, idx) => `antigravity:11:s-${idx + 175}`),
522524
...Array.from({ length: 3 }, (_, idx) => `antigravity:11:g-${idx}`),
523525
],
524526
);
@@ -531,8 +533,9 @@ describe("readAntigravityUsage", () => {
531533
}),
532534
[],
533535
);
536+
// Every row, with metadata or not, comes back through a bounded statement.
534537
for (const [table, total, most] of [
535-
["steps", 150, 64],
538+
["steps", 250, 64],
536539
["gen_metadata", 3, 1],
537540
] as const) {
538541
const rows = reads

‎apps/server/src/provider/Drivers/antigravityUsageReader.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,9 @@ const readDatabase = Effect.fn("readDatabase")(function* (
223223
}>(query, [after, pageRows])
224224
.pipe(Effect.withTracerEnabled(false));
225225
for (const row of rows) {
226+
// Skipped here rather than in SQL, so a statement never scans past
227+
// more than one page of steps that have no metadata.
228+
if (step && row.data === null) continue;
226229
const idx = row.idx;
227230
if (typeof idx !== "number") {
228231
return yield* new AntigravityDatabaseDecodeError({ path });
@@ -252,7 +255,7 @@ const readDatabase = Effect.fn("readDatabase")(function* (
252255
}
253256
const steps = tables.has("steps")
254257
? yield* readMetadata(
255-
"SELECT rowid AS row_id, idx, metadata AS data FROM steps WHERE metadata IS NOT NULL AND rowid > ? ORDER BY rowid LIMIT ?",
258+
"SELECT rowid AS row_id, idx, metadata AS data FROM steps WHERE rowid > ? ORDER BY rowid LIMIT ?",
256259
STEP_PAGE_ROWS,
257260
true,
258261
)

0 commit comments

Comments
 (0)