This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Enumerate over dictionary keys and values without allocations #246
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
139 changes: 139 additions & 0 deletions
139
...ections.Immutable/src/System/Collections/Immutable/ImmutableDictionary`2+KeyCollection.cs
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using System.Collections.Generic; | ||
|
||
namespace System.Collections.Immutable | ||
{ | ||
/// <content> | ||
/// Contains the inner KeyCollection struct. | ||
/// </content> | ||
partial class ImmutableDictionary<TKey, TValue> | ||
{ | ||
/// <summary> | ||
/// This structure represents a collection of keys in a dictionary. | ||
/// </summary> | ||
public struct KeyCollection : IEnumerable<TKey> | ||
{ | ||
/// <summary> | ||
/// The underlying dictionary. | ||
/// </summary> | ||
private readonly ImmutableDictionary<TKey, TValue> _dictionary; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="KeyCollection"/> structure. | ||
/// </summary> | ||
/// <param name="dictionary">The dictionary.</param> | ||
internal KeyCollection(ImmutableDictionary<TKey, TValue> dictionary) | ||
{ | ||
_dictionary = dictionary; | ||
} | ||
|
||
/// <summary> | ||
/// Returns an enumerator that iterates through the collection. | ||
/// </summary> | ||
/// <returns> | ||
/// A <see cref="Enumerator"/> that can be used to iterate through the collection. | ||
/// </returns> | ||
public Enumerator GetEnumerator() | ||
{ | ||
return new Enumerator(_dictionary.GetEnumerator()); | ||
} | ||
|
||
/// <summary> | ||
/// Returns an enumerator that iterates through the collection. | ||
/// </summary> | ||
/// <returns> | ||
/// A <see cref="IEnumerator{T}"/> that can be used to iterate through the collection. | ||
/// </returns> | ||
IEnumerator<TKey> IEnumerable<TKey>.GetEnumerator() | ||
{ | ||
return GetEnumerator(); | ||
} | ||
|
||
/// <summary> | ||
/// Returns an enumerator that iterates through the collection. | ||
/// </summary> | ||
/// <returns> | ||
/// A <see cref="IEnumerator"/> that can be used to iterate through the collection. | ||
/// </returns> | ||
IEnumerator IEnumerable.GetEnumerator() | ||
{ | ||
return GetEnumerator(); | ||
} | ||
|
||
/// <summary> | ||
/// Enumerates the contents of the collection in an allocation-free manner. | ||
/// </summary> | ||
public struct Enumerator : IEnumerator<TKey> | ||
{ | ||
/// <summary> | ||
/// The enumerator over key/value pairs in the dictionary. | ||
/// </summary> | ||
private ImmutableDictionary<TKey, TValue>.Enumerator _dictionaryEnumerator; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="Enumerator"/> structure. | ||
/// </summary> | ||
/// <param name="dictionaryEnumerator">The dictionary enumerator.</param> | ||
internal Enumerator(ImmutableDictionary<TKey, TValue>.Enumerator dictionaryEnumerator) | ||
: this() | ||
{ | ||
_dictionaryEnumerator = dictionaryEnumerator; | ||
} | ||
|
||
/// <summary> | ||
/// Gets the current element. | ||
/// </summary> | ||
public TKey Current | ||
{ | ||
get | ||
{ | ||
return _dictionaryEnumerator.Current.Key; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Gets the current element. | ||
/// </summary> | ||
object IEnumerator.Current | ||
{ | ||
get | ||
{ | ||
return Current; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Advances the enumerator to the next element of the collection. | ||
/// </summary> | ||
/// <returns> | ||
/// true if the enumerator was successfully advanced to the next element; false if the enumerator has | ||
/// passed the end of the collection. | ||
/// </returns> | ||
/// <exception cref="InvalidOperationException">The collection was modified after the enumerator was created.</exception> | ||
public bool MoveNext() | ||
{ | ||
return _dictionaryEnumerator.MoveNext(); | ||
} | ||
|
||
/// <summary> | ||
/// Sets the enumerator to its initial position, which is before the first element in the collection. | ||
/// </summary> | ||
/// <exception cref="InvalidOperationException">The collection was modified after the enumerator was created.</exception> | ||
public void Reset() | ||
{ | ||
_dictionaryEnumerator.Reset(); | ||
} | ||
|
||
/// <summary> | ||
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. | ||
/// </summary> | ||
public void Dispose() | ||
{ | ||
_dictionaryEnumerator.Dispose(); | ||
} | ||
} | ||
} | ||
} | ||
} |
139 changes: 139 additions & 0 deletions
139
...tions.Immutable/src/System/Collections/Immutable/ImmutableDictionary`2+ValueCollection.cs
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using System.Collections.Generic; | ||
|
||
namespace System.Collections.Immutable | ||
{ | ||
/// <content> | ||
/// Contains the inner ValueCollection struct. | ||
/// </content> | ||
partial class ImmutableDictionary<TKey, TValue> | ||
{ | ||
/// <summary> | ||
/// This structure represents a collection of values in a dictionary. | ||
/// </summary> | ||
public struct ValueCollection : IEnumerable<TValue> | ||
{ | ||
/// <summary> | ||
/// The underlying dictionary. | ||
/// </summary> | ||
private readonly ImmutableDictionary<TKey, TValue> _dictionary; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="ValueCollection"/> structure. | ||
/// </summary> | ||
/// <param name="dictionary">The dictionary.</param> | ||
internal ValueCollection(ImmutableDictionary<TKey, TValue> dictionary) | ||
{ | ||
_dictionary = dictionary; | ||
} | ||
|
||
/// <summary> | ||
/// Returns an enumerator that iterates through the collection. | ||
/// </summary> | ||
/// <returns> | ||
/// A <see cref="Enumerator"/> that can be used to iterate through the collection. | ||
/// </returns> | ||
public Enumerator GetEnumerator() | ||
{ | ||
return new Enumerator(_dictionary.GetEnumerator()); | ||
} | ||
|
||
/// <summary> | ||
/// Returns an enumerator that iterates through the collection. | ||
/// </summary> | ||
/// <returns> | ||
/// A <see cref="IEnumerator{T}"/> that can be used to iterate through the collection. | ||
/// </returns> | ||
IEnumerator<TValue> IEnumerable<TValue>.GetEnumerator() | ||
{ | ||
return GetEnumerator(); | ||
} | ||
|
||
/// <summary> | ||
/// Returns an enumerator that iterates through the collection. | ||
/// </summary> | ||
/// <returns> | ||
/// A <see cref="IEnumerator"/> that can be used to iterate through the collection. | ||
/// </returns> | ||
IEnumerator IEnumerable.GetEnumerator() | ||
{ | ||
return GetEnumerator(); | ||
} | ||
|
||
/// <summary> | ||
/// Enumerates the contents of the collection in an allocation-free manner. | ||
/// </summary> | ||
public struct Enumerator : IEnumerator<TValue> | ||
{ | ||
/// <summary> | ||
/// The enumerator over key/value pairs in the dictionary. | ||
/// </summary> | ||
private ImmutableDictionary<TKey, TValue>.Enumerator _dictionaryEnumerator; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="Enumerator"/> structure. | ||
/// </summary> | ||
/// <param name="dictionaryEnumerator">The dictionary enumerator.</param> | ||
internal Enumerator(ImmutableDictionary<TKey, TValue>.Enumerator dictionaryEnumerator) | ||
: this() | ||
{ | ||
_dictionaryEnumerator = dictionaryEnumerator; | ||
} | ||
|
||
/// <summary> | ||
/// Gets the current element. | ||
/// </summary> | ||
public TValue Current | ||
{ | ||
get | ||
{ | ||
return _dictionaryEnumerator.Current.Value; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Gets the current element. | ||
/// </summary> | ||
object IEnumerator.Current | ||
{ | ||
get | ||
{ | ||
return Current; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Advances the enumerator to the next element of the collection. | ||
/// </summary> | ||
/// <returns> | ||
/// true if the enumerator was successfully advanced to the next element; false if the enumerator has | ||
/// passed the end of the collection. | ||
/// </returns> | ||
/// <exception cref="InvalidOperationException">The collection was modified after the enumerator was created.</exception> | ||
public bool MoveNext() | ||
{ | ||
return _dictionaryEnumerator.MoveNext(); | ||
} | ||
|
||
/// <summary> | ||
/// Sets the enumerator to its initial position, which is before the first element in the collection. | ||
/// </summary> | ||
/// <exception cref="InvalidOperationException">The collection was modified after the enumerator was created.</exception> | ||
public void Reset() | ||
{ | ||
_dictionaryEnumerator.Reset(); | ||
} | ||
|
||
/// <summary> | ||
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. | ||
/// </summary> | ||
public void Dispose() | ||
{ | ||
_dictionaryEnumerator.Dispose(); | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains 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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unfortunately, this is a breaking change that would prevent applications built against the stable 1.0 immutable collections from running against the new version. We preserve binary compatibility between stable package versions and therefore I don't think we can make this transformation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for taking a look. I wasn't sure what breaking changes policy was in place since no stable releases of System.Collections.Immutable have appeared on NuGet yet. I see now that the changes are also evaluated against the stable release of Microsoft.Bcl.Immutable.
Tomorrow I'll update this pull request to revert the change to this property and instead expose the
KeyCollection
andValueCollection
constructors, which allows users to use instances of these types without incurring allocations. Use of theKeys
andValues
properties will require a boxing allocation but should be slightly more efficient overall than the current iterator implementation.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You could just change the return type to
IEnumerable<TKey/TValue>
and it should be binary compatibleThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@pdelvo In other words revert this part of the change as @sharwell suggested he'd do next? :)
I don't think the extra code for the key and value enumeration is worth it if won't save on boxing for existing code. Consumers needing maximum efficiency can already get it with just one extra line of code:
Before:
After:
As such, I suggest we leave things as they are.