Repository navigation
aspnetcore: ef updates - #5933
Conversation
| int currentValue, newValue; | ||
|
|
||
| var ids = Enumerable.Range(1, 10000).OrderBy(x => random.Next()).Take(count); | ||
| var ids = Enumerable.Range(1, 10000).Select(x => _random.Next(1, 10001)).Distinct().Take(count).ToArray(); |
There was a problem hiding this comment.
In this version, you have a couple of issues:
- no guarantee that enough distinct numbers will be created to satisfy
count - it does many allocations: some for the
Set<T>inDistinct(), some for the temporaryBuffer<T>inToArray()and one for the final array (with a copy).
The previous implementation does have some allocations implied by OrderBy() but it also has the guarantee of having sufficient elements.
That said, both propositions do too many allocations and are not efficient. Instead we can use a simple Fisher-Yates to generate a randomize range in O(n) with only one allocation. See my own implementation as an example.
var ids = ShuffleUtility.RandomRange(1, 10000).Take(count);
foreach (int id in ids)
{
// etc.
}There was a problem hiding this comment.
count is 20 at most so enough distinct numbers should be generated, else there may be a problem with Random.Next(). All implementations are required to generate distinct Ids in order to pass test verification, I believe, though it may allow for a margin of error.
Regarding allocations, there may be a few but I doubt it matters, the database is likely the performance bottleneck.
Feel free to open a PR, if it really improves rps, great.
* aspcore: add platform multiple queries * Add missing console output * tweak postgresql connectionstring * aspnetcore: ef updates Co-authored-by: Stefan Negulescu <[email protected]>
Try to improve Entity Framework Updates.