Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement SegmentedDictionary<TKey, TValue> #50156

Merged
merged 20 commits into from
Jan 13, 2021

Conversation

sharwell
Copy link
Member

@sharwell sharwell commented Dec 29, 2020

This collection is derived from Dictionary<TKey, TValue> (dotnet/runtime@cb5f173b, which is .NET 5.0.2), with the backing arrays replaced by SegmentedArray<T> to avoid all Large Object Heap allocations. In most cases, the performance of this collection is indistinguishable from Dictionary<TKey, TValue>, with the exception of not using the Large Object Heap.

Aside from the changes to segment the storage arrays, the following changes are made relative to the original implementation:

  • SegmentedDictionary<TKey, TValue> does not support binary serialization
  • SegmentedDictionary<TKey, TValue> always uses default string comparers by default, and does not attempt to force the use of non-randomized comparers for performance in low-collision scenarios
  • SegmentedDictionary<TKey, TValue> always uses HashHelpers.FastMod, which optimizes AnyCPU performance for 64-bit runtimes

💡 If necessary, updates from dotnet/runtime can be incorporated back into roslyn by branching off of
276a8a6 (which is a verbatim copy of the original files updated to include a link to the original source), and then merging the resulting minimized changes back into master. Commit 276a8a6 shows the update from dotnet/runtime@cf258a14 to dotnet/runtime@cb5f173b.

@333fred

This comment has been minimized.

@sharwell

This comment has been minimized.

@sharwell sharwell force-pushed the segmented-dictionary branch from 0055246 to 7db5534 Compare December 29, 2020 18:30
@@ -13,13 +13,57 @@ internal static class SegmentedArray
/// <seealso cref="Array.Clear(Array, int, int)"/>
internal static void Clear<T>(SegmentedArray<T> buckets, int index, int length)
{
throw new NotImplementedException();
if (index < 0 || length < 0 || (uint)(index + length) > (uint)buckets.Length)
ThrowHelper.ThrowArgumentOutOfRangeException();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider breaking this up and including the argument that was out of range.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With that said, this class is weird - the instinct is to keep all the original coding approach from Array but it's both ugly and difficult to port directly. So I could really go either way on it.

@333fred
Copy link
Member

333fred commented Dec 29, 2020

Done review pass (commit 15)

@sharwell sharwell force-pushed the segmented-dictionary branch from 4304898 to aa2ec01 Compare December 29, 2020 22:44
@sharwell
Copy link
Member Author

@jaredpar Can you review this?

Copy link
Member

@jaredpar jaredpar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When copying code from other repos we should link to that code so bug and security fixes can be ported.

@sharwell sharwell force-pushed the segmented-dictionary branch from c3b3d7a to 7ff5c6f Compare January 12, 2021 22:11
@sharwell sharwell merged commit 0ee10f2 into dotnet:master Jan 13, 2021
@ghost ghost added this to the Next milestone Jan 13, 2021
@sharwell sharwell deleted the segmented-dictionary branch January 13, 2021 00:18
@Cosifne Cosifne modified the milestones: Next, 16.9 Jan 27, 2021
@JoeRobich JoeRobich modified the milestones: 16.9, 16.9.P4 Jan 27, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants