Repository navigation
Don't invalidate enumerators on capacity changes that keep item positions - #137213
Merged
Merged
Conversation
…ions dotnet#82500 removed these increments from List<T> and Stack<T>, because resizing the array there doesn't change what an enumerator sees. The same is true for a few more types that still bump the version: - Queue<T> (EnsureCapacity and TrimExcess, via SetCapacity). dotnet#82500 left it alone because growing relays out the items from index 0, but the enumerator stores an offset from _head and re-reads _head and _array on every MoveNext, and SetCapacity resets _head to 0. It already worked this way in 2023. - Dictionary<TKey, TValue>.EnsureCapacity. Resize copies the entries to the same indices. HashSet<T>.EnsureCapacity never had the increment. - PriorityQueue<TElement, TPriority>.EnsureCapacity and TrimExcess. Both are an Array.Resize, and the enumerator reads _nodes[_index]. - OrderedDictionary<TKey, TValue>.EnsureCapacity. Resize is an Array.Copy, and TrimExcess already didn't increment. TrimExcess on Dictionary and HashSet still invalidates, since it compacts the entries. The Queue and Dictionary EnsureCapacity tests now check that the enumeration continues with the same items, and there are new tests for Queue.TrimExcess (with the items wrapped around the array), PriorityQueue and OrderedDictionary that change the capacity in the middle of an enumeration.
|
Azure Pipelines: Successfully started running 3 pipeline(s). 12 pipeline(s) were filtered out due to trigger conditions. There may be pipelines that require an authorized user to comment /azp run to run. |
Contributor
|
Tagging subscribers to this area: @dotnet/area-system-collections |
…ment These tests compare the serialized fields of a Queue<T>, including _version. The first Enqueue grows the empty array, and SetCapacity no longer increments the version, so the expected values are one lower.
Contributor
Author
|
Second commit updates three DCS/DCJS baselines that include |
krwq
reviewed
Nov 13, 2026
krwq
reviewed
Nov 13, 2026
…ries move TrimExcess copies the entries with CopyEntries, which only moves entries when it compacts free slots left by Remove. When there are none (nothing removed, or the slots were reused by later adds or reset by Clear) every entry keeps its index, so the enumerators keep working. Increment the version only when _freeCount > 0.
3 tasks
jkotas
reviewed
Nov 16, 2026
…when entries move" This reverts commit 838d42a. TrimExcess keeps the unconditional increment so whether it invalidates doesn't depend on earlier removals.
krwq
approved these changes
Nov 20, 2026
Member
Copilot Failures Analysis
|
This was referenced Nov 20, 2026
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.
#82500 removed the capacity-only
_version++fromList<T>andStack<T>, since resizing their arrays doesn't change what an enumerator returns. This does the same for the other types where that's true:Queue<T>:SetCapacity, whichEnsureCapacityand bothTrimExcessoverloads go through. Remove unnecessary _version++ from a few places #82500 kept this one because growing a queue relays out the items from index 0. But the enumerator stores an offset from_headand re-reads_headand_arrayon each MoveNext, andSetCapacityresets_headto 0. That was already the case when Remove unnecessary _version++ from a few places #82500 was merged.Dictionary<TKey, TValue>.EnsureCapacity:Resizecopies the entries to the same indices.HashSet<T>.EnsureCapacitynever had the increment.PriorityQueue<TElement, TPriority>.EnsureCapacityandTrimExcess: both are anArray.Resize, and the enumerator reads_nodes[_index].OrderedDictionary<TKey, TValue>.EnsureCapacity:Resizeis anArray.Copy. ItsTrimExcessalready didn't increment.TrimExcessonDictionaryandHashSetkeeps the increment. It compacts the entries when some were removed. With none removed the entries stay at the same indices, but it increments anyway, so that whether it invalidates doesn't depend on earlier removals.The increment in
Dictionary.EnsureCapacitycame from dotnet/coreclr#19096, after a review comment thatTrimExcessandEnsureCapacitycould invalidate an enumeration. That's true forTrimExcessbut not forEnsureCapacity.The Queue and Dictionary EnsureCapacity tests are flipped to
DoesNotInvalidateEnumeration, like #82500 did for List and Stack, and now check that the enumeration returns the same items. The PriorityQueueEnsureCapacity(2 * Count)case moved to the non-modifying operations. New tests for QueueTrimExcess(with the items wrapped around the end of the array), PriorityQueue and OrderedDictionary change the capacity in the middle of an enumeration and compare the whole sequence.System.Collections.Tests passes on osx-arm64. With the increments put back, 28 of the new and changed tests fail. I also checked it outside the test suite: interrupt an enumeration at every position with the capacity change, finish it, and compare with an undisturbed enumeration. That covered sizes 0 to 64, wrapped and unwrapped queues, and dictionaries with and without removed entries, 53,625 cases in all, with no differences.
The
Dictionary<TKey, TValue>.GetEnumeratordocs say that "changing the capacity" invalidates the enumerator. That's still true forTrimExcess, but I can send a docs PR to make it specific if this goes in.Related to #81523.