Skip to content

Commit 61f0241

Browse files
perf: reuse ResizeArray buffer in chunkBy and chunkByAsync
Previously, each time a new chunk was started in chunkBy/chunkByAsync, a fresh ResizeArray was allocated. Since ToArray() already captures an independent copy of the data, the internal buffer can be safely reused by calling Clear() instead of constructing a new ResizeArray. This eliminates one heap allocation per chunk boundary, reducing GC pressure for sequences with many distinct runs. 5017 tests pass, 2 skipped. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 852f4b1 commit 61f0241

File tree

2 files changed

+3
-2
lines changed

2 files changed

+3
-2
lines changed

release-notes.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
Release notes:
33

44
1.0.0
5+
- perf: TaskSeq.chunkBy and chunkByAsync reuse the ResizeArray buffer between chunks, reducing allocations on sequences with many chunk boundaries
56
- fixes: TaskSeq.insertAt, insertManyAt, removeAt, removeManyAt, updateAt now raise ArgumentNullException (not NullReferenceException) when given a null source; insertManyAt also validates the values argument
67
- refactor: simplify lengthBy and lengthBeforeMax to use while! and remove the redundant mutable 'go' and initial MoveNextAsync
78
- adds TaskSeq.distinctUntilChangedWith and TaskSeq.distinctUntilChangedWithAsync, #345

src/FSharp.Control.TaskSeq/TaskSeqInternal.fs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1754,7 +1754,7 @@ module internal TaskSeqInternal =
17541754
currentChunk.Add item
17551755
else
17561756
yield prevKey, currentChunk.ToArray()
1757-
currentChunk <- ResizeArray<'T>()
1757+
currentChunk.Clear() // reuse backing array; ToArray() already captured a snapshot
17581758
currentChunk.Add item
17591759
maybeCurrentKey <- ValueSome key
17601760

@@ -1782,7 +1782,7 @@ module internal TaskSeqInternal =
17821782
currentChunk.Add item
17831783
else
17841784
yield prevKey, currentChunk.ToArray()
1785-
currentChunk <- ResizeArray<'T>()
1785+
currentChunk.Clear() // reuse backing array; ToArray() already captured a snapshot
17861786
currentChunk.Add item
17871787
maybeCurrentKey <- ValueSome key
17881788

0 commit comments

Comments
 (0)