Skip to content
This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
/ corefx Public archive

Enumerate over dictionary keys and values without allocations #246

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,10 @@
<Compile Include="System\Collections\Immutable\ImmutableDictionary`2+DebuggerProxy.cs" />
<Compile Include="System\Collections\Immutable\ImmutableDictionary`2+Enumerator.cs" />
<Compile Include="System\Collections\Immutable\ImmutableDictionary`2+HashBucket.cs" />
<Compile Include="System\Collections\Immutable\ImmutableDictionary`2+KeyCollection.cs" />
<Compile Include="System\Collections\Immutable\ImmutableDictionary`2+MutationInput.cs" />
<Compile Include="System\Collections\Immutable\ImmutableDictionary`2+MutationResult.cs" />
<Compile Include="System\Collections\Immutable\ImmutableDictionary`2+ValueCollection.cs" />
<Compile Include="System\Collections\Immutable\ImmutableDictionary`2.cs" />
<Compile Include="System\Collections\Immutable\ImmutableExtensions.cs" />
<Compile Include="System\Collections\Immutable\ImmutableHashSet.cs" />
Expand Down
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();
}
}
}
}
}
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();
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -166,34 +166,44 @@ public IEqualityComparer<TValue> ValueComparer
/// <summary>
/// Gets the keys in the map.
/// </summary>
public IEnumerable<TKey> Keys
public KeyCollection Keys
Copy link
Contributor

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.

Copy link
Member Author

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 and ValueCollection constructors, which allows users to use instances of these types without incurring allocations. Use of the Keys and Values properties will require a boxing allocation but should be slightly more efficient overall than the current iterator implementation.

Copy link

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 compatible

Copy link
Contributor

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:

foreach (var key in dictionary.Keys) {
   ...
}

After:

foreach (var pair in dictionary) {
    var key = pair.Key;
    ...
}

As such, I suggest we leave things as they are.

{
get
{
foreach (var bucket in this.root)
{
foreach (var item in bucket.Value)
{
yield return item.Key;
}
}
return new KeyCollection(this);
}
}

/// <summary>
/// Gets the keys in the map.
/// </summary>
IEnumerable<TKey> IReadOnlyDictionary<TKey, TValue>.Keys
{
get
{
return Keys;
}
}

/// <summary>
/// Gets the values in the map.
/// </summary>
public IEnumerable<TValue> Values
public ValueCollection Values
{
get
{
foreach (var bucket in this.root)
{
foreach (var item in bucket.Value)
{
yield return item.Value;
}
}
return new ValueCollection(this);
}
}

/// <summary>
/// Gets the values in the map.
/// </summary>
IEnumerable<TValue> IReadOnlyDictionary<TKey, TValue>.Values
{
get
{
return Values;
}
}

Expand Down