[Repo Assist] Fix traverseOptionAsync/traverseChoiceAsync — avoid wasteful MoveNext on failure#284
Draft
github-actions[bot] wants to merge 2 commits intomainfrom
Conversation
…l MoveNext on failure Both functions previously called ie.MoveNext() unconditionally at the end of each loop iteration. When the mapping function f returned failure (None / Choice2Of2), the loop set the sentinel variables and then still called MoveNext(), reading one element from the source that would never be used. For sequences with observable side effects on enumeration this was unexpected behaviour. Fix: restructure the loop so MoveNext() is only called in the success branch. Also modernise mutable state from ref cells to mutable locals. Tests: 4 new tests covering - success path for both functions (returns all mapped values) - 'does not read past failing element' assertion (enumerator read count) All 325 tests pass. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
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
Refactors
AsyncSeq.traverseOptionAsyncandAsyncSeq.traverseChoiceAsyncto fix a subtle over-enumeration bug and improve readability.The bug
Both functions used a
whileloop structured like this:When
freturnsNone(orChoice2Of2), the code correctly set the exit flags — but then unconditionally calledie.MoveNext()anyway, reading one extra element from the source that would never be used. For sequences with observable side effects on enumeration (logging, side-effecting generators) this was surprising and incorrect.The fix
The
MoveNext()call is moved inside the success branch so it only runs when we actually need the next element:refcells are also replaced withmutablelocals, andValueOptionis used for the error holder intraverseChoiceAsyncto avoid heap-allocating aSomewrapper on every run.New tests
traverseOptionAsync returns Some sequence when all elements succeedtraverseOptionAsync does not read past failing elementtraverseChoiceAsync returns Choice1Of2 sequence when all elements succeedtraverseChoiceAsync does not read past failing elementTest Status
✅ Build succeeded (0 errors, pre-existing warnings only — NU1605, FS9999 for
groupByAsync).✅ All 325 tests pass — 4 new, 321 pre-existing.