-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
Performance improvements for Random.Shuffle() #83305
Closed
Closed
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
6307d29
Performance improvements for Random.Shuffle()
wainwrightmark 262e466
Merge branch 'dotnet:main' into shuffle-performance
wainwrightmark 7895efd
Apply suggestions from code review
wainwrightmark d94eb2e
Apply suggestions from code review
wainwrightmark 5e54371
Introduced assertions for thresholds in IncreasingUniform
wainwrightmark 60c7fce
Use Pearson's chi-squared test for Shuffle_Array_Fairness
wainwrightmark 90893c5
Fixed indentation
wainwrightmark 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -273,18 +273,160 @@ public void Shuffle<T>(T[] values) | |
/// </remarks> | ||
public void Shuffle<T>(Span<T> values) | ||
{ | ||
int n = values.Length; | ||
var chooser = new IncreasingUniform(this); | ||
for (int i = 1; i < values.Length; i++) | ||
{ | ||
int index = chooser.NextIndex(); | ||
|
||
// This pattern is faster than tuple deconstruction for large structs | ||
var swap = values[index]; | ||
values[index] = values[i]; | ||
values[i] = swap; | ||
} | ||
} | ||
|
||
private struct IncreasingUniform | ||
{ | ||
private readonly Random _random; | ||
private int _n; | ||
private int _chunk; | ||
private int _chunkRemaining; | ||
|
||
public IncreasingUniform(Random random) | ||
{ | ||
_random = random; | ||
_chunk = 0; | ||
_n = 1; | ||
_chunkRemaining = 0; | ||
} | ||
|
||
for (int i = 0; i < n - 1; i++) | ||
/// <summary> | ||
/// Increase n by one and then return a random positive integer less than the new n | ||
/// </summary> | ||
public int NextIndex() | ||
{ | ||
int j = Next(i, n); | ||
int nextN = _n + 1; | ||
if (_chunkRemaining == 0) | ||
{ | ||
(int bound, int remaining) = CalculateBound(nextN); | ||
_chunkRemaining = remaining - 1; | ||
_chunk = _random.Next(bound); | ||
} | ||
else | ||
{ | ||
_chunkRemaining--; | ||
} | ||
|
||
if (j != i) | ||
int result; | ||
if (_chunkRemaining == 0) | ||
{ | ||
T temp = values[i]; | ||
values[i] = values[j]; | ||
values[j] = temp; | ||
result = _chunk; | ||
} | ||
else | ||
{ | ||
result = _chunk % nextN; | ||
_chunk /= nextN; | ||
} | ||
|
||
_n = nextN; | ||
return result; | ||
} | ||
|
||
/// <summary> | ||
/// Calculate the highest (x,k) such that x = n*n+1*..*n+k-1 where x is a 32 bit integer, | ||
/// assuming n is 2, 13, or >= 20 | ||
/// </summary> | ||
private static (int, int) CalculateBound(int n) | ||
{ | ||
// This method should always first be called with 2, then 13, then higher numbers starting at 20 | ||
Debug.Assert(n == 2 || n == 13 || n > 19); | ||
// Threshold_6 is the highest value of n for which k will be 6 | ||
const int Threshold_6 = 33; | ||
const int Threshold_5 = 71; | ||
const int Threshold_4 = 213; | ||
const int Threshold_3 = 1289; | ||
const int Threshold_2 = 46340; | ||
AssertIsThreshold(6, Threshold_6); | ||
AssertIsThreshold(5, Threshold_5); | ||
AssertIsThreshold(4, Threshold_4); | ||
AssertIsThreshold(3, Threshold_3); | ||
AssertIsThreshold(2, Threshold_2); | ||
|
||
int count; | ||
switch (n) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can this switch use fancy new pattern matching syntax to be more compact? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I tried this but I don't think relational patterns allow return statements. |
||
{ | ||
case 2: | ||
const int Product_2_11 = 2 * 3 * 4 * 5 * 6 * 7 * 8 * 9 * 10 * 11 * 12; | ||
Debug.Assert(Product_2_11 == 479001600); | ||
return (Product_2_11, 11); | ||
case 13: | ||
const int Product_13_7 = 13 * 14 * 15 * 16 * 17 * 18 * 19; | ||
Debug.Assert(Product_13_7 == 253955520); | ||
return (Product_13_7, 7); | ||
case <= Threshold_6: | ||
count = 6; | ||
break; | ||
case <= Threshold_5: | ||
count = 5; | ||
break; | ||
case <= Threshold_4: | ||
count = 4; | ||
break; | ||
case <= Threshold_3: | ||
count = 3; | ||
break; | ||
case <= Threshold_2: | ||
count = 2; | ||
break; | ||
default: | ||
return (n, 1); | ||
} | ||
|
||
int product = GetProduct(n, count - 1); | ||
return (product, count); | ||
} | ||
|
||
private static int GetProduct(int n, int multiplications) | ||
{ | ||
int product = n; | ||
while (multiplications > 0) | ||
{ | ||
product *= n + multiplications; | ||
multiplications--; | ||
} | ||
|
||
return product; | ||
} | ||
|
||
/// <summary> | ||
/// Checks that t*t+1*..*t+k-1 is a 32 bit integer, and that t is the highest integer such that this is the case | ||
/// </summary> | ||
[System.Diagnostics.Conditional("DEBUG")] | ||
private static void AssertIsThreshold(int k, int t) | ||
{ | ||
static bool IsProductI32(int n, int multiplications) | ||
{ | ||
try | ||
{ | ||
checked | ||
{ | ||
int product = n; | ||
while (multiplications > 0) | ||
{ | ||
product *= n + multiplications; | ||
multiplications--; | ||
} | ||
} | ||
} | ||
catch (OverflowException) | ||
{ | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
Debug.Assert(IsProductI32(t, k - 1), $"k = {k}; t = {t}"); | ||
Debug.Assert(!IsProductI32(t, k), $"k + 1 = {k + 1}; t = {t}"); | ||
} | ||
} | ||
|
||
|
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.
is that something the compiler could/should improve?
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.
It was mentioned here, my understand is that it will be fixed after C# 11