Skip to content

Commit

Permalink
Fix memory memory management for flatMap
Browse files Browse the repository at this point in the history
prevent the flatMap operator from accumulating in an internal array while concurrency is maxed out
  • Loading branch information
thepont committed Oct 7, 2024
1 parent b8890f1 commit 752157f
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/asynciterable/operators/_flatten.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,12 @@ export class FlattenConcurrentAsyncIterable<TSource, TResult> extends AsyncItera
}
if (active < concurrent) {
pullNextOuter(value as TSource);
results[0] = outer.next();
} else {
// remove the outer iterator from the race, we're full
results[0] = NEVER_PROMISE;
outerValues.push(value as TSource);
}
results[0] = outer.next();
break;
}
case Type.INNER: {
Expand All @@ -119,6 +121,8 @@ export class FlattenConcurrentAsyncIterable<TSource, TResult> extends AsyncItera
}
case Type.INNER: {
--active;
// add the outer iterator to the race
results[0] = outer.next();
// return the current slot to the pool
innerIndices.push(index);
// synchronously drain the `outerValues` buffer
Expand Down

0 comments on commit 752157f

Please sign in to comment.