-
Couldn't load subscription status.
- Fork 5.2k
Faster optimized frozen dictionary creation (6/6) #88093
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
3973b70
don't use custom ToArray for small frozen collections, up to 50% gain…
adamsitnik 3914d9b
for these types GetHashCode returns their value casted to int, so whe…
adamsitnik 0c29242
move Length Buckets code to a dedicated helper type to reduce code du…
adamsitnik bdd93e6
Merge remote-tracking branch 'upstream/main' into minorFrozenPerfFixes
adamsitnik 29b7121
address code review feedback:
adamsitnik 8f057e8
address code review feedback: improve the comment
adamsitnik 4fbdc78
address code review feedback: add nint and uint test coverage
adamsitnik f7267b9
address code review feedback: remove NET6_0_OR_GREATER case
adamsitnik 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 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
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
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
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
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
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
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
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
102 changes: 102 additions & 0 deletions
102
...raries/System.Collections.Immutable/src/System/Collections/Frozen/String/LengthBuckets.cs
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System.Buffers; | ||
| using System.Collections.Generic; | ||
| using System.Diagnostics; | ||
|
|
||
| namespace System.Collections.Frozen | ||
| { | ||
| internal static class LengthBuckets | ||
| { | ||
| /// <summary>The maximum number of items allowed per bucket. The larger the value, the longer it can take to search a bucket, which is sequentially examined.</summary> | ||
| internal const int MaxPerLength = 5; | ||
| /// <summary>Allowed ratio between buckets with values and total buckets. Under this ratio, this implementation won't be used due to too much wasted space.</summary> | ||
| private const double EmptyLengthsRatio = 0.2; | ||
|
|
||
| internal static int[]? CreateLengthBucketsArrayIfAppropriate(string[] keys, IEqualityComparer<string> comparer, int minLength, int maxLength) | ||
| { | ||
| Debug.Assert(comparer == EqualityComparer<string>.Default || comparer == StringComparer.Ordinal || comparer == StringComparer.OrdinalIgnoreCase); | ||
| Debug.Assert(minLength >= 0 && maxLength >= minLength); | ||
|
|
||
| // If without even looking at the keys we know that some bucket will exceed the max per-bucket | ||
| // limit (pigeon hole principle), we can early-exit out without doing any further work. | ||
| int spread = maxLength - minLength + 1; | ||
| if (keys.Length / spread > MaxPerLength) | ||
| { | ||
| return null; | ||
| } | ||
|
|
||
| int arraySize = spread * MaxPerLength; | ||
| #if NET6_0_OR_GREATER | ||
| if (arraySize > Array.MaxLength) | ||
| #else | ||
| if (arraySize > 0X7FFFFFC7) | ||
| #endif | ||
| { | ||
| // In the future we may lower the value, as it may be quite unlikely | ||
| // to have a LOT of strings of different sizes. | ||
| return null; | ||
| } | ||
|
|
||
| // Instead of creating a dictionary of lists or a multi-dimensional array | ||
| // we rent a single dimension array, where every bucket has five slots. | ||
| // The bucket starts at (key.Length - minLength) * 5. | ||
| // Each value is an index of the key from _keys array | ||
| // or just -1, which represents "null". | ||
| int[] buckets = ArrayPool<int>.Shared.Rent(arraySize); | ||
| buckets.AsSpan(0, arraySize).Fill(-1); | ||
|
|
||
| int nonEmptyCount = 0; | ||
| for (int i = 0; i < keys.Length; i++) | ||
| { | ||
| string key = keys[i]; | ||
| int startIndex = (key.Length - minLength) * MaxPerLength; | ||
| int endIndex = startIndex + MaxPerLength; | ||
| int index = startIndex; | ||
|
|
||
| while (index < endIndex) | ||
| { | ||
| ref int bucket = ref buckets[index]; | ||
| if (bucket < 0) | ||
| { | ||
| if (index == startIndex) | ||
| { | ||
| nonEmptyCount++; | ||
| } | ||
|
|
||
| bucket = i; | ||
| break; | ||
| } | ||
|
|
||
| index++; | ||
| } | ||
|
|
||
| if (index == endIndex) | ||
| { | ||
| // If we've already hit the max per-bucket limit, bail. | ||
| ArrayPool<int>.Shared.Return(buckets); | ||
| return null; | ||
| } | ||
| } | ||
|
|
||
| // If there would be too much empty space in the lookup array, bail. | ||
| if (nonEmptyCount / (double)spread < EmptyLengthsRatio) | ||
| { | ||
| ArrayPool<int>.Shared.Return(buckets); | ||
| return null; | ||
| } | ||
|
|
||
| #if NET6_0_OR_GREATER | ||
| // We don't need an array with every value initialized to zero if we are just about to overwrite every value anyway. | ||
| int[] copy = GC.AllocateUninitializedArray<int>(arraySize); | ||
| Array.Copy(buckets, copy, arraySize); | ||
| #else | ||
| int[] copy = buckets.AsSpan(0, arraySize).ToArray(); | ||
| #endif | ||
| ArrayPool<int>.Shared.Return(buckets); | ||
|
|
||
| return copy; | ||
| } | ||
| } | ||
| } |
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.