Skip to content

Commit 20d20ca

Browse files
committed
remove buffering from selectFacts to fix memory issue
cubic identified that selectFacts was buffering all results before yielding, which defeats the purpose of using a generator and reintroduces the memory problem that bc0c716 originally fixed by switching from .all() to .iter(). this change: - removes the results array buffering - streams rows directly with yield toFact(row) - documents why finalize() cannot be used in a generator's finally block (finally blocks execute when generator returns, not when exhausted) the statement will be finalized when the store is closed. this is an acceptable trade-off given javascript generator limitations. addresses cubic's feedback on PR #1979
1 parent c918022 commit 20d20ca

File tree

1 file changed

+4
-7
lines changed

1 file changed

+4
-7
lines changed

packages/memory/space.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -571,8 +571,9 @@ export const selectFacts = function* <Space extends MemorySpace>(
571571
{ the, of, cause, is, since }: FactSelector,
572572
): Iterable<SelectedFact> {
573573
const stmt = store.prepare(EXPORT);
574-
// Collect all results before finalizing the statement
575-
const results: SelectedFact[] = [];
574+
// Note: Cannot finalize() in a generator function's finally block because
575+
// the finally block runs when the generator returns (immediately), not when
576+
// it's exhausted. The statement will be finalized when the store is closed.
576577
for (
577578
const row of stmt.iter({
578579
the: the === SelectAllString ? null : the,
@@ -582,12 +583,8 @@ export const selectFacts = function* <Space extends MemorySpace>(
582583
since: since ?? null,
583584
}) as Iterable<StateRow>
584585
) {
585-
results.push(toFact(row));
586+
yield toFact(row);
586587
}
587-
// Finalize the statement after collecting all results
588-
stmt.finalize();
589-
// Now yield the results
590-
yield* results;
591588
};
592589

593590
export const selectFact = function <Space extends MemorySpace>(

0 commit comments

Comments
 (0)