Skip to content

Commit 5c53c4d

Browse files
committed
perf(web): the retention estimate charges a row that holds its own copy
A version 5 or uncompacted record keeps a visible row whose copy differs from its item, and the estimate charged that row 64 bytes, so a large stale copy could take a thread past the budget unseen.
1 parent 4953f59 commit 5c53c4d

2 files changed

Lines changed: 18 additions & 7 deletions

File tree

‎packages/client-runtime/src/state/threadRetention.test.ts‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,14 @@ describe("estimateRetainedThreadBytes", () => {
4545
} as unknown as OrchestrationV2ThreadProjection;
4646
expect(estimateRetainedThreadBytes(inherited)).toBeGreaterThan(1_000);
4747
});
48+
49+
it("charges a local row that holds its own copy of an item", () => {
50+
const shared = withItems(1, 1_000);
51+
const copied = {
52+
...shared,
53+
visibleTurnItems: shared.visibleTurnItems.map((row) => ({ ...row, item: { ...row.item } })),
54+
} as unknown as OrchestrationV2ThreadProjection;
55+
const extra = estimateRetainedThreadBytes(copied) - estimateRetainedThreadBytes(shared);
56+
expect(extra).toBeGreaterThan(1_000);
57+
});
4858
});

‎packages/client-runtime/src/state/threadRetention.ts‎

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,18 @@ function retainedBytes(value: unknown): number {
2626
}
2727

2828
/**
29-
* A cheap estimate of what a thread keeps in the heap, from its items, inherited rows and message
30-
* records, with strings counted by length. It walks one thread when the thread is left. Timeline
31-
* rows and parsed markdown come on top, which matters only for small threads.
29+
* A cheap estimate of what a thread keeps in the heap, from its items, rows holding an item of their
30+
* own and message records, with strings counted by length. It walks one thread when the thread is
31+
* left. Timeline rows and parsed markdown come on top, which matters only for small threads.
3232
*/
3333
export function estimateRetainedThreadBytes(projection: OrchestrationV2ThreadProjection): number {
3434
let bytes = retainedBytes(projection.turnItems) + retainedBytes(projection.messages);
35+
const turnItems = new Set<object>(projection.turnItems);
3536
for (const row of projection.visibleTurnItems) {
36-
bytes +=
37-
row.sourceThreadId === projection.thread.id
38-
? OBJECT_OVERHEAD_BYTES
39-
: retainedBytes(row.item) + OBJECT_OVERHEAD_BYTES;
37+
// An inherited row, or a saved record's copy that differs from its item, holds its own.
38+
bytes += turnItems.has(row.item)
39+
? OBJECT_OVERHEAD_BYTES
40+
: retainedBytes(row.item) + OBJECT_OVERHEAD_BYTES;
4041
}
4142
return bytes;
4243
}

0 commit comments

Comments
 (0)