[Repo Assist] perf+fix: optimize pairwise, fix splitAt/tryTail enumerator disposal on exception#320
Draft
github-actions[bot] wants to merge 2 commits intomainfrom
Conversation
…on exception Task 8 (Performance): Optimize AsyncSeq.pairwise to use a hasPrev flag and mutable field instead of wrapping prev in Some on every step. Eliminates per-element heap allocation for long sequences. Task 3 (Bug fix): Fix resource leak in splitAt and tryTail. Previously, if the source sequence threw an exception during the initial MoveNext() call, the underlying enumerator was never disposed. Added try...with to call ie.Dispose() on exception. Also replaces 'ref' cell with 'mutable' in splitAt's rest sequence. Tests: 425/425 pass (3 new tests added) Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
7 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
🤖 This PR was created by Repo Assist, an automated AI assistant.
Summary
Two improvements in one PR: a performance optimization (Task 8) and a bug fix (Task 3).
Task 8 — Performance:
AsyncSeq.pairwiseRoot cause: The previous implementation used
Optionboxing for theprevelement (prev <- Some von every iteration), allocating a new heap object per step.Fix: Replace
'T optionwith ahasPrev: boolflag and a directmutable prev: 'Tfield.Benefit: Eliminates one
'T optionheap allocation per input element, reducing GC pressure for long sequences.Task 3 — Bug fix:
splitAtandtryTailenumerator disposal on exceptionRoot cause: Both functions obtained an enumerator with
let ie = source.GetEnumerator()(notuse ie), intending to transfer ownership to the returnedAsyncSeq. However, if the source threw an exception during the initialMoveNext()call,ie.Dispose()was never called — causing a resource leak.Fix: Wrap the initial async body in
try...with ex -> ie.Dispose(); return raise exso the enumerator is always disposed on exception or cancellation.Bonus: The
let cur = ref bref cell insplitAt's rest sequence was also replaced withlet mutable cur = b(no allocation, same semantics).Trade-off: The "caller discards the rest without enumerating it" case is a pre-existing limitation of this API design and is not addressed here (it would require a different return type).
Test Status
✅ Build: 0 errors, pre-existing warnings only
✅ Tests: 425/425 passed (3 new tests added)
AsyncSeq.pairwise with many elements produces correct pairs— validates the optimization produces correct output for a 10-element sequenceAsyncSeq.splitAt disposes enumerator when source throws during collection— verifies disposal on exception mid-collectionAsyncSeq.tryTail disposes enumerator when source throws on first MoveNext— verifies disposal when first async step fails