Repository navigation
Leave an OrderedCollection unchanged when its comparer throws during AddRange [patch] - #160
Open
matt-edmondson wants to merge 2 commits into
Open
matt-edmondson wants to merge 2 commits into
matt-edmondson wants to merge 2 commits into
Conversation
…AddRange [patch] The bulk path of AddRange grew the backing list and merged in place from the back, so a comparer that threw partway left the collection unsorted, with some elements duplicated and others lost. Merge into a new array and swap it in only once the merge finishes, as OrderedMap.AddRange does. Fixes #158 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017vfJW3k7ndhCVDCcjtiA4W
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017vfJW3k7ndhCVDCcjtiA4W
|
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.



Fixes #158
What was wrong
For a batch over 16 elements,
OrderedCollection<T>.AddRangegrew the backing list and then merged in place from the back. The merge is the first place new elements are compared with existing ones, so a comparer that threw there left the list grown and partly overwritten. The collection ended up unsorted, with some elements duplicated and others lost.Change
AddRangenow merges into a newT[]and replaces the list contents only after the merge completes, which is whatOrderedMap.AddRangealready does. Ties still place the added element after the existing one.AddRange_ComparerThrowsDuringMerge_LeavesCollectionUnchangeduses the repro from the issue (17 elements, a comparer that throws on 30). It fails onmainand passes with the fix.AddRange_ComparerThrowsOnSmallBatch_LeavesCollectionSortedpins the small-batch path: after a throw the collection is still sorted, keeps its original elements, and has no duplicates.Verification
Generated by Claude Code